From: Ian Lance Taylor <iant@golang.org>
To: gcc-patches <gcc-patches@gcc.gnu.org>,
"gofrontend-dev@googlegroups.com"
<gofrontend-dev@googlegroups.com>
Subject: Go patch committed: handle out of range constant integer to string conversion
Date: Wed, 02 Sep 2015 00:46:00 -0000 [thread overview]
Message-ID: <CAOyqgcV7653bbCv8QkWXMK0xZcj6_FHP-7poKmD130VTyuD+1w@mail.gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 287 bytes --]
This patch by Chris Manghane fixes the Go compiler to correctly handle
an integer to string conversion when the integer is an out of range
constant. This fixes https://golang.org/issue/11525 . Bootstrapped
and ran Go testsuite on x86_64-unknown-linux-gnu. Committed to
mainline.
Ian
[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 1708 bytes --]
Index: gcc/go/gofrontend/MERGE
===================================================================
--- gcc/go/gofrontend/MERGE (revision 227332)
+++ gcc/go/gofrontend/MERGE (working copy)
@@ -1,4 +1,4 @@
-65672c16004c6d6d0247b6691881d282ffca89e3
+a63e173b20baa1a48470dd31a1fb1f2704b37011
The first line of this file holds the git revision number of the last
merge done from the gofrontend repository.
Index: gcc/go/gofrontend/expressions.cc
===================================================================
--- gcc/go/gofrontend/expressions.cc (revision 227299)
+++ gcc/go/gofrontend/expressions.cc (working copy)
@@ -3039,6 +3039,25 @@ Type_conversion_expression::do_lower(Gog
}
}
+ // According to the language specification on string conversions
+ // (http://golang.org/ref/spec#Conversions_to_and_from_a_string_type):
+ // When converting an integer into a string, the string will be a UTF-8
+ // representation of the integer and integers "outside the range of valid
+ // Unicode code points are converted to '\uFFFD'."
+ if (type->is_string_type())
+ {
+ Numeric_constant nc;
+ if (val->numeric_constant_value(&nc) && nc.is_int())
+ {
+ // An integer value doesn't fit in the Unicode code point range if it
+ // overflows the Go "int" type or is negative.
+ unsigned long ul;
+ if (!nc.set_type(Type::lookup_integer_type("int"), false, location)
+ || nc.to_unsigned_long(&ul) == Numeric_constant::NC_UL_NEGATIVE)
+ return Expression::make_string("\ufffd", location);
+ }
+ }
+
if (type->is_slice_type())
{
Type* element_type = type->array_type()->element_type()->forwarded();
reply other threads:[~2015-09-02 0:46 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAOyqgcV7653bbCv8QkWXMK0xZcj6_FHP-7poKmD130VTyuD+1w@mail.gmail.com \
--to=iant@golang.org \
--cc=gcc-patches@gcc.gnu.org \
--cc=gofrontend-dev@googlegroups.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).