No need to check for string option values being null.

A string option, if present, always has a value; it might be a null
*string*, but you won't get a null pointer (if the option isn't present,
it simply isn't present).

Fix some comments while we're at it.

Change-Id: I9c1420f56998a7d04de5c5cc2e92631b181f303a
Reviewed-on: https://code.wireshark.org/review/16564
Reviewed-by: Guy Harris <guy@alum.mit.edu>
This commit is contained in:
Guy Harris 2016-07-20 17:27:36 -07:00
parent be1398c17c
commit 3beab65515
9 changed files with 28 additions and 39 deletions

View File

@ -993,34 +993,29 @@ print_stats_table(const gchar *filename, capture_info *cf_info)
char *opt_comment;
for (i = 0; wtap_block_get_nth_string_option_value(cf_info->shb, OPT_COMMENT, i, &opt_comment) == WTAP_OPTTYPE_SUCCESS; i++) {
if (opt_comment != NULL) {
putsep();
putquote();
printf("%s", opt_comment);
putquote();
}
putsep();
putquote();
printf("%s", opt_comment);
putquote();
}
}
if (cap_file_more_info) {
char *str;
if (wtap_block_get_string_option_value(cf_info->shb, OPT_SHB_HARDWARE, &str) == WTAP_OPTTYPE_SUCCESS &&
str != NULL) {
if (wtap_block_get_string_option_value(cf_info->shb, OPT_SHB_HARDWARE, &str) == WTAP_OPTTYPE_SUCCESS) {
putsep();
putquote();
printf("%s", str);
putquote();
}
if (wtap_block_get_string_option_value(cf_info->shb, OPT_SHB_OS, &str) == WTAP_OPTTYPE_SUCCESS &&
str != NULL) {
if (wtap_block_get_string_option_value(cf_info->shb, OPT_SHB_OS, &str) == WTAP_OPTTYPE_SUCCESS) {
putsep();
putquote();
printf("%s", str);
putquote();
}
if (wtap_block_get_string_option_value(cf_info->shb, OPT_SHB_USERAPPL, &str) == WTAP_OPTTYPE_SUCCESS &&
str != NULL) {
if (wtap_block_get_string_option_value(cf_info->shb, OPT_SHB_USERAPPL, &str) == WTAP_OPTTYPE_SUCCESS) {
putsep();
putquote();
printf("%s", str);

View File

@ -46,11 +46,9 @@ cap_file_get_interface_name(void *data, guint32 interface_id)
g_free(idb_info);
if (wtapng_if_descr) {
if (wtap_block_get_string_option_value(wtapng_if_descr, OPT_IDB_NAME, &interface_name) == WTAP_OPTTYPE_SUCCESS &&
interface_name)
if (wtap_block_get_string_option_value(wtapng_if_descr, OPT_IDB_NAME, &interface_name) == WTAP_OPTTYPE_SUCCESS)
return interface_name;
if (wtap_block_get_string_option_value(wtapng_if_descr, OPT_IDB_DESCR, &interface_name) == WTAP_OPTTYPE_SUCCESS &&
interface_name)
if (wtap_block_get_string_option_value(wtapng_if_descr, OPT_IDB_DESCR, &interface_name) == WTAP_OPTTYPE_SUCCESS)
return interface_name;
}
return "unknown";

View File

@ -1379,8 +1379,7 @@ main(int argc, char *argv[])
g_assert(filename);
/* If we don't have an application name add Editcap */
if (wtap_block_get_string_option_value(g_array_index(shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, &shb_user_appl) != WTAP_OPTTYPE_SUCCESS ||
shb_user_appl == NULL) {
if (wtap_block_get_string_option_value(g_array_index(shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, &shb_user_appl) != WTAP_OPTTYPE_SUCCESS) {
wtap_block_add_string_option_format(g_array_index(shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, "Editcap " VERSION);
}

View File

@ -558,7 +558,7 @@ extern int wslua_set__index(lua_State *L);
char* str; \
if ((obj->member) && (obj->member->len > 0)) { \
if (wtap_block_get_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, &str) == WTAP_OPTTYPE_SUCCESS) { \
lua_pushstring(L,str); /* this pushes nil if obj->member is null */ \
lua_pushstring(L,str); \
} \
} \
})
@ -572,7 +572,7 @@ extern int wslua_set__index(lua_State *L);
char* str; \
if ((obj->member) && (obj->member->len > 0)) { \
if (wtap_block_get_nth_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, 0, &str) == WTAP_OPTTYPE_SUCCESS) { \
lua_pushstring(L,str); /* this pushes nil if obj->member is null */ \
lua_pushstring(L,str); \
} \
} \
})

8
file.c
View File

@ -3866,11 +3866,9 @@ cf_update_capture_comment(capture_file *cf, gchar *comment)
wtap_block_add_string_option(shb_inf, OPT_COMMENT, comment, strlen(comment));
} else {
/* See if the comment has changed or not */
if (shb_comment) {
if (strcmp(shb_comment, comment) == 0) {
g_free(comment);
return;
}
if (strcmp(shb_comment, comment) == 0) {
g_free(comment);
return;
}
/* The comment has changed, let's update it */

View File

@ -2877,8 +2877,7 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
nrb_hdrs = wtap_file_get_nrb_for_new_file(cf->wth);
/* If we don't have an application name add Tshark */
if (wtap_block_get_string_option_value(g_array_index(shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, &shb_user_appl) != WTAP_OPTTYPE_SUCCESS ||
shb_user_appl == NULL) {
if (wtap_block_get_string_option_value(g_array_index(shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, &shb_user_appl) != WTAP_OPTTYPE_SUCCESS) {
/* this is free'd by wtap_block_free() later */
wtap_block_add_string_option_format(g_array_index(shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, "TShark (Wireshark) %s", get_ws_vcs_version_info());
}

View File

@ -296,7 +296,7 @@ summary_open_cb(GtkWidget *w _U_, gpointer d _U_)
/* XXX - this only shows the last comment */
for (i = 0; wtap_block_get_nth_string_option_value(shb_inf, OPT_COMMENT, i, &opt_comment) == WTAP_OPTTYPE_SUCCESS; i++) {
if (opt_comment != NULL && opt_comment[0] != '\0')
if (opt_comment[0] != '\0')
gtk_text_buffer_set_text (buffer, opt_comment, -1);
}
}
@ -349,20 +349,20 @@ summary_open_cb(GtkWidget *w _U_, gpointer d _U_)
char *str;
if (wtap_block_get_string_option_value(shb_inf, OPT_SHB_HARDWARE, &str) == WTAP_OPTTYPE_SUCCESS &&
str != NULL && str[0] != '\0') {
str[0] != '\0') {
g_snprintf(string_buff, SHB_STR_SNIP_LEN, "%s", str);
add_string_to_grid(grid, &row, "Capture HW:",string_buff);
}
if (wtap_block_get_string_option_value(shb_inf, OPT_SHB_OS, &str) == WTAP_OPTTYPE_SUCCESS &&
str != NULL && str[0] != '\0') {
str[0] != '\0') {
/* truncate the strings to a reasonable length */
g_snprintf(string_buff, SHB_STR_SNIP_LEN, "%s", str);
add_string_to_grid(grid, &row, "OS:", string_buff);
}
if (wtap_block_get_string_option_value(shb_inf, OPT_SHB_USERAPPL, &str) == WTAP_OPTTYPE_SUCCESS &&
str != NULL && str[0] != '\0') {
str[0] != '\0') {
/* truncate the strings to a reasonable length */
g_snprintf(string_buff, SHB_STR_SNIP_LEN, "%s", str);
add_string_to_grid(grid, &row, "Capture application:", string_buff);
@ -766,19 +766,19 @@ summary_to_texbuff(GtkTextBuffer *buffer)
char *str;
if (wtap_block_get_string_option_value(shb_inf, OPT_SHB_HARDWARE, &str) == WTAP_OPTTYPE_SUCCESS &&
str != NULL && str[0] != '\0') {
str[0] != '\0') {
/* truncate the string to a reasonable length */
g_snprintf(string_buff, SUM_STR_MAX, INDENT "Capture HW: %s\n", str);
gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
}
if (wtap_block_get_string_option_value(shb_inf, OPT_SHB_OS, &str) == WTAP_OPTTYPE_SUCCESS &&
str != NULL && str[0] != '\0') {
str[0] != '\0') {
/* truncate the strings to a reasonable length */
g_snprintf(string_buff, SUM_STR_MAX, INDENT "OS: %s\n", str);
gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
}
if (wtap_block_get_string_option_value(shb_inf, OPT_SHB_USERAPPL, &str) == WTAP_OPTTYPE_SUCCESS &&
str != NULL && str[0] != '\0') {
str[0] != '\0') {
/* truncate the string to a reasonable length */
g_snprintf(string_buff, SUM_STR_MAX, INDENT "Capture application: %s\n", str);
gtk_text_buffer_insert_at_cursor (buffer, string_buff, -1);
@ -902,7 +902,7 @@ summary_to_texbuff(GtkTextBuffer *buffer)
for (i = 0; wtap_block_get_nth_string_option_value(shb_inf, OPT_COMMENT, i, &opt_comment) == WTAP_OPTTYPE_SUCCESS; i++) {
/* XXX - separator between comments? */
if (opt_comment != NULL && opt_comment[0] != '\0')
if (opt_comment[0] != '\0')
gtk_text_buffer_insert_at_cursor(buffer, opt_comment, -1);
}
}

View File

@ -248,7 +248,7 @@ QString CaptureFilePropertiesDialog::summaryToHtml()
if (shb_inf != NULL) {
QString capture_hardware(unknown);
if (wtap_block_get_string_option_value(shb_inf, OPT_SHB_HARDWARE, &str) == WTAP_OPTTYPE_SUCCESS) {
if (str != NULL && str[0] != '\0') {
if (str[0] != '\0') {
capture_hardware = str;
}
}
@ -260,7 +260,7 @@ QString CaptureFilePropertiesDialog::summaryToHtml()
QString capture_os(unknown);
if (wtap_block_get_string_option_value(shb_inf, OPT_SHB_OS, &str) == WTAP_OPTTYPE_SUCCESS) {
if (str != NULL && str[0] != '\0') {
if (str[0] != '\0') {
capture_os = str;
}
}
@ -271,7 +271,7 @@ QString CaptureFilePropertiesDialog::summaryToHtml()
QString capture_app(unknown);
if (wtap_block_get_string_option_value(shb_inf, OPT_SHB_USERAPPL, &str) == WTAP_OPTTYPE_SUCCESS) {
if (str != NULL && str[0] != '\0') {
if (str[0] != '\0') {
capture_app = str;
}
}

View File

@ -387,7 +387,7 @@ create_shb_header(const merge_in_file_t *in_files, const guint in_file_count,
* XXX - fix this to handle multiple comments from a single file.
*/
if (wtap_block_get_nth_string_option_value(shb_hdr, OPT_COMMENT, 0, &shb_comment) == WTAP_OPTTYPE_SUCCESS &&
shb_comment && strlen(shb_comment) > 0) {
strlen(shb_comment) > 0) {
/* very lame way to save comments - does not save them from the other files */
g_string_append_printf(comment_gstr, "%s \n",shb_comment);
}