Skip to content

Commit 0046ed4

Browse files
committed
Fix an edge case in wasmfs_fetch for reads far past end of file
Use the minimum of the requested last chunk and the actual last chunk when accessing chunked data
1 parent 44544ea commit 0046ed4

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/lib/libwasmfs_fetch.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ addToLibrary({
4949
fileInfo.headers.has('Content-Length') &&
5050
fileInfo.headers.get('Accept-Ranges') == 'bytes' &&
5151
(parseInt(fileInfo.headers.get('Content-Length'), 10) > chunkSize*2)) {
52+
var size = parseInt(fileInfo.headers.get('Content-Length'), 10);
5253
wasmFS$JSMemoryRanges[file] = {
53-
size: parseInt(fileInfo.headers.get('Content-Length'), 10),
54+
size,
5455
chunks: [],
5556
chunkSize: chunkSize
5657
};
58+
firstChunk = 0;
59+
lastChunk = Math.min(lastChunk, ((size-1) / chunkSize));
5760
} else {
5861
// may as well/forced to download the whole file
5962
var wholeFileReq = await fetch(url);
@@ -133,7 +136,9 @@ addToLibrary({
133136
var chunkSize = fileInfo.chunkSize;
134137
var firstChunk = (offset / chunkSize) | 0;
135138
// See comments in getFileRange.
136-
var lastChunk = ((offset+length-1) / chunkSize) | 0;
139+
var lastChunk = Math.min(
140+
((offset+length-1) / chunkSize),
141+
((fileInfo.size-1) / chunkSize)) | 0;
137142
var readLength = 0;
138143
for (var i = firstChunk; i <= lastChunk; i++) {
139144
var chunk = chunks[i];

0 commit comments

Comments
 (0)