diff --git a/capinfos.c b/capinfos.c index 96a7773e53..8accd470b7 100644 --- a/capinfos.c +++ b/capinfos.c @@ -1240,25 +1240,25 @@ process_cap_file(wtap *wth, const char *filename) wtap_optionblock_get_option_string(shb_inf, OPT_COMMENT, &shb_str); shb_str_no_newlines = g_strdup(shb_str); string_replace_newlines(shb_str_no_newlines); - wtap_optionblock_set_option_string(cf_info.shb, OPT_COMMENT, shb_str_no_newlines); + wtap_optionblock_set_option_string(cf_info.shb, OPT_COMMENT, shb_str_no_newlines, (gsize)(shb_str_no_newlines ? strlen(shb_str_no_newlines) : 0)); g_free(shb_str_no_newlines); wtap_optionblock_get_option_string(shb_inf, OPT_SHB_HARDWARE, &shb_str); shb_str_no_newlines = g_strdup(shb_str); string_replace_newlines(shb_str_no_newlines); - wtap_optionblock_set_option_string(cf_info.shb, OPT_SHB_HARDWARE, shb_str_no_newlines); + wtap_optionblock_set_option_string(cf_info.shb, OPT_SHB_HARDWARE, shb_str_no_newlines, (gsize)(shb_str_no_newlines ? strlen(shb_str_no_newlines) : 0)); g_free(shb_str_no_newlines); wtap_optionblock_get_option_string(shb_inf, OPT_SHB_OS, &shb_str); shb_str_no_newlines = g_strdup(shb_str); string_replace_newlines(shb_str_no_newlines); - wtap_optionblock_set_option_string(cf_info.shb, OPT_SHB_OS, shb_str_no_newlines); + wtap_optionblock_set_option_string(cf_info.shb, OPT_SHB_OS, shb_str_no_newlines, (gsize)(shb_str_no_newlines ? strlen(shb_str_no_newlines) : 0)); g_free(shb_str_no_newlines); wtap_optionblock_get_option_string(shb_inf, OPT_SHB_USERAPPL, &shb_str); shb_str_no_newlines = g_strdup(shb_str); string_replace_newlines(shb_str_no_newlines); - wtap_optionblock_set_option_string(cf_info.shb, OPT_SHB_USERAPPL, shb_str_no_newlines); + wtap_optionblock_set_option_string(cf_info.shb, OPT_SHB_USERAPPL, shb_str_no_newlines, (gsize)(shb_str_no_newlines ? strlen(shb_str_no_newlines) : 0)); g_free(shb_str_no_newlines); } diff --git a/debian/libwiretap0.symbols b/debian/libwiretap0.symbols index 2c7ff4a8d0..ec7bb30b9e 100644 --- a/debian/libwiretap0.symbols +++ b/debian/libwiretap0.symbols @@ -86,6 +86,7 @@ libwiretap.so.0 libwiretap0 #MINVER# wtap_optionblock_get_option_uint64@Base 2.1.0 wtap_optionblock_set_option_custom@Base 2.1.0 wtap_optionblock_set_option_string@Base 2.1.0 + wtap_optionblock_set_option_string_format@Base 2.1.0 wtap_optionblock_set_option_uint8@Base 2.1.0 wtap_optionblock_set_option_uint64@Base 2.1.0 wtap_pcap_encap_to_wtap_encap@Base 1.9.1 diff --git a/editcap.c b/editcap.c index 0bcab3308c..a67e6ae747 100644 --- a/editcap.c +++ b/editcap.c @@ -1382,9 +1382,7 @@ main(int argc, char *argv[]) /* If we don't have an application name add Editcap */ wtap_optionblock_get_option_string(shb_hdr, OPT_SHB_USERAPPL, &shb_user_appl); if (shb_user_appl == NULL) { - shb_user_appl = g_strdup("Editcap " VERSION); - wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_USERAPPL, shb_user_appl); - g_free(shb_user_appl); + wtap_optionblock_set_option_string_format(shb_hdr, OPT_SHB_USERAPPL, "Editcap " VERSION); } pdh = editcap_dump_open(filename, diff --git a/epan/wslua/wslua.h b/epan/wslua/wslua.h index fb067a2f38..8e074eb00c 100644 --- a/epan/wslua/wslua.h +++ b/epan/wslua/wslua.h @@ -618,7 +618,8 @@ extern int wslua_set__index(lua_State *L); } else { \ return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \ } \ - wtap_optionblock_set_option_string(obj->member, option, s); \ + wtap_optionblock_set_option_string(obj->member, option, s, strlen(s)); \ + g_free(s); \ return 0; \ } \ /* silly little trick so we can add a semicolon after this macro */ \ diff --git a/tshark.c b/tshark.c index c4ade31a2b..dc28f8769e 100644 --- a/tshark.c +++ b/tshark.c @@ -3272,9 +3272,7 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type, wtap_optionblock_get_option_string(shb_hdr, OPT_SHB_USERAPPL, &shb_user_appl); if (shb_user_appl == NULL) { /* this is free'd by wtap_optionblock_free() later */ - shb_user_appl = g_strdup_printf("TShark (Wireshark) %s", get_ws_vcs_version_info()); - wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_USERAPPL, shb_user_appl); - g_free(shb_user_appl); + wtap_optionblock_set_option_string_format(shb_hdr, OPT_SHB_USERAPPL, "TShark (Wireshark) %s", get_ws_vcs_version_info()); } if (linktype != WTAP_ENCAP_PER_PACKET && diff --git a/ui/gtk/file_import_dlg.c b/ui/gtk/file_import_dlg.c index 5a5816fd27..b39b38fa0a 100644 --- a/ui/gtk/file_import_dlg.c +++ b/ui/gtk/file_import_dlg.c @@ -463,7 +463,7 @@ file_import_open(text_import_info_t *info) wtap_optionblock_t int_data; wtapng_if_descr_mandatory_t *int_data_mand; GString *os_info_str; - gchar *opt_comment, *wireshark_ver; + gsize opt_len; /* Create data for SHB */ os_info_str = g_string_new(""); @@ -472,22 +472,19 @@ file_import_open(text_import_info_t *info) shb_hdr = wtap_optionblock_create(WTAP_OPTION_BLOCK_NG_SECTION); /* options */ - opt_comment = g_strdup_printf("File created by File->Import of file %s", info->import_text_filename); - wtap_optionblock_set_option_string(shb_hdr, OPT_COMMENT, opt_comment); - g_free(opt_comment); + wtap_optionblock_set_option_string_format(shb_hdr, OPT_COMMENT, "File created by File->Import of file %s", info->import_text_filename); /* * UTF-8 string containing the name of the operating system used to create * this section. */ - wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_OS, g_string_free(os_info_str, TRUE)); + opt_len = os_info_str->len; + wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_OS, g_string_free(os_info_str, TRUE), opt_len); /* * UTF-8 string containing the name of the application used to create * this section. */ - wireshark_ver = g_strdup_printf("Wireshark %s", get_ws_vcs_version_info()); - wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_USERAPPL, wireshark_ver); - g_free(wireshark_ver); + wtap_optionblock_set_option_string_format(shb_hdr, OPT_SHB_USERAPPL, "Wireshark %s", get_ws_vcs_version_info()); /* Create fake IDB info */ idb_inf = g_new(wtapng_iface_descriptions_t,1); @@ -500,7 +497,7 @@ file_import_open(text_import_info_t *info) int_data_mand->time_units_per_second = 1000000; /* default microsecond resolution */ int_data_mand->link_type = wtap_wtap_encap_to_pcap_encap(info->encapsulation); int_data_mand->snap_len = WTAP_MAX_PACKET_SIZE; - wtap_optionblock_set_option_string(int_data, OPT_IDB_NAME, "Fake IF File->Import"); + wtap_optionblock_set_option_string(int_data, OPT_IDB_NAME, "Fake IF File->Import", strlen("Fake IF File->Import")); g_array_append_val(idb_inf->interface_data, int_data); diff --git a/ui/tap_export_pdu.c b/ui/tap_export_pdu.c index 3131973ed2..37706ba718 100644 --- a/ui/tap_export_pdu.c +++ b/ui/tap_export_pdu.c @@ -107,7 +107,7 @@ exp_pdu_open(exp_pdu_t *exp_pdu_tap_data, int fd, char *comment) wtap_optionblock_t int_data; wtapng_if_descr_mandatory_t *int_data_mand; GString *os_info_str; - gchar *opt_comment, *wireshark_ver; + gsize opt_len; /* Create data for SHB */ os_info_str = g_string_new(""); @@ -116,22 +116,20 @@ exp_pdu_open(exp_pdu_t *exp_pdu_tap_data, int fd, char *comment) shb_hdr = wtap_optionblock_create(WTAP_OPTION_BLOCK_NG_SECTION); /* options */ - opt_comment = comment; - wtap_optionblock_set_option_string(shb_hdr, OPT_COMMENT, opt_comment); - g_free(opt_comment); + wtap_optionblock_set_option_string(shb_hdr, OPT_COMMENT, comment, strlen(comment)); + g_free(comment); /* * UTF-8 string containing the name of the operating system used to create * this section. */ - wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_OS, g_string_free(os_info_str, TRUE)); + opt_len = os_info_str->len; + wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_OS, g_string_free(os_info_str, TRUE), opt_len); /* * UTF-8 string containing the name of the application used to create * this section. */ - wireshark_ver = g_strdup_printf("Wireshark %s", get_ws_vcs_version_info()); - wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_USERAPPL, wireshark_ver); - g_free(wireshark_ver); + wtap_optionblock_set_option_string_format(shb_hdr, OPT_SHB_USERAPPL, "Wireshark %s", get_ws_vcs_version_info()); /* Create fake IDB info */ idb_inf = g_new(wtapng_iface_descriptions_t,1); @@ -145,7 +143,7 @@ exp_pdu_open(exp_pdu_t *exp_pdu_tap_data, int fd, char *comment) int_data_mand->link_type = wtap_wtap_encap_to_pcap_encap(WTAP_ENCAP_WIRESHARK_UPPER_PDU); int_data_mand->snap_len = WTAP_MAX_PACKET_SIZE; - wtap_optionblock_set_option_string(int_data, OPT_IDB_NAME, "Fake IF, PDU->Export"); + wtap_optionblock_set_option_string(int_data, OPT_IDB_NAME, "Fake IF, PDU->Export", strlen("Fake IF, PDU->Export")); wtap_optionblock_set_option_uint8(int_data, OPT_IDB_TSRESOL, 9); g_array_append_val(idb_inf->interface_data, int_data); diff --git a/wiretap/erf.c b/wiretap/erf.c index beb0fdecea..5ca207ae47 100644 --- a/wiretap/erf.c +++ b/wiretap/erf.c @@ -951,7 +951,6 @@ int erf_populate_interfaces(wtap *wth) wtap_optionblock_t int_data; wtapng_if_descr_mandatory_t* int_data_mand; int i; - char* tmp; if (!wth) return -1; @@ -983,12 +982,8 @@ int erf_populate_interfaces(wtap *wth) int_data_mand->num_stat_entries = 0; int_data_mand->interface_statistics = NULL; - tmp = g_strdup_printf("Port %c", 'A'+i); - wtap_optionblock_set_option_string(int_data, OPT_IDB_NAME, tmp); - g_free(tmp); - tmp = g_strdup_printf("ERF Interface Id %d (Port %c)", i, 'A'+i); - wtap_optionblock_set_option_string(int_data, OPT_IDB_DESCR, tmp); - g_free(tmp); + wtap_optionblock_set_option_string_format(int_data, OPT_IDB_NAME, "Port %c", 'A'+i); + wtap_optionblock_set_option_string_format(int_data, OPT_IDB_DESCR, "ERF Interface Id %d (Port %c)", i, 'A'+i); g_array_append_val(wth->interface_data, int_data); } @@ -1072,7 +1067,7 @@ static struct erf_if_mapping* erf_find_interface_mapping(erf_t *erf_priv, guint6 return (struct erf_if_mapping*) g_hash_table_lookup(erf_priv->if_map, &if_map_lookup); } -static gchar* erf_interface_descr_strdup(guint64 host_id, guint8 source_id, guint8 if_num, const gchar *descr) +static void erf_set_interface_descr(wtap_optionblock_t block, guint option_id, guint64 host_id, guint8 source_id, guint8 if_num, const gchar *descr) { /* Source XXX,*/ char sourceid_buf[16]; @@ -1091,9 +1086,9 @@ static gchar* erf_interface_descr_strdup(guint64 host_id, guint8 source_id, guin } if (descr) { - return g_strdup_printf("%s (ERF%s%s Interface %d)", descr, hostid_buf, sourceid_buf, if_num); + wtap_optionblock_set_option_string_format(block, option_id, "%s (ERF%s%s Interface %d)", descr, hostid_buf, sourceid_buf, if_num); } else { - return g_strdup_printf("Port %c (ERF%s%s Interface %d)", 'A'+if_num, hostid_buf, sourceid_buf, if_num); + wtap_optionblock_set_option_string_format(block, option_id, "Port %c (ERF%s%s Interface %d)", 'A'+if_num, hostid_buf, sourceid_buf, if_num); } } @@ -1105,7 +1100,6 @@ static int erf_update_implicit_host_id(erf_t *erf_priv, wtap *wth, guint64 impli GList* item = NULL; wtap_optionblock_t int_data; struct erf_if_mapping* if_map = NULL; - char* tmp; int i; if (!erf_priv) @@ -1137,12 +1131,8 @@ static int erf_update_implicit_host_id(erf_t *erf_priv, wtap *wth, guint64 impli if (if_map->interfaces[i].if_index >= 0) { /* XXX: this is a pointer! */ int_data = g_array_index(wth->interface_data, wtap_optionblock_t, if_map->interfaces[i].if_index); - tmp = erf_interface_descr_strdup(implicit_host_id, if_map->source_id, (guint8) i, if_map->interfaces[i].name); - wtap_optionblock_set_option_string(int_data, OPT_IDB_NAME, tmp); - g_free(tmp); - tmp = erf_interface_descr_strdup(implicit_host_id, if_map->source_id, (guint8) i, if_map->interfaces[i].descr); - wtap_optionblock_set_option_string(int_data, OPT_IDB_DESCR, tmp); - g_free(tmp); + erf_set_interface_descr(int_data, OPT_IDB_NAME, implicit_host_id, if_map->source_id, (guint8) i, if_map->interfaces[i].name); + erf_set_interface_descr(int_data, OPT_IDB_DESCR, implicit_host_id, if_map->source_id, (guint8) i, if_map->interfaces[i].descr); } } /* Re-add the item under the implicit Host ID */ @@ -1162,7 +1152,6 @@ int erf_populate_interface(erf_t *erf_priv, wtap *wth, union wtap_pseudo_header wtap_optionblock_t int_data; wtapng_if_descr_mandatory_t* int_data_mand; struct erf_if_mapping* if_map = NULL; - char* tmp; if (!wth || !pseudo_header || !erf_priv || if_num > 3) return -1; @@ -1219,12 +1208,8 @@ int erf_populate_interface(erf_t *erf_priv, wtap *wth, union wtap_pseudo_header int_data_mand->num_stat_entries = 0; int_data_mand->interface_statistics = NULL; - tmp = erf_interface_descr_strdup(host_id, source_id, if_num, NULL); - wtap_optionblock_set_option_string(int_data, OPT_IDB_NAME, tmp); - g_free(tmp); - tmp = erf_interface_descr_strdup(host_id, source_id, if_num, NULL); - wtap_optionblock_set_option_string(int_data, OPT_IDB_DESCR, tmp); - g_free(tmp); + erf_set_interface_descr(int_data, OPT_IDB_NAME, host_id, source_id, if_num, NULL); + erf_set_interface_descr(int_data, OPT_IDB_DESCR, host_id, source_id, if_num, NULL); if_map->interfaces[if_num].if_index = (int) wth->interface_data->len; g_array_append_val(wth->interface_data, int_data); @@ -1290,14 +1275,7 @@ static int populate_capture_host_info(erf_t *erf_priv, wtap *wth, union wtap_pse switch (tag.type) { case ERF_META_TAG_comment: - /* - * XXX: Would be really nice if wtap_optionblock_set_option_string() - * supported supplying a length (or didn't strdup), this is all - * through PCAP-NG too. - */ - tmp = g_strndup((gchar*) tag.value, tag.length); - wtap_optionblock_set_option_string(shb_hdr, OPT_COMMENT, tmp); - g_free(tmp); + wtap_optionblock_set_option_string(shb_hdr, OPT_COMMENT, tag.value, tag.length); break; } /* Fall through */ @@ -1322,9 +1300,7 @@ static int populate_capture_host_info(erf_t *erf_priv, wtap *wth, union wtap_pse descr = g_strndup((gchar*) tag.value, tag.length); break; case ERF_META_TAG_os: - tmp = g_strndup((gchar*) tag.value, tag.length); - wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_OS, tmp); - g_free(tmp); + wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_OS, tag.value, tag.length); break; case ERF_META_TAG_app_name: g_free(app_name); @@ -1353,7 +1329,7 @@ static int populate_capture_host_info(erf_t *erf_priv, wtap *wth, union wtap_pse /* If no app_version will just use app_name */ tmp = g_strjoin(" ", app_name, app_version, NULL); - wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_USERAPPL, tmp); + wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_USERAPPL, tmp, strlen(tmp)); g_free(tmp); g_free(app_name); @@ -1382,15 +1358,13 @@ static int populate_capture_host_info(erf_t *erf_priv, wtap *wth, union wtap_pse /* Combine into "Description (Model; CPU)" */ if (state->sectiontype == ERF_META_SECTION_HOST && descr) { if (modelcpu) { - tmp = g_strdup_printf("%s (%s)", descr, modelcpu); - wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_HARDWARE, tmp); - g_free(tmp); + wtap_optionblock_set_option_string_format(shb_hdr, OPT_SHB_HARDWARE, "%s (%s)", descr, modelcpu); } else { - wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_HARDWARE, descr); + wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_HARDWARE, descr, strlen(descr)); /*descr = NULL;*/ } - } else { - wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_HARDWARE, modelcpu); + } else if (modelcpu) { + wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_HARDWARE, modelcpu, strlen(modelcpu)); /*modelcpu = NULL;*/ } @@ -1458,7 +1432,6 @@ static int populate_interface_info(erf_t *erf_priv, wtap *wth, union wtap_pseudo wtap_optionblock_t int_data = NULL; wtapng_if_descr_mandatory_t* int_data_mand = NULL; wtapng_if_descr_filter_t if_filter; - char* tmp; guint32 if_num = 0; struct erf_if_info* if_info = NULL; @@ -1539,30 +1512,22 @@ static int populate_interface_info(erf_t *erf_priv, wtap *wth, union wtap_pseudo /* TODO: fall back to module "dev_name Port N"? */ if (!if_info->name) { if_info->name = g_strndup((gchar*) tag.value, tag.length); - tmp = erf_interface_descr_strdup(state->if_map->host_id, state->if_map->source_id, (guint8) if_num, if_info->name); - wtap_optionblock_set_option_string(int_data, OPT_IDB_NAME, tmp); - g_free(tmp); + erf_set_interface_descr(int_data, OPT_IDB_NAME, state->if_map->host_id, state->if_map->source_id, (guint8) if_num, if_info->name); /* If we have no description, also copy to wtap if_description */ if (!if_info->descr) { - tmp = erf_interface_descr_strdup(state->if_map->host_id, state->if_map->source_id, (guint8) if_num, if_info->name); - wtap_optionblock_set_option_string(int_data, OPT_IDB_DESCR, tmp); - g_free(tmp); + erf_set_interface_descr(int_data, OPT_IDB_DESCR, state->if_map->host_id, state->if_map->source_id, (guint8) if_num, if_info->name); } } break; case ERF_META_TAG_descr: if (!if_info->descr) { if_info->descr = g_strndup((gchar*) tag.value, tag.length); - tmp = erf_interface_descr_strdup(state->if_map->host_id, state->if_map->source_id, (guint8) if_num, if_info->descr); - wtap_optionblock_set_option_string(int_data, OPT_IDB_DESCR, tmp); - g_free(tmp); + erf_set_interface_descr(int_data, OPT_IDB_DESCR, state->if_map->host_id, state->if_map->source_id, (guint8) if_num, if_info->descr); /* If we have no name, also copy to wtap if_name */ if (!if_info->name) { - tmp = erf_interface_descr_strdup(state->if_map->host_id, state->if_map->source_id, (guint8) if_num, if_info->descr); - wtap_optionblock_set_option_string(int_data, OPT_IDB_NAME, tmp); - g_free(tmp); + erf_set_interface_descr(int_data, OPT_IDB_NAME, state->if_map->host_id, state->if_map->source_id, (guint8) if_num, if_info->descr); } } break; @@ -1592,9 +1557,7 @@ static int populate_interface_info(erf_t *erf_priv, wtap *wth, union wtap_pseudo } break; case ERF_META_TAG_comment: - tmp = g_strndup((gchar*) tag.value, tag.length); - wtap_optionblock_set_option_string(int_data, OPT_COMMENT, tmp); - g_free(tmp); + wtap_optionblock_set_option_string(int_data, OPT_COMMENT, tag.value, tag.length); break; case ERF_META_TAG_filter: if_filter.if_filter_str = g_strndup((gchar*) tag.value, tag.length); diff --git a/wiretap/file_access.c b/wiretap/file_access.c index 61653fc311..0f5dbd52c0 100644 --- a/wiretap/file_access.c +++ b/wiretap/file_access.c @@ -2204,7 +2204,8 @@ wtap_dump_init_dumper(int file_type_subtype, int encap, int snaplen, gboolean co descr_mand->time_units_per_second = 1000000; /* default microsecond resolution */ descr_mand->link_type = wtap_wtap_encap_to_pcap_encap(encap); descr_mand->snap_len = snaplen; - wtap_optionblock_set_option_string(descr, OPT_IDB_NAME, "Unknown/not available in original file format(libpcap)"); + wtap_optionblock_set_option_string(descr, OPT_IDB_NAME, "Unknown/not available in original file format(libpcap)", + strlen("Unknown/not available in original file format(libpcap)")); descr_mand->num_stat_entries = 0; /* Number of ISB:s */ descr_mand->interface_statistics = NULL; diff --git a/wiretap/lanalyzer.c b/wiretap/lanalyzer.c index 72ebba82e3..eb4ad3815e 100644 --- a/wiretap/lanalyzer.c +++ b/wiretap/lanalyzer.c @@ -323,12 +323,15 @@ wtap_open_return_val lanalyzer_open(wtap *wth, int *err, gchar **err_info) comment = (char *)g_malloc(record_length + 1); if (!wtap_read_bytes(wth->fh, comment, record_length, err, err_info)) { - if (*err != WTAP_ERR_SHORT_READ) - return WTAP_OPEN_ERROR; + if (*err != WTAP_ERR_SHORT_READ) { + g_free(comment); + return WTAP_OPEN_ERROR; + } + g_free(comment); return WTAP_OPEN_NOT_MINE; } - comment[record_length] = '\0'; - wtap_optionblock_set_option_string(wth->shb_hdr, OPT_COMMENT, comment); + wtap_optionblock_set_option_string(wth->shb_hdr, OPT_COMMENT, comment, record_length); + g_free(comment); } /* If we made it this far, then the file is a LANAlyzer file. diff --git a/wiretap/merge.c b/wiretap/merge.c index 83ad2f35e6..52709c8948 100644 --- a/wiretap/merge.c +++ b/wiretap/merge.c @@ -370,6 +370,7 @@ create_shb_header(const merge_in_file_t *in_files, const guint in_file_count, guint i; char* shb_comment = NULL; wtapng_mandatory_section_t* shb_data; + gsize opt_len; shb_hdr = wtap_file_get_shb_for_new_file(in_files[0].wth); @@ -396,13 +397,15 @@ create_shb_header(const merge_in_file_t *in_files, const guint in_file_count, shb_data = (wtapng_mandatory_section_t*)wtap_optionblock_get_mandatory_data(shb_hdr); shb_data->section_length = -1; /* TODO: handle comments from each file being merged */ - wtap_optionblock_set_option_string(shb_hdr, OPT_COMMENT, g_string_free(comment_gstr, TRUE)); /* section comment */ - wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_HARDWARE, NULL ); /* NULL if not available, UTF-8 string containing the */ + opt_len = comment_gstr->len; + wtap_optionblock_set_option_string(shb_hdr, OPT_COMMENT, g_string_free(comment_gstr, TRUE), opt_len); /* section comment */ + wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_HARDWARE, NULL, 0 ); /* NULL if not available, UTF-8 string containing the */ /* description of the hardware used to create this section. */ - wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_OS, g_string_free(os_info_str, TRUE)); /* UTF-8 string containing the name */ + opt_len = os_info_str->len; + wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_OS, g_string_free(os_info_str, TRUE), opt_len); /* UTF-8 string containing the name */ /* of the operating system used to create this section. */ - wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_USERAPPL, (char*)app_name ); /* NULL if not available, UTF-8 string containing the name */ + wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_USERAPPL, (char*)app_name, app_name ? strlen(app_name): 0 ); /* NULL if not available, UTF-8 string containing the name */ /* of the application used to create this section. */ return shb_hdr; diff --git a/wiretap/nettrace_3gpp_32_423.c b/wiretap/nettrace_3gpp_32_423.c index a93ff1d9ac..9fd47fbc1d 100644 --- a/wiretap/nettrace_3gpp_32_423.c +++ b/wiretap/nettrace_3gpp_32_423.c @@ -214,10 +214,10 @@ nettrace_close(wtap *wth) wtap_close(file_info->wth_tmp_file); /*Clear the shb info, it's been freed by wtap_close*/ - wtap_optionblock_set_option_string(wth->shb_hdr, OPT_COMMENT, NULL); - wtap_optionblock_set_option_string(wth->shb_hdr, OPT_SHB_HARDWARE, NULL); - wtap_optionblock_set_option_string(wth->shb_hdr, OPT_SHB_OS, NULL); - wtap_optionblock_set_option_string(wth->shb_hdr, OPT_SHB_USERAPPL, NULL); + wtap_optionblock_set_option_string(wth->shb_hdr, OPT_COMMENT, NULL, 0); + wtap_optionblock_set_option_string(wth->shb_hdr, OPT_SHB_HARDWARE, NULL, 0); + wtap_optionblock_set_option_string(wth->shb_hdr, OPT_SHB_OS, NULL, 0); + wtap_optionblock_set_option_string(wth->shb_hdr, OPT_SHB_USERAPPL, NULL, 0); /* delete the temp file */ ws_unlink(file_info->tmpname); @@ -730,10 +730,10 @@ create_temp_pcapng_file(wtap *wth, int *err, gchar **err_info, nettrace_3gpp_32_ int scan_found; unsigned second, ms; gboolean do_random = FALSE; - gchar* wireshark_ver; char *curr_pos, *next_msg_pos, *next_pos, *prev_pos; int name_str_len; char name_str[64]; + gsize opt_len; /* Info to build exported_pdu tags*/ exported_pdu_info_t exported_pdu_info; @@ -760,20 +760,20 @@ create_temp_pcapng_file(wtap *wth, int *err, gchar **err_info, nettrace_3gpp_32_ shb_hdr = wtap_optionblock_create(WTAP_OPTION_BLOCK_NG_SECTION); /* options */ - wtap_optionblock_set_option_string(wth->shb_hdr, OPT_COMMENT, "File converted to Exported PDU format during opening"); + wtap_optionblock_set_option_string(wth->shb_hdr, OPT_COMMENT, "File converted to Exported PDU format during opening", + strlen("File converted to Exported PDU format during opening")); /* * UTF-8 string containing the name of the operating system used to create * this section. */ - wtap_optionblock_set_option_string(wth->shb_hdr, OPT_SHB_OS, g_string_free(os_info_str, TRUE)); + opt_len = os_info_str->len; + wtap_optionblock_set_option_string(wth->shb_hdr, OPT_SHB_OS, g_string_free(os_info_str, TRUE), opt_len); /* * UTF-8 string containing the name of the application used to create * this section. */ - wireshark_ver = g_strdup_printf("Wireshark %s", get_ws_vcs_version_info()); - wtap_optionblock_set_option_string(wth->shb_hdr, OPT_SHB_USERAPPL, wireshark_ver); - g_free(wireshark_ver); + wtap_optionblock_set_option_string_format(wth->shb_hdr, OPT_SHB_USERAPPL, "Wireshark %s", get_ws_vcs_version_info()); /* Create fake IDB info */ idb_inf = g_new(wtapng_iface_descriptions_t, 1); @@ -786,7 +786,7 @@ create_temp_pcapng_file(wtap *wth, int *err, gchar **err_info, nettrace_3gpp_32_ int_data_mand->time_units_per_second = 1000000; /* default microsecond resolution */ int_data_mand->link_type = wtap_wtap_encap_to_pcap_encap(WTAP_ENCAP_WIRESHARK_UPPER_PDU); int_data_mand->snap_len = WTAP_MAX_PACKET_SIZE; - wtap_optionblock_set_option_string(int_data, OPT_IDB_NAME, "Fake IF"); + wtap_optionblock_set_option_string(int_data, OPT_IDB_NAME, "Fake IF", strlen("Fake IF")); int_data_mand->num_stat_entries = 0; /* Number of ISB:s */ int_data_mand->interface_statistics = NULL; diff --git a/wiretap/pcapng.c b/wiretap/pcapng.c index 3ebbfcaf1c..fa483367d1 100644 --- a/wiretap/pcapng.c +++ b/wiretap/pcapng.c @@ -620,7 +620,7 @@ pcapng_read_section_header_block(FILE_T fh, pcapng_block_header_t *bh, case(OPT_COMMENT): if (oh.option_length > 0 && oh.option_length < opt_cont_buf_len) { tmp_content = g_strndup((char *)option_content, oh.option_length); - wtap_optionblock_set_option_string(wblock->block, OPT_COMMENT, tmp_content); + wtap_optionblock_set_option_string(wblock->block, OPT_COMMENT, option_content, oh.option_length); pcapng_debug("pcapng_read_section_header_block: opt_comment %s", tmp_content); g_free(tmp_content); } else { @@ -630,7 +630,7 @@ pcapng_read_section_header_block(FILE_T fh, pcapng_block_header_t *bh, case(OPT_SHB_HARDWARE): if (oh.option_length > 0 && oh.option_length < opt_cont_buf_len) { tmp_content = g_strndup((char *)option_content, oh.option_length); - wtap_optionblock_set_option_string(wblock->block, OPT_SHB_HARDWARE, tmp_content); + wtap_optionblock_set_option_string(wblock->block, OPT_SHB_HARDWARE, option_content, oh.option_length); pcapng_debug("pcapng_read_section_header_block: shb_hardware %s", tmp_content); g_free(tmp_content); } else { @@ -640,7 +640,7 @@ pcapng_read_section_header_block(FILE_T fh, pcapng_block_header_t *bh, case(OPT_SHB_OS): if (oh.option_length > 0 && oh.option_length < opt_cont_buf_len) { tmp_content = g_strndup((char *)option_content, oh.option_length); - wtap_optionblock_set_option_string(wblock->block, OPT_SHB_OS, tmp_content); + wtap_optionblock_set_option_string(wblock->block, OPT_SHB_OS, option_content, oh.option_length); pcapng_debug("pcapng_read_section_header_block: shb_os %s", tmp_content); g_free(tmp_content); } else { @@ -650,7 +650,7 @@ pcapng_read_section_header_block(FILE_T fh, pcapng_block_header_t *bh, case(OPT_SHB_USERAPPL): if (oh.option_length > 0 && oh.option_length < opt_cont_buf_len) { tmp_content = g_strndup((char *)option_content, oh.option_length); - wtap_optionblock_set_option_string(wblock->block, OPT_SHB_USERAPPL, tmp_content); + wtap_optionblock_set_option_string(wblock->block, OPT_SHB_USERAPPL, option_content, oh.option_length); pcapng_debug("pcapng_read_section_header_block: shb_user_appl %s", tmp_content); g_free(tmp_content); } else { @@ -781,7 +781,7 @@ pcapng_read_if_descr_block(wtap *wth, FILE_T fh, pcapng_block_header_t *bh, case(OPT_COMMENT): /* opt_comment */ if (oh.option_length > 0 && oh.option_length < opt_cont_buf_len) { tmp_content = g_strndup((char *)option_content, oh.option_length); - wtap_optionblock_set_option_string(wblock->block, OPT_COMMENT, tmp_content); + wtap_optionblock_set_option_string(wblock->block, OPT_COMMENT, option_content, oh.option_length); pcapng_debug("pcapng_read_if_descr_block: opt_comment %s", tmp_content); g_free(tmp_content); } else { @@ -791,7 +791,7 @@ pcapng_read_if_descr_block(wtap *wth, FILE_T fh, pcapng_block_header_t *bh, case(OPT_IDB_NAME): /* if_name */ if (oh.option_length > 0 && oh.option_length < opt_cont_buf_len) { tmp_content = g_strndup((char *)option_content, oh.option_length); - wtap_optionblock_set_option_string(wblock->block, OPT_IDB_NAME, tmp_content); + wtap_optionblock_set_option_string(wblock->block, OPT_IDB_NAME, option_content, oh.option_length); pcapng_debug("pcapng_read_if_descr_block: if_name %s", tmp_content); g_free(tmp_content); } else { @@ -801,7 +801,7 @@ pcapng_read_if_descr_block(wtap *wth, FILE_T fh, pcapng_block_header_t *bh, case(OPT_IDB_DESCR): /* if_description */ if (oh.option_length > 0 && oh.option_length < opt_cont_buf_len) { tmp_content = g_strndup((char *)option_content, oh.option_length); - wtap_optionblock_set_option_string(wblock->block, OPT_IDB_DESCR, tmp_content); + wtap_optionblock_set_option_string(wblock->block, OPT_IDB_DESCR, option_content, oh.option_length); pcapng_debug("pcapng_read_if_descr_block: if_description %s", tmp_content); g_free(tmp_content); } else { @@ -900,7 +900,7 @@ pcapng_read_if_descr_block(wtap *wth, FILE_T fh, pcapng_block_header_t *bh, */ if (oh.option_length > 0 && oh.option_length < opt_cont_buf_len) { tmp_content = g_strndup((char *)option_content, oh.option_length); - wtap_optionblock_set_option_string(wblock->block, OPT_IDB_OS, tmp_content); + wtap_optionblock_set_option_string(wblock->block, OPT_IDB_OS, option_content, oh.option_length); pcapng_debug("pcapng_read_if_descr_block: if_os %s", tmp_content); g_free(tmp_content); } else { @@ -1847,7 +1847,7 @@ read_options: case(OPT_COMMENT): if (oh.option_length > 0 && oh.option_length < opt_cont_buf_len) { tmp_content = g_strndup((char *)option_content, oh.option_length); - wtap_optionblock_set_option_string(wblock->block, OPT_COMMENT, tmp_content); + wtap_optionblock_set_option_string(wblock->block, OPT_COMMENT, option_content, oh.option_length); pcapng_debug("pcapng_read_name_resolution_block: length %u opt_comment '%s'", oh.option_length, tmp_content); g_free(tmp_content); } else { @@ -1975,9 +1975,9 @@ pcapng_read_interface_statistics_block(FILE_T fh, pcapng_block_header_t *bh, pca case(OPT_COMMENT): /* opt_comment */ if (oh.option_length > 0 && oh.option_length < opt_cont_buf_len) { tmp_content = g_strndup((char *)option_content, oh.option_length); - wtap_optionblock_set_option_string(wblock->block, OPT_COMMENT, tmp_content); - g_free(tmp_content); + wtap_optionblock_set_option_string(wblock->block, OPT_COMMENT, option_content, oh.option_length); pcapng_debug("pcapng_read_interface_statistics_block: opt_comment %s", tmp_content); + g_free(tmp_content); } else { pcapng_debug("pcapng_read_interface_statistics_block: opt_comment length %u seems strange", oh.option_length); } diff --git a/wiretap/wtap.c b/wiretap/wtap.c index 30f4123fe0..bdc74adcbf 100644 --- a/wiretap/wtap.c +++ b/wiretap/wtap.c @@ -191,7 +191,7 @@ wtap_file_get_shb_for_new_file(wtap *wth) /* options */ wtap_optionblock_get_option_string(wth->shb_hdr, OPT_COMMENT, &opt_comment); - wtap_optionblock_set_option_string(shb_hdr, OPT_COMMENT, opt_comment); + wtap_optionblock_set_option_string(shb_hdr, OPT_COMMENT, opt_comment, (gsize)(opt_comment ? strlen(opt_comment) : 0)); return shb_hdr; } @@ -221,13 +221,13 @@ wtap_write_nrb_comment(wtap *wth, gchar *comment) wth->nrb_hdr = wtap_optionblock_create(WTAP_OPTION_BLOCK_NG_NRB); } - wtap_optionblock_set_option_string(wth->nrb_hdr, OPT_COMMENT, comment); + wtap_optionblock_set_option_string(wth->nrb_hdr, OPT_COMMENT, comment, (gsize)(comment ? strlen(comment) : 0)); } void wtap_write_shb_comment(wtap *wth, gchar *comment) { - wtap_optionblock_set_option_string(wth->shb_hdr, OPT_COMMENT, comment); + wtap_optionblock_set_option_string(wth->shb_hdr, OPT_COMMENT, comment, (gsize)(comment ? strlen(comment) : 0)); } wtapng_iface_descriptions_t * diff --git a/wiretap/wtap_opttypes.c b/wiretap/wtap_opttypes.c index 7ae09a3669..9c53b48a9d 100644 --- a/wiretap/wtap_opttypes.c +++ b/wiretap/wtap_opttypes.c @@ -389,7 +389,7 @@ int wtap_optionblock_add_option(wtap_optionblock_t block, guint option_id, wtap_ return WTAP_OPTTYPE_SUCCESS; } -int wtap_optionblock_set_option_string(wtap_optionblock_t block, guint option_id, char* value) +int wtap_optionblock_set_option_string(wtap_optionblock_t block, guint option_id, char* value, gsize value_length) { wtap_optblock_value_t* opt_value = wtap_optionblock_get_option(block, option_id); @@ -401,7 +401,26 @@ int wtap_optionblock_set_option_string(wtap_optionblock_t block, guint option_id return WTAP_OPTTYPE_TYPE_MISMATCH; g_free(opt_value->option.stringval); - opt_value->option.stringval = g_strdup(value); + opt_value->option.stringval = g_strndup(value, value_length); + return WTAP_OPTTYPE_SUCCESS; +} + +int wtap_optionblock_set_option_string_format(wtap_optionblock_t block, guint option_id, const char *format, ...) +{ + va_list va; + wtap_optblock_value_t* opt_value = wtap_optionblock_get_option(block, option_id); + + /* Didn't find the option */ + if (opt_value == NULL) + return WTAP_OPTTYPE_NOT_FOUND; + + if (opt_value->info->type != WTAP_OPTTYPE_STRING) + return WTAP_OPTTYPE_TYPE_MISMATCH; + + g_free(opt_value->option.stringval); + va_start(va, format); + opt_value->option.stringval = g_strdup_vprintf(format, va); + va_end(va); return WTAP_OPTTYPE_SUCCESS; } diff --git a/wiretap/wtap_opttypes.h b/wiretap/wtap_opttypes.h index 410dcb2e32..56d0c29e19 100644 --- a/wiretap/wtap_opttypes.h +++ b/wiretap/wtap_opttypes.h @@ -133,9 +133,20 @@ int wtap_optionblock_add_option(wtap_optionblock_t block, guint option_id, wtap_ * @param[in] block Block to add option * @param[in] option_id Identifier value for option * @param[in] value New value of option + * @param[in] value_length Maximum length of string to copy. * @return 0 if successful */ -WS_DLL_PUBLIC int wtap_optionblock_set_option_string(wtap_optionblock_t block, guint option_id, char* value); +WS_DLL_PUBLIC int wtap_optionblock_set_option_string(wtap_optionblock_t block, guint option_id, char* value, gsize value_length); + +/** Set printf-styled string option value to an option block + * + * @param[in] block Block to add option + * @param[in] option_id Identifier value for option + * @param[in] format printf like format string + * @return 0 if successful + */ +WS_DLL_PUBLIC int wtap_optionblock_set_option_string_format(wtap_optionblock_t block, guint option_id, const char *format, ...) + G_GNUC_PRINTF(3,4); /** Get string option value from an option block *