compiler: avoid silent truncation for string(1 << 32)
authorIan Lance Taylor <iant@golang.org>
Tue, 24 Nov 2020 01:48:28 +0000 (17:48 -0800)
committerIan Lance Taylor <iant@golang.org>
Wed, 25 Nov 2020 16:02:39 +0000 (08:02 -0800)
In the conversion of a constant integer to a string type, the value of
the constant integer was being silently truncated from unsigned long
to unsigned int, producing the wrong string value.  Add an explicit
overflow check to avoid this problem.

For golang/go#42790

Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/272611

gcc/go/gofrontend/MERGE
gcc/go/gofrontend/expressions.cc

index 3e94dabcd3075e0717e8febe29eee5d2f5177c05..c14ee7e7b142f617595eceebb81f7f1d19a8e091 100644 (file)
@@ -1,4 +1,4 @@
-78c9a657fdbc9e812d39910fb93fbae4affe4360
+8cbe18aff99dbf79bd1adb9c025418e84505ffd5
 
 The first line of this file holds the git revision number of the last
 merge done from the gofrontend repository.
index 6bc93488939dbf4b70d0fd30f0ce3cef9c7e6bdb..448888b0ad76def59c0dd27b637564dd7adb1127 100644 (file)
@@ -4024,8 +4024,16 @@ Type_conversion_expression::do_string_constant_value(std::string* val) const
          unsigned long ival;
          if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID)
            {
+             unsigned int cval = static_cast<unsigned int>(ival);
+             if (static_cast<unsigned long>(cval) != ival)
+               {
+                 go_warning_at(this->location(), 0,
+                               "unicode code point 0x%lx out of range",
+                               ival);
+                 cval = 0xfffd; // Unicode "replacement character."
+               }
              val->clear();
-             Lex::append_char(ival, true, val, this->location());
+             Lex::append_char(cval, true, val, this->location());
              return true;
            }
        }