From 84a13151049b6acc74e7ecfbe55ae48631378ac1 Mon Sep 17 00:00:00 2001 From: rdb Date: Thu, 10 Jan 2019 21:41:18 +0100 Subject: [PATCH] More fixes to work around broken libc++ eof() in Mac OS X 10.7 --- dtool/src/prc/streamReader.cxx | 19 +++++++++++++++---- dtool/src/prc/streamReader_ext.cxx | 4 ++-- panda/src/downloader/decompressor.cxx | 4 ++-- panda/src/downloader/documentSpec.cxx | 4 ++-- panda/src/downloader/httpChannel.cxx | 4 ++-- panda/src/downloader/httpDate.cxx | 2 +- panda/src/downloader/socketStream.cxx | 2 +- panda/src/express/hashVal.cxx | 4 ++-- panda/src/express/make_ca_bundle.cxx | 2 +- panda/src/express/multifile.cxx | 9 ++++----- panda/src/gobj/texture.cxx | 4 ++-- panda/src/pnmimage/pnmimage_base.cxx | 8 ++++---- pandatool/src/miscprogs/binToC.cxx | 2 +- 13 files changed, 39 insertions(+), 29 deletions(-) diff --git a/dtool/src/prc/streamReader.cxx b/dtool/src/prc/streamReader.cxx index 74f278e220..3286eca67a 100644 --- a/dtool/src/prc/streamReader.cxx +++ b/dtool/src/prc/streamReader.cxx @@ -26,6 +26,9 @@ get_string() { // First, get the length of the string size_t size = get_uint16(); + if (size == 0) { + return string(); + } char *buffer = (char *)alloca(size); _in->read(buffer, size); @@ -42,6 +45,9 @@ get_string32() { // First, get the length of the string size_t size = get_uint32(); + if (size == 0) { + return string(); + } char *buffer = (char *)PANDA_MALLOC_ARRAY(size); _in->read(buffer, size); @@ -60,7 +66,7 @@ get_z_string() { string result; int ch = _in->get(); - while (!_in->eof() && !_in->fail() && ch != '\0') { + while (!_in->fail() && ch != EOF && ch != '\0') { result += (char)ch; ch = _in->get(); } @@ -76,6 +82,10 @@ string StreamReader:: get_fixed_string(size_t size) { nassertr(!_in->eof() && !_in->fail(), string()); + if (size == 0) { + return string(); + } + char *buffer = (char *)alloca(size); _in->read(buffer, size); size_t read_bytes = _in->gcount(); @@ -90,8 +100,9 @@ get_fixed_string(size_t size) { */ void StreamReader:: skip_bytes(size_t size) { - nassertv(!_in->eof() && !_in->fail()); + nassertv(!_in->fail()); nassertv((int)size >= 0); + nassertv(size == 0 || !_in->eof()); while (size > 0) { _in->get(); @@ -145,9 +156,9 @@ string StreamReader:: readline() { string line; int ch = _in->get(); - while (!_in->eof() && !_in->fail()) { + while (ch != EOF && !_in->fail()) { line += (char)ch; - if (ch == '\n') { + if (ch == '\n' || _in->eof()) { // Here's the newline character. return line; } diff --git a/dtool/src/prc/streamReader_ext.cxx b/dtool/src/prc/streamReader_ext.cxx index 94c346e370..8e1b2b42ec 100644 --- a/dtool/src/prc/streamReader_ext.cxx +++ b/dtool/src/prc/streamReader_ext.cxx @@ -45,9 +45,9 @@ readline() { std::string line; int ch = in->get(); - while (!in->eof() && !in->fail()) { + while (ch != EOF && !in->fail()) { line += ch; - if (ch == '\n') { + if (ch == '\n' || in->eof()) { // Here's the newline character. break; } diff --git a/panda/src/downloader/decompressor.cxx b/panda/src/downloader/decompressor.cxx index 272279047d..bc4822ee91 100644 --- a/panda/src/downloader/decompressor.cxx +++ b/panda/src/downloader/decompressor.cxx @@ -183,7 +183,7 @@ decompress(const Filename &source_file) { return false; int ch = _decompress->get(); - while (!_decompress->eof() && !_decompress->fail()) { + while (ch != EOF && !_decompress->fail()) { _dest->put(ch); ch = _decompress->get(); } @@ -207,7 +207,7 @@ decompress(Ramfile &source_and_dest_file) { IDecompressStream decompress(&source, false); int ch = decompress.get(); - while (!decompress.eof() && !decompress.fail()) { + while (ch != EOF && !decompress.fail()) { dest.put(ch); ch = decompress.get(); } diff --git a/panda/src/downloader/documentSpec.cxx b/panda/src/downloader/documentSpec.cxx index 6ca2a9b260..34bfe3808d 100644 --- a/panda/src/downloader/documentSpec.cxx +++ b/panda/src/downloader/documentSpec.cxx @@ -66,7 +66,7 @@ input(std::istream &in) { // Scan the tag, up to but not including the closing paren. std::string tag; in >> ch; - while (!in.fail() && !in.eof() && ch != ')') { + while (!in.fail() && ch != EOF && ch != ')') { tag += ch; // We want to include embedded whitespace, so we use get(). ch = in.get(); @@ -81,7 +81,7 @@ input(std::istream &in) { // Scan the date, up to but not including the closing bracket. if (ch != ']') { std::string date; - while (!in.fail() && !in.eof() && ch != ']') { + while (!in.fail() && ch != EOF && ch != ']') { date += ch; ch = in.get(); } diff --git a/panda/src/downloader/httpChannel.cxx b/panda/src/downloader/httpChannel.cxx index 17c12d806a..365c819b0f 100644 --- a/panda/src/downloader/httpChannel.cxx +++ b/panda/src/downloader/httpChannel.cxx @@ -2762,7 +2762,7 @@ bool HTTPChannel:: server_getline(string &str) { nassertr(!_source.is_null(), false); int ch = (*_source)->get(); - while (!(*_source)->eof() && !(*_source)->fail()) { + while (ch != EOF && !(*_source)->fail()) { switch (ch) { case '\n': // end-of-line character, we're done. @@ -2850,7 +2850,7 @@ bool HTTPChannel:: server_get(string &str, size_t num_bytes) { nassertr(!_source.is_null(), false); int ch = (*_source)->get(); - while (!(*_source)->eof() && !(*_source)->fail()) { + while (ch != EOF && !(*_source)->fail()) { _working_get += (char)ch; if (_working_get.length() >= num_bytes) { str = _working_get; diff --git a/panda/src/downloader/httpDate.cxx b/panda/src/downloader/httpDate.cxx index a651b50f89..e49769257f 100644 --- a/panda/src/downloader/httpDate.cxx +++ b/panda/src/downloader/httpDate.cxx @@ -279,7 +279,7 @@ input(std::istream &in) { string date; ch = in.get(); - while (!in.fail() && !in.eof() && ch != '"') { + while (!in.fail() && ch != EOF && ch != '"') { date += ch; ch = in.get(); } diff --git a/panda/src/downloader/socketStream.cxx b/panda/src/downloader/socketStream.cxx index eec65d1857..cd18e7f972 100644 --- a/panda/src/downloader/socketStream.cxx +++ b/panda/src/downloader/socketStream.cxx @@ -56,7 +56,7 @@ do_receive_datagram(Datagram &dg) { // Read the first two bytes: the datagram length. while ((int)_data_so_far.size() < _tcp_header_size) { int ch = _istream->get(); - if (_istream->eof() || _istream->fail()) { + if (ch == EOF || _istream->fail()) { _istream->clear(); return false; } diff --git a/panda/src/express/hashVal.cxx b/panda/src/express/hashVal.cxx index 5d74f1dac4..479f91f752 100644 --- a/panda/src/express/hashVal.cxx +++ b/panda/src/express/hashVal.cxx @@ -50,7 +50,7 @@ input_hex(istream &in) { size_t i = 0; int ch = in.get(); - while (!in.eof() && !in.fail() && isxdigit(ch)) { + while (ch != EOF && !in.fail() && isxdigit(ch)) { if (i < 32) { buffer[i] = (char)ch; } @@ -63,7 +63,7 @@ input_hex(istream &in) { return; } - if (!in.eof()) { + if (ch != EOF) { in.putback((char)ch); } else { in.clear(); diff --git a/panda/src/express/make_ca_bundle.cxx b/panda/src/express/make_ca_bundle.cxx index 0773bb88ec..2a9712b3ce 100644 --- a/panda/src/express/make_ca_bundle.cxx +++ b/panda/src/express/make_ca_bundle.cxx @@ -108,7 +108,7 @@ main(int argc, char *argv[]) { int col = 0; unsigned int ch; ch = in.get(); - while (!in.fail() && !in.eof()) { + while (!in.fail() && ch != EOF) { if (col == 0) { out << "\n "; } else if (col == col_width) { diff --git a/panda/src/express/multifile.cxx b/panda/src/express/multifile.cxx index cdb1c56ec8..4467132b79 100644 --- a/panda/src/express/multifile.cxx +++ b/panda/src/express/multifile.cxx @@ -1785,8 +1785,7 @@ compare_subfile(int index, const Filename &filename) { in2.seekg(0); int byte1 = in1->get(); int byte2 = in2.get(); - while (!in1->fail() && !in1->eof() && - !in2.fail() && !in2.eof()) { + while (!in1->fail() && !in2.fail()) { if (byte1 != byte2) { close_read_subfile(in1); return false; @@ -2497,7 +2496,7 @@ read_index(istream &read, streampos fpos, Multifile *multifile) { StreamReader reader(read); streampos next_index = multifile->word_to_streampos(reader.get_uint32()); - if (read.eof() || read.fail()) { + if (read.fail()) { _flags |= SF_index_invalid; return 0; } @@ -2529,7 +2528,7 @@ read_index(istream &read, streampos fpos, Multifile *multifile) { } size_t name_length = reader.get_uint16(); - if (read.eof() || read.fail()) { + if (read.fail()) { _flags |= SF_index_invalid; return 0; } @@ -2543,7 +2542,7 @@ read_index(istream &read, streampos fpos, Multifile *multifile) { _name = string(name_buffer, name_length); PANDA_FREE_ARRAY(name_buffer); - if (read.eof() || read.fail()) { + if (read.fail()) { _flags |= SF_index_invalid; return 0; } diff --git a/panda/src/gobj/texture.cxx b/panda/src/gobj/texture.cxx index 1ba5b891e3..d28e2d208b 100644 --- a/panda/src/gobj/texture.cxx +++ b/panda/src/gobj/texture.cxx @@ -4158,7 +4158,7 @@ do_read_dds(CData *cdata, istream &in, const string &filename, bool header_only) cdata->_num_mipmap_levels_read = cdata->_ram_images.size(); } - if (in.fail() || in.eof()) { + if (in.fail()) { gobj_cat.error() << filename << ": truncated DDS file.\n"; return false; @@ -4924,7 +4924,7 @@ do_read_ktx(CData *cdata, istream &in, const string &filename, bool header_only) } } - if (in.fail() || in.eof()) { + if (in.fail()) { gobj_cat.error() << filename << ": truncated KTX file.\n"; return false; diff --git a/panda/src/pnmimage/pnmimage_base.cxx b/panda/src/pnmimage/pnmimage_base.cxx index 0fb9bedfc6..d5ef09e415 100644 --- a/panda/src/pnmimage/pnmimage_base.cxx +++ b/panda/src/pnmimage/pnmimage_base.cxx @@ -120,7 +120,7 @@ int pm_readbigshort(istream *in, short *sP) { StreamReader reader(in, false); *sP = reader.get_be_int16(); - return (!in->eof() && !in->fail()) ? 0 : -1; + return (!in->fail()) ? 0 : -1; } int @@ -134,7 +134,7 @@ int pm_readbiglong(istream *in, long *lP) { StreamReader reader(in, false); *lP = reader.get_be_int32(); - return (!in->eof() && !in->fail()) ? 0 : -1; + return (!in->fail()) ? 0 : -1; } int @@ -148,7 +148,7 @@ int pm_readlittleshort(istream *in, short *sP) { StreamReader reader(in, false); *sP = reader.get_int16(); - return (!in->eof() && !in->fail()) ? 0 : -1; + return (!in->fail()) ? 0 : -1; } int @@ -162,7 +162,7 @@ int pm_readlittlelong(istream *in, long *lP) { StreamReader reader(in, false); *lP = reader.get_int32(); - return (!in->eof() && !in->fail()) ? 0 : -1; + return (!in->fail()) ? 0 : -1; } int diff --git a/pandatool/src/miscprogs/binToC.cxx b/pandatool/src/miscprogs/binToC.cxx index f963c08986..6eff47c9d1 100644 --- a/pandatool/src/miscprogs/binToC.cxx +++ b/pandatool/src/miscprogs/binToC.cxx @@ -101,7 +101,7 @@ run() { int col = 0; unsigned int ch; ch = in.get(); - while (!in.fail() && !in.eof()) { + while (!in.fail() && ch != EOF) { if (col == 0) { out << "\n "; } else if (col == col_width) {