Handle error condition in file.read() (#1599)
* handle error condition in file.read() * simplify loop initialization
This commit is contained in:
parent
59b9b3e26f
commit
c70dc7497e
|
@ -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);
|
||||
// bypass search if no end character provided
|
||||
for (i = end_char != EOF ? 0 : n; i < n; ++i)
|
||||
if (p[i] == end_char)
|
||||
{
|
||||
++i;
|
||||
break;
|
||||
}
|
||||
if (n > 0 && end_char != EOF) {
|
||||
for (i = 0; i < n; ++i)
|
||||
if (p[i] == end_char)
|
||||
{
|
||||
++i;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
i = n;
|
||||
}
|
||||
|
||||
if (i == 0) {
|
||||
if (i == 0 || n == VFS_RES_ERR) {
|
||||
if (heap_mem) {
|
||||
luaM_free(L, heap_mem);
|
||||
heap_mem = NULL;
|
||||
|
|
|
@ -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 ) {
|
||||
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 ) {
|
||||
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 ) {
|
||||
|
|
Loading…
Reference in New Issue