From e60997fde46c8e73cdbab114eef41ad537fcdc6f Mon Sep 17 00:00:00 2001 From: Gavin Halliday Date: Tue, 3 Dec 2024 12:41:06 +0000 Subject: [PATCH] HPCC-33065 Improve the error message when failing to read an index header/footer Signed-off-by: Gavin Halliday --- system/jhtree/jhtree.cpp | 12 ++++++++---- system/jlib/jio.cpp | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/system/jhtree/jhtree.cpp b/system/jhtree/jhtree.cpp index 8148c37477c..802798357e5 100644 --- a/system/jhtree/jhtree.cpp +++ b/system/jhtree/jhtree.cpp @@ -1222,8 +1222,9 @@ CDiskKeyIndex::CDiskKeyIndex(unsigned _iD, IFileIO *_io, const char *_name, bool blockedIOSize = _blockedIOSize; io.setown(_io); KeyHdr hdr; - if (io->read(0, sizeof(hdr), &hdr) != sizeof(hdr)) - throw MakeStringException(0, "Failed to read key header: file too small, could not read %u bytes", (unsigned) sizeof(hdr)); + offset_t sizeRead = io->read(0, sizeof(hdr), &hdr); + if (sizeRead != sizeof(hdr)) + throw MakeStringException(0, "Failed to read key header: file too small, could not read %u bytes - read %u", (unsigned) sizeof(hdr), (unsigned)sizeRead); #ifdef _DEBUG //In debug mode always use the trailing header if it is available to ensure that code path is tested @@ -1233,8 +1234,11 @@ CDiskKeyIndex::CDiskKeyIndex(unsigned _iD, IFileIO *_io, const char *_name, bool #endif { _WINREV(hdr.nodeSize); - if (!io->read(io->size() - hdr.nodeSize, sizeof(hdr), &hdr)) - throw MakeStringException(4, "Invalid key %s: failed to read trailing key header", _name); + offset_t actualSize = io->size(); + offset_t readOffset = actualSize - hdr.nodeSize; + sizeRead = io->read(readOffset, sizeof(hdr), &hdr); + if (sizeRead != sizeof(hdr)) + throw MakeStringException(4, "Invalid key %s: failed to read trailing key header at offset %llu, read %u", _name, readOffset, (unsigned)sizeRead); } init(hdr, isTLK); } diff --git a/system/jlib/jio.cpp b/system/jlib/jio.cpp index e193660f1d5..67dc8785fe9 100644 --- a/system/jlib/jio.cpp +++ b/system/jlib/jio.cpp @@ -160,7 +160,7 @@ extern jlib_decl size32_t checked_pread(const char * filename, int file, void *b readNow = 0; break; } - throw makeErrnoExceptionV(errno, "checked_pread for file '%s'", filename); + throw makeErrnoExceptionV(errno, "checked_pread for file '%s' @%lld", filename, pos); } } else if (!readNow)