public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Go patch committed: Avoid undefined behavior in Import::read
@ 2020-10-06 20:53 Ian Lance Taylor
  0 siblings, 0 replies; only message in thread
From: Ian Lance Taylor @ 2020-10-06 20:53 UTC (permalink / raw)
  To: gcc-patches, gofrontend-dev

[-- Attachment #1: Type: text/plain, Size: 371 bytes --]

This Go frontend patch by Nikhil Benesch avoids undefined behavior in
Import::read.  For some implementations of Stream, advancing the
stream will invalidate the previously-returned peek buffer.  Copy the
peek buffer before
advancing in Import::read to avoid this undefined behavior.
Bootstrapped and ran Go testsuite on x86_64-pc-linux-gnu.  Committed
to mainline.

Ian

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 2392 bytes --]

1cf34967b5ae0439cd0b9955594aaabddc4c5f82
diff --git a/gcc/go/gofrontend/MERGE b/gcc/go/gofrontend/MERGE
index 701b2d427e3..c5c02aa2392 100644
--- a/gcc/go/gofrontend/MERGE
+++ b/gcc/go/gofrontend/MERGE
@@ -1,4 +1,4 @@
-d00febdab0535546ccbf1ef634be1f23b09c8b77
+613e530547549f4220c4571ea913acbe5fa56f72
 
 The first line of this file holds the git revision number of the last
 merge done from the gofrontend repository.
diff --git a/gcc/go/gofrontend/gogo.cc b/gcc/go/gofrontend/gogo.cc
index aef1c47d26e..f40f13129e6 100644
--- a/gcc/go/gofrontend/gogo.cc
+++ b/gcc/go/gofrontend/gogo.cc
@@ -6212,7 +6212,7 @@ Function::import_func(Import* imp, std::string* pname,
 	  return false;
 	}
 
-      *body = imp->read(static_cast<size_t>(llen));
+      imp->read(static_cast<size_t>(llen), body);
     }
 
   return true;
diff --git a/gcc/go/gofrontend/import.cc b/gcc/go/gofrontend/import.cc
index 081afefa083..c6c1178bc24 100644
--- a/gcc/go/gofrontend/import.cc
+++ b/gcc/go/gofrontend/import.cc
@@ -1375,8 +1375,8 @@ Import::read_name()
 
 // Read LENGTH bytes from the stream.
 
-std::string
-Import::read(size_t length)
+void
+Import::read(size_t length, std::string* out)
 {
   const char* data;
   if (!this->stream_->peek(length, &data))
@@ -1385,10 +1385,11 @@ Import::read(size_t length)
 	go_error_at(this->location_, "import error at %d: expected %d bytes",
 		    this->stream_->pos(), static_cast<int>(length));
       this->stream_->set_saw_error();
-      return "";
+      *out = std::string("");
+      return;
     }
+  *out = std::string(data, length);
   this->advance(length);
-  return std::string(data, length);
 }
 
 // Turn a string into a integer with appropriate error handling.
diff --git a/gcc/go/gofrontend/import.h b/gcc/go/gofrontend/import.h
index b12b3b843df..1d8aae493f2 100644
--- a/gcc/go/gofrontend/import.h
+++ b/gcc/go/gofrontend/import.h
@@ -240,10 +240,10 @@ class Import : public Import_expression
   get_char()
   { return this->stream_->get_char(); }
 
-  // Read LENGTH characters into a string and advance past them.  On
-  // EOF reports an error and returns an empty string.
-  std::string
-  read(size_t length);
+  // Read LENGTH characters into *OUT and advance past them.  On
+  // EOF reports an error and sets *OUT to an empty string.
+  void
+  read(size_t length, std::string* out);
 
   // Return true at the end of the stream.
   bool

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-10-06 20:54 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-06 20:53 Go patch committed: Avoid undefined behavior in Import::read Ian Lance Taylor

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).