diff options
author | Rico Tzschichholz <ricotz@ubuntu.com> | 2022-11-21 10:55:44 +0100 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2022-12-08 15:37:08 +0100 |
commit | 865733142cad7f44f1c41a3e4bcd043048a3c756 (patch) | |
tree | e35043cab381c99ff339d4097ddc4b4ac07f7c3f /vala | |
parent | 546567e487bd503d9c4bb2c5a1543fcc871740fe (diff) | |
download | vala-865733142cad7f44f1c41a3e4bcd043048a3c756.tar.gz |
vala: Improve integer literal checks
Diffstat (limited to 'vala')
-rw-r--r-- | vala/valaintegerliteral.vala | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/vala/valaintegerliteral.vala b/vala/valaintegerliteral.vala index e28960126..ec6146bc2 100644 --- a/vala/valaintegerliteral.vala +++ b/vala/valaintegerliteral.vala @@ -78,7 +78,36 @@ public class Vala.IntegerLiteral : Literal { value = value.substring (0, value.length - 1); } - int64 n = int64.parse (value); + bool negative = value.has_prefix ("-"); + if (negative && u) { + Report.error (source_reference, "unsigned integer literal cannot be negative"); + error = true; + } + + int64 n = 0LL; + uint64 un = 0ULL; + + errno = 0; + if (negative) { + n = int64.parse (value); + } else { + un = uint64.parse (value); + } + if (errno == ERANGE) { + Report.error (source_reference, "integer literal is too large for its type"); + error = true; + } else if (errno == EINVAL) { + Report.error (source_reference, "invalid integer literal"); + error = true; + } + + if (un > int64.MAX) { + // value doesn't fit into signed 64-bit + u = true; + l = 2; + } else if (!negative) { + n = (int64) un; + } if (!u && (n > int.MAX || n < int.MIN)) { // value doesn't fit into signed 32-bit l = 2; |