aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/library_fs.js3
-rw-r--r--src/library_memfs.js3
-rw-r--r--tests/unistd/io.c6
-rw-r--r--tests/unistd/io.out4
4 files changed, 16 insertions, 0 deletions
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);
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