From 3c5700c255f4603b5df9c6d183d13dd71a083cc3 Mon Sep 17 00:00:00 2001 From: m-holger Date: Sat, 27 May 2023 18:19:52 +0100 Subject: Code tidy - reflow comments and strings --- libqpdf/InputSource.cc | 74 ++++++++++++++++++++------------------------------ 1 file changed, 30 insertions(+), 44 deletions(-) (limited to 'libqpdf/InputSource.cc') diff --git a/libqpdf/InputSource.cc b/libqpdf/InputSource.cc index 597781fe..f5dc7706 100644 --- a/libqpdf/InputSource.cc +++ b/libqpdf/InputSource.cc @@ -20,12 +20,10 @@ InputSource::getLastOffset() const std::string InputSource::readLine(size_t max_line_length) { - // Return at most max_line_length characters from the next line. - // Lines are terminated by one or more \r or \n characters. - // Consume the trailing newline characters but don't return them. - // After this is called, the file will be positioned after a line - // terminator or at the end of the file, and last_offset will - // point to position the file had when this method was called. + // Return at most max_line_length characters from the next line. Lines are terminated by one or + // more \r or \n characters. Consume the trailing newline characters but don't return them. + // After this is called, the file will be positioned after a line terminator or at the end of + // the file, and last_offset will point to position the file had when this method was called. qpdf_offset_t offset = this->tell(); auto bp = std::make_unique(max_line_length + 1); @@ -45,22 +43,18 @@ InputSource::readLine(size_t max_line_length) bool InputSource::findFirst(char const* start_chars, qpdf_offset_t offset, size_t len, Finder& finder) { - // Basic approach: search for the first character of start_chars - // starting from offset but not going past len (if len != 0). Once - // the first character is found, see if it is the beginning of a - // sequence of characters matching start_chars. If so, call - // finder.check() to do caller-specific additional checks. If not, - // keep searching. + // Basic approach: search for the first character of start_chars starting from offset but not + // going past len (if len != 0). Once the first character is found, see if it is the beginning + // of a sequence of characters matching start_chars. If so, call finder.check() to do + // caller-specific additional checks. If not, keep searching. - // This code is tricky and highly subject to off-by-one or other - // edge case logic errors. See comments throughout that explain - // how we're not missing any edge cases. There are also tests - // specifically constructed to make sure we caught the edge cases - // in testing. + // This code is tricky and highly subject to off-by-one or other edge case logic errors. See + // comments throughout that explain how we're not missing any edge cases. There are also tests + // specifically constructed to make sure we caught the edge cases in testing. char buf[1025]; // size known to input_source.cc in libtests - // To enable us to guarantee null-termination, save an extra byte - // so that buf[size] is valid memory. + // To enable us to guarantee null-termination, save an extra byte so that buf[size] is valid + // memory. size_t size = sizeof(buf) - 1; if ((strlen(start_chars) < 1) || (strlen(start_chars) > size)) { throw std::logic_error("InputSource::findSource called with" @@ -71,19 +65,15 @@ InputSource::findFirst(char const* start_chars, qpdf_offset_t offset, size_t len qpdf_offset_t buf_offset = offset; size_t bytes_read = 0; - // Guarantee that we return from this loop. Each time through, we - // either return, advance p, or restart the loop with a condition - // that will cause return on the next pass. Eventually we will - // either be out of range or hit EOF, either of which forces us to - // return. + // Guarantee that we return from this loop. Each time through, we either return, advance p, or + // restart the loop with a condition that will cause return on the next pass. Eventually we will + // either be out of range or hit EOF, either of which forces us to return. while (true) { - // Do we need to read more data? Pretend size = 5, buf starts - // at 0, and start_chars has 3 characters. buf[5] is valid and - // null. If p == 2, start_chars could be buf[2] through - // buf[4], so p + strlen(start_chars) == buf + size is okay. - // If p points to buf[size], since strlen(start_chars) is - // always >= 1, this overflow test will be correct for that - // case regardless of start_chars. + // Do we need to read more data? Pretend size = 5, buf starts at 0, and start_chars has 3 + // characters. buf[5] is valid and null. If p == 2, start_chars could be buf[2] through + // buf[4], so p + strlen(start_chars) == buf + size is okay. If p points to buf[size], since + // strlen(start_chars) is always >= 1, this overflow test will be correct for that case + // regardless of start_chars. if ((p == nullptr) || ((p + strlen(start_chars)) > (buf + bytes_read))) { if (p) { QTC::TC( @@ -91,9 +81,8 @@ InputSource::findFirst(char const* start_chars, qpdf_offset_t offset, size_t len buf_offset += (p - buf); } this->seek(buf_offset, SEEK_SET); - // Read into buffer and zero out the rest of the buffer - // including buf[size]. We allocated an extra byte so that - // we could guarantee null termination as an extra + // Read into buffer and zero out the rest of the buffer including buf[size]. We + // allocated an extra byte so that we could guarantee null termination as an extra // protection against overrun when using string functions. bytes_read = this->read(buf, size); if (bytes_read < strlen(start_chars)) { @@ -122,19 +111,16 @@ InputSource::findFirst(char const* start_chars, qpdf_offset_t offset, size_t len } } if ((p + strlen(start_chars)) > (buf + bytes_read)) { - // If there are not enough bytes left in the file for - // start_chars, we will detect this on the next pass - // as EOF and return. + // If there are not enough bytes left in the file for start_chars, we will detect + // this on the next pass as EOF and return. QTC::TC("libtests", "InputSource not enough bytes"); continue; } - // See if p points to a sequence matching start_chars. We - // already checked above to make sure we are not going to - // overrun memory. + // See if p points to a sequence matching start_chars. We already checked above to make + // sure we are not going to overrun memory. if (strncmp(p, start_chars, strlen(start_chars)) == 0) { - // Call finder.check() with the input source - // positioned to the point of the match. + // Call finder.check() with the input source positioned to the point of the match. this->seek(buf_offset + (p - buf), SEEK_SET); if (finder.check()) { return true; @@ -144,8 +130,8 @@ InputSource::findFirst(char const* start_chars, qpdf_offset_t offset, size_t len } else { QTC::TC("libtests", "InputSource first char matched but not string"); } - // This occurrence of the first character wasn't a match. - // Skip over it and keep searching. + // This occurrence of the first character wasn't a match. Skip over it and keep + // searching. ++p; } else { // Trigger reading the next block -- cgit v1.2.3-70-g09d2