|
| 1 | +#### TruncatedDivision.ql |
| 2 | + |
| 3 | +# NOTE: The following test case will only work under Python 2. |
| 4 | + |
| 5 | +# Truncated division occurs when two integers are divided. This causes the |
| 6 | +# fractional part, if there is one, to be discared. So for example, `2 / 3` will |
| 7 | +# evaluate to `0` instead of `0.666...`. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +## Negative Cases |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +# This case is good, and is a minimal obvious case that should be good. It |
| 18 | +# SHOULD NOT be found by the query. |
| 19 | +print(3.0 / 2.0) |
| 20 | + |
| 21 | +# This case is good, because it explicitly converts the possibly-truncated |
| 22 | +# value to an integer. It SHOULD NOT be found by the query. |
| 23 | + |
1 | 24 | def return_three(): |
2 | 25 | return 3 |
3 | 26 |
|
4 | 27 | def return_two(): |
5 | 28 | return 2 |
6 | 29 |
|
7 | | -def f1(): |
8 | | - return 3 / 2 |
| 30 | +print(int(return_three() / return_two())) |
| 31 | + |
| 32 | + |
9 | 33 |
|
10 | | -def f2(): |
11 | | - return return_three() / return_two() |
| 34 | +# These cases are good, because `halve` checks the type, and if the type would |
| 35 | +# truncate, it explicitly converts to a float first before doing the division. |
| 36 | +# These SHOULD NOT be found by the query. |
12 | 37 |
|
13 | | -def f3(x): |
| 38 | +def halve(x): |
14 | 39 | if isinstance(x, float): |
15 | 40 | return x / 2 |
16 | 41 | else: |
17 | 42 | return (1.0 * x) / 2 |
18 | 43 |
|
19 | | -def f4(): |
20 | | - do_stuff(f3(1)) |
21 | | - do_stuff(f3(1.0)) |
| 44 | +print(halve(1)) |
| 45 | +print(halve(1.0)) |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +# This case is good, because the sum is `3.0`, which is a float, and will not |
| 50 | +# truncate. This case SHOULD NOT be found by the query. |
| 51 | + |
| 52 | +print(average([1.0, 2.0])) |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +## Positive Cases |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +# This case is bad, and is a minimal obvious case that should be bad. It |
| 63 | +# SHOULD be found by the query. |
| 64 | + |
| 65 | +print(3 / 2) |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +# This case is bad. It uses indirect returns of integers through function calls |
| 70 | +# to produce the problem. I |
| 71 | + |
| 72 | +print(return_three() / return_two()) |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +# This case is bad, because the sum is `3`, which is an integer, and will |
| 77 | +# truncate when divided by the length `2`. This case SHOULD be found by the |
| 78 | +# query. |
| 79 | + |
| 80 | +# NOTE (2020-02-20): |
| 81 | +# The current version of the Value/pointsTo API doesn't permit this detection, |
| 82 | +# unfortunately, but we preserve this example in the hopes that future |
| 83 | +# versions will catch it. That will necessitate changing the expected results. |
| 84 | + |
| 85 | +def average(l): |
| 86 | + return sum(l) / len(l) |
22 | 87 |
|
23 | | -def f5(): |
24 | | - return int(return_three() / return_two()) |
| 88 | +print(average([1,2])) |
0 commit comments