Skip to content

Commit e802ea7

Browse files
committed
Harden File::Read()
Check f_read() result and bytesread truncation. Fix #15 thanks @Hoek67 Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 7affa73 commit e802ea7

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/SD.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,12 @@ void File::printTwoDigits(uint8_t v)
354354
*/
355355
int File::read()
356356
{
357-
uint8_t byteread;
357+
UINT byteread;
358358
int8_t data;
359-
f_read(_fil, (void *)&data, 1, (UINT *)&byteread);
360-
return data;
359+
if (f_read(_fil, (void *)&data, 1, (UINT *)&byteread) == FR_OK) {
360+
return data;
361+
}
362+
return -1;
361363
}
362364

363365
/**
@@ -368,11 +370,12 @@ int File::read()
368370
*/
369371
int File::read(void *buf, size_t len)
370372
{
371-
uint8_t bytesread;
372-
373-
f_read(_fil, buf, len, (UINT *)&bytesread);
374-
return bytesread;
373+
UINT bytesread;
375374

375+
if (f_read(_fil, buf, len, (UINT *)&bytesread) == FR_OK) {
376+
return bytesread;
377+
}
378+
return -1;
376379
}
377380

378381
/**

0 commit comments

Comments
 (0)