From 5d3de03be532ae60e82fa3d059683c2e082c7af0 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sun, 19 May 2013 10:55:14 +0200 Subject: [PATCH] libroot_build: Fix fs_read_attr() in fs_attr_untyped.cpp * With a specified buffer size smaller than the attribute size the function would fail with ERANGE on Linux although it should just read as much as possible. Now we always read into our temporary data buffer with the full buffer size. * Fix return value in case pos is > 0. pos must be subtracted from the bytes actually read. --- src/build/libroot/fs_attr_untyped.cpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/build/libroot/fs_attr_untyped.cpp b/src/build/libroot/fs_attr_untyped.cpp index 02ea2af095..aeffdca36e 100644 --- a/src/build/libroot/fs_attr_untyped.cpp +++ b/src/build/libroot/fs_attr_untyped.cpp @@ -523,8 +523,7 @@ fs_read_attr(int fd, const char *_attribute, uint32 type, off_t pos, // read the attribute char attributeBuffer[sizeof(AttributeHeader) + kMaxAttributeLength]; - ssize_t bytesRead = min_c((size_t)kMaxAttributeLength, readBytes) - + sizeof(AttributeHeader); + ssize_t bytesRead = sizeof(attributeBuffer); if (localFD.Path()) { bytesRead = get_attribute(-1, localFD.Path(), attribute.c_str(), attributeBuffer, bytesRead); @@ -549,12 +548,19 @@ fs_read_attr(int fd, const char *_attribute, uint32 type, off_t pos, } // copy the result into the provided buffer - bytesRead -= sizeof(AttributeHeader); - if (bytesRead > pos) { + bytesRead -= sizeof(AttributeHeader) + pos; + if (bytesRead < 0) { + // that means pos > + errno = B_BAD_VALUE; + return -1; + } + + if (bytesRead > 0) { + if ((size_t)bytesRead > readBytes) + bytesRead = readBytes; memcpy(buffer, attributeBuffer + sizeof(AttributeHeader) + pos, - bytesRead - pos); - } else - bytesRead = 0; + bytesRead); + } return bytesRead; }