Browse Source

flt: fix crash due to macOS libc++ bug in istream::eof()

Apparently eof() in older versions of libc++ returns true if the last character has just been read, whereas the proper behavior is only to return true when attempting to read past the end of the stream.  Since failbit is reliably set in all cases when reading past the length of the stream, we should only trust the result of eof() if fail() also returns true.
rdb 7 years ago
parent
commit
3e5f2cf672
1 changed files with 17 additions and 17 deletions
  1. 17 17
      pandatool/src/flt/fltRecordReader.cxx

+ 17 - 17
pandatool/src/flt/fltRecordReader.cxx

@@ -139,13 +139,13 @@ advance(bool ok_eof) {
     _datagram = Datagram();
     _datagram = Datagram();
   }
   }
 
 
-  if (_in.eof()) {
-    _state = S_eof;
-    assert(!flt_error_abort);
-    return FE_end_of_file;
-  }
-
   if (_in.fail()) {
   if (_in.fail()) {
+    if (_in.eof()) {
+      _state = S_eof;
+      assert(!flt_error_abort);
+      return FE_end_of_file;
+    }
+
     _state = S_error;
     _state = S_error;
     assert(!flt_error_abort);
     assert(!flt_error_abort);
     return FE_read_error;
     return FE_read_error;
@@ -170,13 +170,13 @@ advance(bool ok_eof) {
       delete[] buffer;
       delete[] buffer;
     }
     }
 
 
-    if (_in.eof()) {
-      _state = S_eof;
-      assert(!flt_error_abort);
-      return FE_end_of_file;
-    }
-
     if (_in.fail()) {
     if (_in.fail()) {
+      if (_in.eof()) {
+        _state = S_eof;
+        assert(!flt_error_abort);
+        return FE_end_of_file;
+      }
+
       _state = S_error;
       _state = S_error;
       assert(!flt_error_abort);
       assert(!flt_error_abort);
       return FE_read_error;
       return FE_read_error;
@@ -222,11 +222,11 @@ read_next_header() {
   char bytes[header_size];
   char bytes[header_size];
   _in.read(bytes, header_size);
   _in.read(bytes, header_size);
 
 
-  if (_in.eof()) {
-    _next_error = FE_end_of_file;
-    return;
-
-  } else if (_in.fail()) {
+  if (_in.fail()) {
+    if (_in.eof()) {
+      _next_error = FE_end_of_file;
+      return;
+    }
     _next_error = FE_read_error;
     _next_error = FE_read_error;
     return;
     return;
   }
   }