compiler: use temporary to avoid early destruction
The code was passing a substr directly to strtol, and then checking
the *end value returned by strtol. But the substr could be destroyed
as soon as strtol returns, making the test of *end invalid.
Also fix an incorrect test of the string index rather than the value.
Fixes https://gcc.gnu.org/PR90110
Change-Id: I020a26249ab00d5828621a0ec120e3f7c790df2e
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/172663
Reviewed-by: Than McIntosh <thanm@google.com>
diff --git a/go/import.cc b/go/import.cc
index d783043..c1982eb 100644
--- a/go/import.cc
+++ b/go/import.cc
@@ -1478,8 +1478,9 @@
this->off_ = i + 1;
char *end;
- long val = strtol(this->body_.substr(start, i - start).c_str(), &end, 10);
- if (*end != '\0' || i > 0x7fffffff)
+ std::string num = this->body_.substr(start, i - start);
+ long val = strtol(num.c_str(), &end, 10);
+ if (*end != '\0' || val > 0x7fffffff)
{
if (!this->saw_error_)
go_error_at(this->location(),