diff options
author | Michael J. Bishop <mbtyke@gmail.com> | 2013-09-17 13:08:02 -0400 |
---|---|---|
committer | Michael J. Bishop <mbtyke@gmail.com> | 2013-09-17 13:08:02 -0400 |
commit | 8c867e3a4646dadf6922907543103ccf9a11fd75 (patch) | |
tree | ae87d513826db36774618fc9c7c7833f2893c60c | |
parent | c72022ee3788281dd764782eb51395e322efb1bc (diff) |
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
-rw-r--r-- | src/library_fs.js | 3 | ||||
-rw-r--r-- | src/library_memfs.js | 3 |
2 files changed, 6 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); |