Handle error condition in file.read() (#1599)

* handle error condition in file.read()

* simplify loop initialization
This commit is contained in:
Arnim Läuger 2016-11-19 16:36:00 +01:00 committed by Philip Gladstone
parent 59b9b3e26f
commit c70dc7497e
2 changed files with 17 additions and 9 deletions

View File

@ -350,14 +350,18 @@ static int file_g_read( lua_State* L, int n, int16_t end_char, int fd )
n = vfs_read(fd, p, n); n = vfs_read(fd, p, n);
// bypass search if no end character provided // bypass search if no end character provided
for (i = end_char != EOF ? 0 : n; i < n; ++i) if (n > 0 && end_char != EOF) {
if (p[i] == end_char) for (i = 0; i < n; ++i)
{ if (p[i] == end_char)
++i; {
break; ++i;
} break;
}
} else {
i = n;
}
if (i == 0) { if (i == 0 || n == VFS_RES_ERR) {
if (heap_mem) { if (heap_mem) {
luaM_free(L, heap_mem); luaM_free(L, heap_mem);
heap_mem = NULL; heap_mem = NULL;

View File

@ -433,13 +433,17 @@ static sint32_t myspiffs_vfs_close( const struct vfs_file *fd ) {
static sint32_t myspiffs_vfs_read( const struct vfs_file *fd, void *ptr, size_t len ) { static sint32_t myspiffs_vfs_read( const struct vfs_file *fd, void *ptr, size_t len ) {
GET_FILE_FH(fd); GET_FILE_FH(fd);
return SPIFFS_read( &fs, fh, ptr, len ); sint32_t n = SPIFFS_read( &fs, fh, ptr, len );
return n >= 0 ? n : VFS_RES_ERR;
} }
static sint32_t myspiffs_vfs_write( const struct vfs_file *fd, const void *ptr, size_t len ) { static sint32_t myspiffs_vfs_write( const struct vfs_file *fd, const void *ptr, size_t len ) {
GET_FILE_FH(fd); GET_FILE_FH(fd);
return SPIFFS_write( &fs, fh, (void *)ptr, len ); sint32_t n = SPIFFS_write( &fs, fh, (void *)ptr, len );
return n >= 0 ? n : VFS_RES_ERR;
} }
static sint32_t myspiffs_vfs_lseek( const struct vfs_file *fd, sint32_t off, int whence ) { static sint32_t myspiffs_vfs_lseek( const struct vfs_file *fd, sint32_t off, int whence ) {