From d4ae67d0b4ec178fccbcbbbb14c0c5b0b48ce860 Mon Sep 17 00:00:00 2001 From: jfollas Date: Sat, 13 Feb 2016 18:58:04 -0500 Subject: [PATCH] Changed filename length error handling per TerryE's suggestion --- app/modules/file.c | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/app/modules/file.c b/app/modules/file.c index d9f77062..f588bfa5 100644 --- a/app/modules/file.c +++ b/app/modules/file.c @@ -20,8 +20,8 @@ static int file_open( lua_State* L ) } const char *fname = luaL_checklstring( L, 1, &len ); - if( len > FS_NAME_MAX_LENGTH ) - return luaL_error(L, "filename too long"); + luaL_argcheck(L, len <= FS_NAME_MAX_LENGTH, 1, "filename too long"); + const char *mode = luaL_optstring(L, 2, "r"); file_fd = fs_open(fname, fs_mode2flag(mode)); @@ -118,9 +118,8 @@ static int file_seek (lua_State *L) static int file_exists( lua_State* L ) { size_t len; - const char *fname = luaL_checklstring( L, 1, &len ); - if( len > FS_NAME_MAX_LENGTH ) - return luaL_error(L, "filename too long"); + const char *fname = luaL_checklstring( L, 1, &len ); + luaL_argcheck(L, len <= FS_NAME_MAX_LENGTH, 1, "filename too long"); spiffs_stat stat; int rc = SPIFFS_stat(&fs, (char *)fname, &stat); @@ -134,9 +133,8 @@ static int file_exists( lua_State* L ) static int file_remove( lua_State* L ) { size_t len; - const char *fname = luaL_checklstring( L, 1, &len ); - if( len > FS_NAME_MAX_LENGTH ) - return luaL_error(L, "filename too long"); + const char *fname = luaL_checklstring( L, 1, &len ); + luaL_argcheck(L, len <= FS_NAME_MAX_LENGTH, 1, "filename too long"); file_close(L); SPIFFS_remove(&fs, (char *)fname); return 0; @@ -173,12 +171,10 @@ static int file_rename( lua_State* L ) } const char *oldname = luaL_checklstring( L, 1, &len ); - if( len > FS_NAME_MAX_LENGTH ) - return luaL_error(L, "filename too long"); - - const char *newname = luaL_checklstring( L, 2, &len ); - if( len > FS_NAME_MAX_LENGTH ) - return luaL_error(L, "filename too long"); + luaL_argcheck(L, len <= FS_NAME_MAX_LENGTH, 1, "filename too long"); + + const char *newname = luaL_checklstring( L, 2, &len ); + luaL_argcheck(L, len <= FS_NAME_MAX_LENGTH, 2, "filename too long"); if(SPIFFS_OK==myspiffs_rename( oldname, newname )){ lua_pushboolean(L, 1); @@ -325,7 +321,6 @@ static const LUA_REG_TYPE file_map[] = { { LSTRKEY( "remove" ), LFUNCVAL( file_remove ) }, { LSTRKEY( "seek" ), LFUNCVAL( file_seek ) }, { LSTRKEY( "flush" ), LFUNCVAL( file_flush ) }, -//{ LSTRKEY( "check" ), LFUNCVAL( file_check ) }, { LSTRKEY( "rename" ), LFUNCVAL( file_rename ) }, { LSTRKEY( "fsinfo" ), LFUNCVAL( file_fsinfo ) }, { LSTRKEY( "exists" ), LFUNCVAL( file_exists ) },