From c70dc7497eb4d39f0862e1476e535fb30748edff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnim=20L=C3=A4uger?= Date: Sat, 19 Nov 2016 16:36:00 +0100 Subject: [PATCH] Handle error condition in file.read() (#1599) * handle error condition in file.read() * simplify loop initialization --- app/modules/file.c | 18 +++++++++++------- app/spiffs/spiffs.c | 8 ++++++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/app/modules/file.c b/app/modules/file.c index 66d3eeb7..3e9de55d 100644 --- a/app/modules/file.c +++ b/app/modules/file.c @@ -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; diff --git a/app/spiffs/spiffs.c b/app/spiffs/spiffs.c index 036118ee..3b71cc03 100644 --- a/app/spiffs/spiffs.c +++ b/app/spiffs/spiffs.c @@ -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 ) {