Skip to content

Commit a524863

Browse files
committed
Fixed a bug in MappedTensor, where consolidated skip-reads would lead to a buffer overrun.
1 parent 81daa1d commit a524863

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

private/mapped_tensor_shim.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,12 @@ void CmdReadChunks(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
363363
for (uChunkElemIndex = 0, vnSubs[1] = 0; uChunkElemIndex < 3; uChunkElemIndex++, vnSubs[1]++) {
364364
dprintf("[%d]", (uint64_t) mfFileChunkIndices[mxCalcSingleSubscript(prhs[2], 2, vnSubs)]);
365365
}
366+
367+
if (uChunkIndex > 20) {
368+
dprintf("...\n");
369+
break;
370+
}
371+
366372
dprintf("\n");
367373
}
368374

@@ -463,7 +469,7 @@ void CmdReadChunks(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
463469
uUniqueDataPtr += nDataElemSize * uChunkSize;
464470

465471
} else if (uChunkSkip < 5) {
466-
dprintf("mts/crc: Consolidated skip-read: read [%ld] bytes per element, skip [%ld] bytes.\n", nDataElemSize, nDataElemSize * (uChunkSkip-1));
472+
dprintf("mts/crc: Consolidated skip-read: read [%ld] elements, [%ld] bytes per element, skip [%ld] bytes.\n", uChunkSize, nDataElemSize, nDataElemSize * (uChunkSkip-1));
467473

468474
if ((vuConsolidatedData = (uint8_t *) malloc(nDataElemSize * uChunkSize * uChunkSkip)) == NULL) {
469475
errprintf("MappedTensor:mapped_tensor_shim:Memory",
@@ -505,12 +511,12 @@ void CmdReadChunks(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
505511
free(vuConsolidatedData);
506512

507513
/* - Increment data pointer */
508-
uUniqueDataPtr = uUniqueDataPtr + uChunkSize * nDataElemSize * (uChunkSkip-1);
514+
uUniqueDataPtr = uUniqueDataPtr + uChunkSize * nDataElemSize;
509515

510516

511517
} else {
512518

513-
dprintf("mts/crc: Single-element skip-read: read [%ld] bytes per element, skip [%ld] bytes.\n", nDataElemSize, nDataElemSize * (uChunkSkip-1));
519+
dprintf("mts/crc: Single-element skip-read: read [%ld] elements, [%ld] bytes per element, skip [%ld] bytes.\n", uChunkSize, nDataElemSize, nDataElemSize * (uChunkSkip-1));
514520

515521
/* - Read an element, then skip elements */
516522
for (uElementIndex = 0; uElementIndex < uChunkSize; uElementIndex++) {

0 commit comments

Comments
 (0)