|
| 1 | +# Using int as boolean values |
| 2 | + |
| 3 | +In FunC, booleans are represented as integers; false is represented as 0 and true is represented as -1 (257 ones in binary notation). |
| 4 | + |
| 5 | +Logical operations are done as bitwise operations over the binary representation of the integer values. Notably, The not operation `~` flips all the bits of an integer value; therefore, a non-zero value other than -1 becomes another non-zero value. |
| 6 | + |
| 7 | +When a condition is checked, every non-zero integer is considered a true value. This, combined with the logical operations being bitwise operations, leads to an unexpected behavior of `if` conditions using the logical operations. |
| 8 | + |
| 9 | +## Example |
| 10 | + |
| 11 | +The following simplified code highlights the unexpected behavior of the `~` operator on a non-zero interger value. |
| 12 | + |
| 13 | +```FunC |
| 14 | +#include "imports/stdlib.fc"; |
| 15 | +
|
| 16 | +() recv_internal(int my_balance, int msg_value, cell in_msg_full, slice in_msg_body) impure { |
| 17 | + int correct_true = -1; |
| 18 | + if (correct_true) { |
| 19 | + ~strdump("correct_true is true"); ;; printed |
| 20 | + } else { |
| 21 | + ~strdump("correct_true is false"); |
| 22 | + } |
| 23 | +
|
| 24 | + if (~ correct_true) { |
| 25 | + ~strdump("~correct_true is true"); |
| 26 | + } else { |
| 27 | + ~strdump("~correct_true is false"); ;; printed |
| 28 | + } |
| 29 | +
|
| 30 | + int correct_false = 0; |
| 31 | + if (correct_false) { |
| 32 | + ~strdump("correct_false is true"); |
| 33 | + } else { |
| 34 | + ~strdump("correct_false is false"); ;; printed |
| 35 | + } |
| 36 | +
|
| 37 | + if (~ correct_false) { |
| 38 | + ~strdump("~correct_false is true"); ;; printed |
| 39 | + } else { |
| 40 | + ~strdump("~correct_false is false"); |
| 41 | + } |
| 42 | +
|
| 43 | + int positive = 10; |
| 44 | + if (positive) { |
| 45 | + ~strdump("positive is true"); ;; printed |
| 46 | + } else { |
| 47 | + ~strdump("positive is false"); |
| 48 | + } |
| 49 | +
|
| 50 | + if (~ positive) { |
| 51 | + ~strdump("~positive is true"); ;; printed but unexpected |
| 52 | + } else { |
| 53 | + ~strdump("~positive is false"); |
| 54 | + } |
| 55 | +
|
| 56 | + int negative = -10; |
| 57 | + if (negative) { |
| 58 | + ~strdump("negative is true"); ;; printed |
| 59 | + } else { |
| 60 | + ~strdump("negative is false"); |
| 61 | + } |
| 62 | +
|
| 63 | + if (~ negative) { |
| 64 | + ~strdump("~negative is true"); ;; printed but unexpected |
| 65 | + } else { |
| 66 | + ~strdump("~negative is false"); |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +The `recv_internal` function above prints the following debug logs: |
| 72 | +``` |
| 73 | + #DEBUG#: correct_true is true |
| 74 | + #DEBUG#: ~correct_true is false |
| 75 | + #DEBUG#: correct_false is false |
| 76 | + #DEBUG#: ~correct_false is true |
| 77 | + #DEBUG#: positive is true |
| 78 | + #DEBUG#: ~positive is true |
| 79 | + #DEBUG#: negative is true |
| 80 | + #DEBUG#: ~negative is true |
| 81 | +``` |
| 82 | + |
| 83 | +It demonstrats that the `~ 10` and `~ -10` both evaluate to `true` instead of becoming `false` with the `~` operator. |
| 84 | + |
| 85 | +## Mitigations |
| 86 | + |
| 87 | +- Always use `0` or `-1` in condition checks to get correct results. |
| 88 | +- Be careful with the logical operator usage on non-zero integer values. |
| 89 | +- Implement test cases to verify correct behavior of all condition checks with different interger values. |
0 commit comments