From b653564fe21c84482d99586b437be1ffcb35459a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stig=20Bj=C3=B8rlykke?= Date: Sun, 12 Oct 2014 22:27:54 +0200 Subject: Lua: luaL_checkstring does not return NULL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removed all checks for NULL strings to cleanup the code. Change-Id: Ia890cd9b206296f586e85214f07765f14984580b Reviewed-on: https://code.wireshark.org/review/4632 Tested-by: Stig Bjørlykke Reviewed-by: Stig Bjørlykke --- epan/wslua/wslua_dir.c | 5 -- epan/wslua/wslua_dumper.c | 12 +---- epan/wslua/wslua_field.c | 2 - epan/wslua/wslua_file.c | 15 +++--- epan/wslua/wslua_gui.c | 61 ++-------------------- epan/wslua/wslua_int64.c | 4 +- epan/wslua/wslua_pinfo.c | 17 ------- epan/wslua/wslua_proto.c | 126 +++++++++++++++++++--------------------------- epan/wslua/wslua_struct.c | 10 ---- epan/wslua/wslua_tvb.c | 5 -- epan/wslua/wslua_util.c | 9 +--- 11 files changed, 65 insertions(+), 201 deletions(-) (limited to 'epan') diff --git a/epan/wslua/wslua_dir.c b/epan/wslua/wslua_dir.c index d39484df48..2368a4e5b1 100644 --- a/epan/wslua/wslua_dir.c +++ b/epan/wslua/wslua_dir.c @@ -195,11 +195,6 @@ WSLUA_CONSTRUCTOR Dir_open(lua_State* L) { Dir dir; char* dirname_clean; - if (!dirname) { - WSLUA_ARG_ERROR(Dir_open,PATHNAME,"must be a string"); - return 0; - } - dirname_clean = wslua_get_actual_filename(dirname); if (!dirname_clean) { WSLUA_ARG_ERROR(Dir_open,PATHNAME,"directory does not exist"); diff --git a/epan/wslua/wslua_dumper.c b/epan/wslua/wslua_dumper.c index 4de04cf15e..80469c2963 100644 --- a/epan/wslua/wslua_dumper.c +++ b/epan/wslua/wslua_dumper.c @@ -216,11 +216,7 @@ WSLUA_CONSTRUCTOR Dumper_new(lua_State* L) { int filetype = luaL_optint(L,WSLUA_OPTARG_Dumper_new_FILETYPE,WTAP_FILE_TYPE_SUBTYPE_PCAP); int encap = luaL_optint(L,WSLUA_OPTARG_Dumper_new_ENCAP,WTAP_ENCAP_ETHERNET); int err = 0; - const char* filename; - - if (! fname) return 0; - - filename = cross_plat_fname(fname); + const char* filename = cross_plat_fname(fname); d = wtap_dump_open(filename, filetype, encap, 0, FALSE, &err); @@ -358,11 +354,7 @@ WSLUA_METHOD Dumper_new_for_current(lua_State* L) { int filetype = luaL_optint(L,WSLUA_OPTARG_Dumper_new_for_current_FILETYPE,WTAP_FILE_TYPE_SUBTYPE_PCAP); int encap; int err = 0; - const char* filename; - - if (! fname) return 0; - - filename = cross_plat_fname(fname); + const char* filename = cross_plat_fname(fname); if (! lua_pinfo ) { WSLUA_ERROR(Dumper_new_for_current,"Cannot be used outside a tap or a dissector"); diff --git a/epan/wslua/wslua_field.c b/epan/wslua/wslua_field.c index ad757929bd..0036f85f8a 100644 --- a/epan/wslua/wslua_field.c +++ b/epan/wslua/wslua_field.c @@ -499,8 +499,6 @@ WSLUA_CONSTRUCTOR Field_new(lua_State *L) { const gchar* name = luaL_checkstring(L,WSLUA_ARG_Field_new_FIELDNAME); Field f; - if (!name) return 0; - if (!proto_registrar_get_byname(name) && !wslua_is_field_available(L, name)) { WSLUA_ARG_ERROR(Field_new,FIELDNAME,"a field with this name must exist"); return 0; diff --git a/epan/wslua/wslua_file.c b/epan/wslua/wslua_file.c index 5802a1a447..2c73a8f37a 100644 --- a/epan/wslua/wslua_file.c +++ b/epan/wslua/wslua_file.c @@ -1282,15 +1282,12 @@ static int FrameInfo_set_data (lua_State* L) { if (lua_isstring(L,2)) { size_t len = 0; const gchar* s = luaL_checklstring(L,2,&len); - if (s) { - /* Make sure we have enough room for the packet */ - ws_buffer_assure_space(fi->buf, len); - memcpy(ws_buffer_start_ptr(fi->buf), s, len); - fi->phdr->caplen = (guint32) len; - fi->phdr->len = (guint32) len; - } else { - luaL_error(L, "FrameInfo's attribute 'data' did not get a valid Lua string"); - } + + /* Make sure we have enough room for the packet */ + ws_buffer_assure_space(fi->buf, len); + memcpy(ws_buffer_start_ptr(fi->buf), s, len); + fi->phdr->caplen = (guint32) len; + fi->phdr->len = (guint32) len; } else luaL_error(L, "FrameInfo's attribute 'data' must be a Lua string"); diff --git a/epan/wslua/wslua_gui.c b/epan/wslua/wslua_gui.c index 0060a667bb..56efdac858 100644 --- a/epan/wslua/wslua_gui.c +++ b/epan/wslua/wslua_gui.c @@ -100,11 +100,6 @@ WSLUA_FUNCTION wslua_register_menu(lua_State* L) { /* Register a menu item in o return 0; } - if(!name) { - WSLUA_ARG_ERROR(register_menu,NAME,"Must be a string"); - return 0; - } - if (!lua_isfunction(L,WSLUA_ARG_register_menu_ACTION)) { WSLUA_ARG_ERROR(register_menu,ACTION,"Must be a function"); return 0; @@ -239,10 +234,7 @@ WSLUA_FUNCTION wslua_new_dialog(lua_State* L) { /* Pops up a new dialog */ return 0; } - if (! (title = luaL_checkstring(L,WSLUA_ARG_new_dialog_TITLE)) ) { - WSLUA_ARG_ERROR(new_dialog,TITLE,"Must be a string"); - return 0; - } + title = luaL_checkstring(L,WSLUA_ARG_new_dialog_TITLE); if (! lua_isfunction(L,WSLUA_ARG_new_dialog_ACTION)) { WSLUA_ARG_ERROR(new_dialog,ACTION,"Must be a function"); @@ -269,15 +261,13 @@ WSLUA_FUNCTION wslua_new_dialog(lua_State* L) { /* Pops up a new dialog */ top -= 2; for (i = 1; i <= top; i++) { - gchar* label = g_strdup(luaL_checkstring(L,i)); - - if (! label) { + if (! lua_isstring(L,i)) { g_ptr_array_free(labels,TRUE); WSLUA_ERROR(new_dialog,"All fields must be strings"); return 0; } - g_ptr_array_add(labels,(gpointer)label); + g_ptr_array_add(labels,(gpointer)g_strdup(luaL_checkstring(L,i))); } g_ptr_array_add(labels,NULL); @@ -499,11 +489,6 @@ WSLUA_METHOD TextWindow_set(lua_State* L) { /* Sets the text. */ return 0; } - if (!text) { - WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Must be a string"); - return 0; - } - ops->set_text(tw->ws_tw,text); /* XXX: this is a bad way to do this - should copy the object on to the stack first */ @@ -520,11 +505,6 @@ WSLUA_METHOD TextWindow_append(lua_State* L) { /* Appends text */ return 0; } - if (!text) { - WSLUA_ARG_ERROR(TextWindow_append,TEXT,"Must be a string"); - return 0; - } - ops->append_text(tw->ws_tw,text); /* XXX: this is a bad way to do this - should copy the object on to the stack first */ @@ -541,11 +521,6 @@ WSLUA_METHOD TextWindow_prepend(lua_State* L) { /* Prepends text */ return 0; } - if (!text) { - WSLUA_ARG_ERROR(TextWindow_prepend,TEXT,"Must be a string"); - return 0; - } - ops->prepend_text(tw->ws_tw,text); /* XXX: this is a bad way to do this - should copy the object on to the stack first */ @@ -743,11 +718,6 @@ WSLUA_FUNCTION wslua_copy_to_clipboard(lua_State* L) { /* Copy a string into the return 0; } - if (!copied_str) { - WSLUA_ARG_ERROR(copy_to_clipboard,TEXT,"Must be a string"); - return 0; - } - gstr = g_string_new(copied_str); ops->copy_to_clipboard(gstr); @@ -770,11 +740,6 @@ WSLUA_FUNCTION wslua_open_capture_file(lua_State* L) { /* Open and display a cap return 0; } - if (!fname) { - WSLUA_ARG_ERROR(open_capture_file,FILENAME,"Must be a string"); - return 0; - } - if (! ops->open_file(fname,filter,&error) ) { lua_pushboolean(L,FALSE); @@ -813,11 +778,6 @@ WSLUA_FUNCTION wslua_set_filter(lua_State* L) { /* Set the main filter text. */ return 0; } - if (!filter_str) { - WSLUA_ARG_ERROR(set_filter,TEXT,"Must be a string"); - return 0; - } - ops->set_filter(filter_str); return 0; @@ -834,11 +794,6 @@ WSLUA_FUNCTION wslua_set_color_filter_slot(lua_State* L) { /* Set packet-colorin return 0; } - if (!filter_str) { - WSLUA_ARG_ERROR(set_color_filter_slot,TEXT,"Must be a string"); - return 0; - } - ops->set_color_filter_slot(row, filter_str); return 0; @@ -878,11 +833,6 @@ WSLUA_FUNCTION wslua_browser_open_url(lua_State* L) { /* Open an url in a browse return 0; } - if (!url) { - WSLUA_ARG_ERROR(browser_open_url,URL,"Must be a string"); - return 0; - } - ops->browser_open_url(url); return 0; @@ -897,11 +847,6 @@ WSLUA_FUNCTION wslua_browser_open_data_file(lua_State* L) { /* Open a file in a return 0; } - if (!file) { - WSLUA_ARG_ERROR(browser_open_data_file,FILENAME,"Must be a string"); - return 0; - } - ops->browser_open_data_file(file); return 0; diff --git a/epan/wslua/wslua_int64.c b/epan/wslua/wslua_int64.c index 4c1a4dc9c2..8cb79b755d 100644 --- a/epan/wslua/wslua_int64.c +++ b/epan/wslua/wslua_int64.c @@ -255,7 +255,7 @@ WSLUA_CONSTRUCTOR Int64_fromhex(lua_State* L) { size_t len = 0; const gchar *s = luaL_checklstring(L,WSLUA_ARG_Int64_fromhex_HEX,&len); - if (s && len > 0) { + if (len > 0) { sscanf(s, "%" G_GINT64_MODIFIER "x", &result); } pushInt64(L,(gint64)result); @@ -815,7 +815,7 @@ WSLUA_CONSTRUCTOR UInt64_fromhex(lua_State* L) { size_t len = 0; const gchar *s = luaL_checklstring(L,WSLUA_ARG_UInt64_fromhex_HEX,&len); - if (s && len > 0) { + if (len > 0) { sscanf(s, "%" G_GINT64_MODIFIER "x", &result); } pushUInt64(L,result); diff --git a/epan/wslua/wslua_pinfo.c b/epan/wslua/wslua_pinfo.c index 0fdd16f701..abe78eb1c9 100644 --- a/epan/wslua/wslua_pinfo.c +++ b/epan/wslua/wslua_pinfo.c @@ -580,11 +580,6 @@ WSLUA_METHOD Column_set(lua_State *L) { if (!(c->cinfo)) return 0; - if (!s) { - WSLUA_ARG_ERROR(Column_set,TEXT,"must be a string"); - return 0; - } - col_add_str(c->cinfo, c->col, s); return 0; @@ -599,11 +594,6 @@ WSLUA_METHOD Column_append(lua_State *L) { if (!(c->cinfo)) return 0; - if (!s) { - WSLUA_ARG_ERROR(Column_append,TEXT,"must be a string"); - return 0; - } - col_append_str(c->cinfo, c->col, s); return 0; @@ -618,11 +608,6 @@ WSLUA_METHOD Column_prepend(lua_State *L) { if (!(c->cinfo)) return 0; - if (!s) { - WSLUA_ARG_ERROR(Column_prepend,TEXT,"must be a string"); - return 0; - } - col_prepend_fstr(c->cinfo, c->col, "%s",s); return 0; @@ -743,8 +728,6 @@ WSLUA_METAMETHOD Columns__index(lua_State *L) { return 0; } - if (!colname) return 0; - for(cn = colnames; cn->name; cn++) { if( g_str_equal(cn->name,colname) ) { Column c = (Column)g_malloc(sizeof(struct _wslua_col_info)); diff --git a/epan/wslua/wslua_proto.c b/epan/wslua/wslua_proto.c index 6cb31dde52..53e580a204 100644 --- a/epan/wslua/wslua_proto.c +++ b/epan/wslua/wslua_proto.c @@ -297,11 +297,6 @@ WSLUA_METAMETHOD Prefs__newindex(lua_State* L) { if (! prefs_p ) return 0; - if (! name ) { - WSLUA_ARG_ERROR(Prefs__newindex,NAME,"must be a string"); - return 0; - } - if (! pref ) { WSLUA_ARG_ERROR(Prefs__newindex,PREF,"must be a valid Pref"); return 0; @@ -419,7 +414,7 @@ WSLUA_METAMETHOD Prefs__index(lua_State* L) { Pref prefs_p = checkPrefs(L,1); const gchar* name = luaL_checkstring(L,WSLUA_ARG_Prefs__index_NAME); - if (! ( name && prefs_p ) ) return 0; + if (! prefs_p ) return 0; if (!prefs_p->next) { luaL_error(L,"No preference is registered yet"); @@ -1495,64 +1490,66 @@ WSLUA_CONSTRUCTOR Proto_new(lua_State* L) { #define WSLUA_ARG_Proto_new_DESC 2 /* A Long Text description of the protocol (usually lowercase). */ const gchar* name = luaL_checkstring(L,WSLUA_ARG_Proto_new_NAME); const gchar* desc = luaL_checkstring(L,WSLUA_ARG_Proto_new_DESC); + Proto proto; + gchar *loname, *hiname; + int proto_id; /* TODO: should really make a common function for all of wslua that does checkstring and non-empty at same time */ - if (!name[0] || !desc[0]) - luaL_argerror(L,WSLUA_ARG_Proto_new_NAME,"must not be an empty string"); - - if ( name ) { - gchar* loname_a; - int proto_id; - - loname_a = g_ascii_strdown(name, -1); - proto_id = proto_get_id_by_filter_name(loname_a); - g_free(loname_a); - if ( proto_id > 0 ) { - WSLUA_ARG_ERROR(Proto_new,NAME,"there cannot be two protocols with the same name"); - return 0; - } else { - Proto proto = (wslua_proto_t *)g_malloc(sizeof(wslua_proto_t)); - gchar* loname = g_ascii_strdown(name, -1); - gchar* hiname = g_ascii_strup(name, -1); + if (!name[0]) { + WSLUA_ARG_ERROR(Proto_new,NAME,"must not be an empty string"); + return 0; + } - proto->name = hiname; - proto->desc = g_strdup(desc); - proto->hfid = proto_register_protocol(proto->desc,hiname,loname); - proto->ett = -1; - proto->is_postdissector = FALSE; + if (!desc[0]) { + WSLUA_ARG_ERROR(Proto_new,DESC,"must not be an empty string"); + return 0; + } - lua_newtable (L); - proto->fields = luaL_ref(L, LUA_REGISTRYINDEX); + loname = g_ascii_strdown(name, -1); + proto_id = proto_get_id_by_filter_name(loname); - lua_newtable (L); - proto->expert_info_table_ref = luaL_ref(L, LUA_REGISTRYINDEX); - proto->expert_module = expert_register_protocol(proto->hfid); + if (proto_id > 0) { + WSLUA_ARG_ERROR(Proto_new,NAME,"there cannot be two protocols with the same name"); + g_free(loname); + return 0; + } - proto->prefs.name = NULL; - proto->prefs.label = NULL; - proto->prefs.desc = NULL; - proto->prefs.value.u = 0; - proto->prefs.next = NULL; - proto->prefs.proto = proto; + proto = (wslua_proto_t *)g_malloc(sizeof(wslua_proto_t)); + hiname = g_ascii_strup(name, -1); - proto->prefs_module = NULL; - proto->handle = NULL; + proto->name = hiname; + proto->desc = g_strdup(desc); + proto->hfid = proto_register_protocol(proto->desc,hiname,loname); + proto->ett = -1; + proto->is_postdissector = FALSE; - lua_rawgeti(L, LUA_REGISTRYINDEX, protocols_table_ref); + lua_newtable (L); + proto->fields = luaL_ref(L, LUA_REGISTRYINDEX); - lua_pushstring(L,loname); - pushProto(L,proto); + lua_newtable (L); + proto->expert_info_table_ref = luaL_ref(L, LUA_REGISTRYINDEX); + proto->expert_module = expert_register_protocol(proto->hfid); - lua_settable(L, -3); + proto->prefs.name = NULL; + proto->prefs.label = NULL; + proto->prefs.desc = NULL; + proto->prefs.value.u = 0; + proto->prefs.next = NULL; + proto->prefs.proto = proto; - pushProto(L,proto); + proto->prefs_module = NULL; + proto->handle = NULL; - WSLUA_RETURN(1); /* The newly created protocol. */ - } - } + lua_rawgeti(L, LUA_REGISTRYINDEX, protocols_table_ref); - WSLUA_ARG_ERROR(Proto_new,NAME,"must be a string"); - return 0; + lua_pushstring(L,loname); + pushProto(L,proto); + + lua_settable(L, -3); + + pushProto(L,proto); + + WSLUA_RETURN(1); /* The newly created protocol. */ } WSLUA_METAMETHOD Proto__call(lua_State* L) { /* Creates a `Proto` object. */ @@ -2028,11 +2025,6 @@ WSLUA_CONSTRUCTOR Dissector_get (lua_State *L) { const gchar* name = luaL_checkstring(L,WSLUA_ARG_Dissector_get_NAME); Dissector d; - if (!name) { - WSLUA_ARG_ERROR(Dissector_get,NAME,"must be a string"); - return 0; - } - if ((d = find_dissector(name))) { pushDissector(L, d); WSLUA_RETURN(1); /* The Dissector reference. */ @@ -2169,8 +2161,6 @@ WSLUA_CONSTRUCTOR DissectorTable_new (lua_State *L) { enum ftenum type = (enum ftenum)luaL_optint(L,WSLUA_OPTARG_DissectorTable_new_TYPE,FT_UINT32); unsigned base = (unsigned)luaL_optint(L,WSLUA_OPTARG_DissectorTable_new_BASE,BASE_DEC); - if(!(name && ui_name)) return 0; - switch(type) { case FT_STRING: base = BASE_NONE; @@ -2264,11 +2254,7 @@ WSLUA_CONSTRUCTOR DissectorTable_get (lua_State *L) { */ #define WSLUA_ARG_DissectorTable_get_TABLENAME 1 /* The short name of the table. */ const gchar* name = luaL_checkstring(L,WSLUA_ARG_DissectorTable_get_TABLENAME); - dissector_table_t table; - - if(!name) return 0; - - table = find_dissector_table(name); + dissector_table_t table = find_dissector_table(name); if (table) { DissectorTable dt = (DissectorTable)g_malloc(sizeof(struct _wslua_distbl_t)); @@ -2514,13 +2500,9 @@ WSLUA_METHOD DissectorTable_try (lua_State *L) { if (type == FT_STRING) { const gchar* pattern = luaL_checkstring(L,WSLUA_ARG_DissectorTable_try_PATTERN); - if (!pattern) { + len = dissector_try_string(dt->table,pattern,tvb->ws_tvb,pinfo->ws_pinfo,ti->tree, NULL); + if (len > 0) { handled = TRUE; - } else { - len = dissector_try_string(dt->table,pattern,tvb->ws_tvb,pinfo->ws_pinfo,ti->tree, NULL); - if (len > 0) { - handled = TRUE; - } } } else if ( type == FT_UINT32 || type == FT_UINT16 || type == FT_UINT8 || type == FT_UINT24 ) { int port = luaL_checkint(L, WSLUA_ARG_DissectorTable_try_PATTERN); @@ -2564,12 +2546,6 @@ WSLUA_METHOD DissectorTable_get_dissector (lua_State *L) { if (type == FT_STRING) { const gchar* pattern = luaL_checkstring(L,WSLUA_ARG_DissectorTable_get_dissector_PATTERN); - - if (!pattern) { - WSLUA_ARG_ERROR(DissectorTable_get_dissector,PATTERN,"must be a string"); - return 0; - } - handle = dissector_get_string_handle(dt->table,pattern); } else if ( type == FT_UINT32 || type == FT_UINT16 || type == FT_UINT8 || type == FT_UINT24 ) { int port = luaL_checkint(L, WSLUA_ARG_DissectorTable_get_dissector_PATTERN); diff --git a/epan/wslua/wslua_struct.c b/epan/wslua/wslua_struct.c index 06e5c203c7..db7e957a9c 100644 --- a/epan/wslua/wslua_struct.c +++ b/epan/wslua/wslua_struct.c @@ -622,11 +622,6 @@ WSLUA_CONSTRUCTOR Struct_tohex (lua_State *L) { just not fromhex. In fact, we should accept/coerce a Int64/UInt64 here too someday. */ s = luaL_checklstring(L, WSLUA_ARG_Struct_tohex_BYTESTRING, &len); - if (!s) { - WSLUA_ARG_ERROR(Struct_tohex,BYTESTRING,"must be a Lua string"); - return 0; - } - lowercase = wslua_optbool(L,WSLUA_OPTARG_Struct_tohex_LOWERCASE,FALSE); sep = luaL_optstring(L,WSLUA_OPTARG_Struct_tohex_SEPARATOR,NULL); @@ -645,11 +640,6 @@ WSLUA_CONSTRUCTOR Struct_fromhex (lua_State *L) { /* luaL_checklstring coerces the argument to a string, and we don't want to do that */ s = wslua_checklstring_only(L, WSLUA_ARG_Struct_fromhex_HEXBYTES, &len); - if (!s) { - WSLUA_ARG_ERROR(Struct_fromhex,HEXBYTES,"must be a Lua string"); - return 0; - } - sep = luaL_optstring(L,WSLUA_OPTARG_Struct_fromhex_SEPARATOR,NULL); wslua_hex2bin(L, s, (guint)len, sep); diff --git a/epan/wslua/wslua_tvb.c b/epan/wslua/wslua_tvb.c index b78814c441..2e129ade98 100644 --- a/epan/wslua/wslua_tvb.c +++ b/epan/wslua/wslua_tvb.c @@ -51,11 +51,6 @@ WSLUA_CONSTRUCTOR ByteArray_new(lua_State* L) { /* Creates a `ByteArray` object. if (lua_gettop(L) >= 1) { s = luaL_checklstring(L,WSLUA_OPTARG_ByteArray_new_HEXBYTES,&len); - if (!s) { - WSLUA_OPTARG_ERROR(ByteArray_new,HEXBYTES,"must be a string"); - return 0; - } - if (lua_gettop(L) >= 2) { if (lua_type(L,2) == LUA_TBOOLEAN && lua_toboolean(L,2)) { ishex = FALSE; diff --git a/epan/wslua/wslua_util.c b/epan/wslua/wslua_util.c index ea53e6b542..d9c2766fdc 100644 --- a/epan/wslua/wslua_util.c +++ b/epan/wslua/wslua_util.c @@ -207,16 +207,9 @@ WSLUA_FUNCTION wslua_dofile(lua_State* L) { in the current directory it will look for it in wireshark's user and system directories. */ #define WSLUA_ARG_dofile_FILENAME 1 /* Name of the file to be run. */ const char *given_fname = luaL_checkstring(L, WSLUA_ARG_dofile_FILENAME); - char* filename; + char* filename = wslua_get_actual_filename(given_fname); int n; - if (!given_fname) { - WSLUA_ARG_ERROR(dofile,FILENAME,"must be a string"); - return 0; - } - - filename = wslua_get_actual_filename(given_fname); - if (!filename) { WSLUA_ARG_ERROR(dofile,FILENAME,"file does not exist"); return 0; -- cgit v1.2.3