gofrontend: correct file reading logic in Stream_from_file
The implementation of Stream_from_file mishandled several cases:
* It reversed the check for whether bytes were already available in
the peek buffer.
* It considered positive return values from lseek to be an error, when
only a -1 return value indicates an error.
Change-Id: Ic699d3ff7fe589f5ca2362b3db2971a6b9dc9022
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/259437
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Trust: Cherry Zhang <cherryyz@google.com>
diff --git a/go/import.cc b/go/import.cc
index c63ae24..081afef 100644
--- a/go/import.cc
+++ b/go/import.cc
@@ -1487,7 +1487,7 @@
bool
Stream_from_file::do_peek(size_t length, const char** bytes)
{
- if (this->data_.length() <= length)
+ if (this->data_.length() >= length)
{
*bytes = this->data_.data();
return true;
@@ -1504,7 +1504,7 @@
return false;
}
- if (lseek(this->fd_, - got, SEEK_CUR) != 0)
+ if (lseek(this->fd_, - got, SEEK_CUR) < 0)
{
if (!this->saw_error())
go_fatal_error(Linemap::unknown_location(), "lseek failed: %m");
@@ -1524,7 +1524,7 @@
void
Stream_from_file::do_advance(size_t skip)
{
- if (lseek(this->fd_, skip, SEEK_CUR) != 0)
+ if (lseek(this->fd_, skip, SEEK_CUR) < 0)
{
if (!this->saw_error())
go_fatal_error(Linemap::unknown_location(), "lseek failed: %m");
@@ -1532,7 +1532,7 @@
}
if (!this->data_.empty())
{
- if (this->data_.length() < skip)
+ if (this->data_.length() > skip)
this->data_.erase(0, skip);
else
this->data_.clear();