From 8c867e3a4646dadf6922907543103ccf9a11fd75 Mon Sep 17 00:00:00 2001 From: "Michael J. Bishop" Date: Tue, 17 Sep 2013 13:08:02 -0400 Subject: Fixes in pread() The bug occurs when pread() doesn't return 0 when asked to read an offset beyond its buffer. This behavior is explicitly documented at: http://pubs.opengroup.org/onlinepubs/000095399/functions/read.html > If the starting position is at or after the end-of-file, 0 > shall be returned --- src/library_fs.js | 3 +++ src/library_memfs.js | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/library_fs.js b/src/library_fs.js index 8bf0a83a..84a5245b 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -1367,7 +1367,10 @@ mergeInto(LibraryManager.library, { throw new FS.ErrnoError(ERRNO_CODES.EIO); } var contents = stream.node.contents; + if (position >= contents.length) + return 0; var size = Math.min(contents.length - position, length); + assert(size >= 0); if (contents.slice) { // normal array for (var i = 0; i < size; i++) { buffer[offset + i] = contents[position + i]; diff --git a/src/library_memfs.js b/src/library_memfs.js index 354f5e95..28178d9f 100644 --- a/src/library_memfs.js +++ b/src/library_memfs.js @@ -177,7 +177,10 @@ mergeInto(LibraryManager.library, { stream_ops: { read: function(stream, buffer, offset, length, position) { var contents = stream.node.contents; + if (position >= contents.length) + return 0; var size = Math.min(contents.length - position, length); + assert(size >= 0); #if USE_TYPED_ARRAYS == 2 if (size > 8 && contents.subarray) { // non-trivial, and typed array buffer.set(contents.subarray(position, position + size), offset); -- cgit v1.2.3-18-g5258 From 2ed2fb5df392251a0957543b920272b9b2ae7908 Mon Sep 17 00:00:00 2001 From: "Michael J. Bishop" Date: Wed, 18 Sep 2013 10:59:04 -0400 Subject: Added to the test_files test to ensure the pread() functionality. --- tests/files.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/files.cpp b/tests/files.cpp index 176cdb62..5d158ba9 100644 --- a/tests/files.cpp +++ b/tests/files.cpp @@ -2,6 +2,7 @@ #include #include #include +#include int main() { @@ -61,7 +62,14 @@ int main() char data2[10]; FILE *inf = fopen("go.out", "rb"); - int num = fread(data2, 1, 10, inf); + + // make sure pread returns 0 if we read starting at the end (or past the end) of the file + int num = pread(fileno(inf), data2, 10, 5); + assert(num == 0); + num = pread(fileno(inf), data2, 10, sizeof(data)*8); + assert(num == 0); + + num = fread(data2, 1, 10, inf); fclose(inf); printf("%d : %d,%d,%d,%d,%d\n", num, data2[0], data2[1], data2[2], data2[3], data2[4]); -- cgit v1.2.3-18-g5258 From ee9d51029780b6542f3b27ab7071a90e8b9a2577 Mon Sep 17 00:00:00 2001 From: "Michael J. Bishop" Date: Wed, 18 Sep 2013 15:43:17 -0400 Subject: Removed old test case and put a test case in a more appropriate area. tests/unistd/io.* --- Revert "Added to the test_files test to ensure the pread() functionality." This reverts commit 2ed2fb5df392251a0957543b920272b9b2ae7908. --- tests/files.cpp | 10 +--------- tests/unistd/io.c | 6 ++++++ tests/unistd/io.out | 4 ++++ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/tests/files.cpp b/tests/files.cpp index 5d158ba9..176cdb62 100644 --- a/tests/files.cpp +++ b/tests/files.cpp @@ -2,7 +2,6 @@ #include #include #include -#include int main() { @@ -62,14 +61,7 @@ int main() char data2[10]; FILE *inf = fopen("go.out", "rb"); - - // make sure pread returns 0 if we read starting at the end (or past the end) of the file - int num = pread(fileno(inf), data2, 10, 5); - assert(num == 0); - num = pread(fileno(inf), data2, 10, sizeof(data)*8); - assert(num == 0); - - num = fread(data2, 1, 10, inf); + int num = fread(data2, 1, 10, inf); fclose(inf); printf("%d : %d,%d,%d,%d,%d\n", num, data2[0], data2[1], data2[2], data2[3], data2[4]); diff --git a/tests/unistd/io.c b/tests/unistd/io.c index 0ff5f4fb..6bf22593 100644 --- a/tests/unistd/io.c +++ b/tests/unistd/io.c @@ -104,6 +104,12 @@ int main() { printf("errno: %d\n\n", errno); errno = 0; + printf("pread past end of file: %d\n", pread(f, readBuffer, sizeof readBuffer, 99999999999)); + printf("data: %s\n", readBuffer); + memset(readBuffer, 0, sizeof readBuffer); + printf("errno: %d\n\n", errno); + errno = 0; + printf("seek: %d\n", lseek(f, 3, SEEK_SET)); printf("errno: %d\n\n", errno); printf("partial read from file: %d\n", read(f, readBuffer, 3)); diff --git a/tests/unistd/io.out b/tests/unistd/io.out index 037d0c34..c979557e 100644 --- a/tests/unistd/io.out +++ b/tests/unistd/io.out @@ -31,6 +31,10 @@ read from file: 10 data: 1234567890 errno: 0 +pread past end of file: 0 +data: +errno: 0 + seek: 3 errno: 0 -- cgit v1.2.3-18-g5258