diff options
author | Jo-Philipp Wich <jo@mein.io> | 2020-09-07 15:50:52 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2020-09-07 16:30:34 +0200 |
commit | a35469d34be384d3a5ae7b852acb6d980f6b702e (patch) | |
tree | bf94fae7176da654944e765ed5ae1ecc1b2a0fee /eval.c | |
parent | dcf0d1285d12684a5907fecb8aadca7bc2e5f44b (diff) |
ast, eval, lexer: keep track of overflows when parsing numbers
This allows number literals that exceed the range INT64_MIN..INT64_MAX
to be truncated to the respective min and max values in a defined manner.
It also makes it possible to have the expression `{{ -9223372036854775808 }}`
actually result in `-9223372036854775808`. Since negation and number
declaration are separate operations, the value would be first truncated to
`9223372036854775807` and then negated, making it impossible to write a
literal INT64_MIN value without tracking the overflow.
Also fix the number parsing logic to not trucate intergers to 32bit.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'eval.c')
-rw-r--r-- | eval.c | 4 |
1 files changed, 4 insertions, 0 deletions
@@ -995,6 +995,7 @@ static struct json_object * ut_execute_unary_plus_minus(struct ut_state *state, uint32_t off) { struct ut_op *op = ut_get_op(state, off); + struct ut_op *op1 = ut_get_child(state, off, 0); struct json_object *val = ut_execute_op(state, op ? op->tree.operand[0] : 0); enum json_type t; int64_t n; @@ -1006,6 +1007,9 @@ ut_execute_unary_plus_minus(struct ut_state *state, uint32_t off) switch (t) { case json_type_int: + if (op1->is_overflow) + return json_object_new_int64(((n >= 0) == (op->type == T_SUB)) ? INT64_MIN : INT64_MAX); + return json_object_new_int64((op->type == T_SUB) ? -n : n); default: |