Wireshark-dev: [Wireshark-dev] Try to use non formatting routines
From: Jakub Zawadzki <[email protected]>
Date: Wed, 11 Mar 2009 01:34:58 +0100
Hi,

Another patch, when we can call (and it won't broke code-look), use
 g_strdup() instead of g_strdup_printf()
 g_strlcpy() instead of g_snprintf()
 g_string_append() instead of g_string_append_printf()

For now it's against gtk/
If it's ok, I'll prepare patch against rest source.
Index: gtk/graph_analysis.c
===================================================================
--- gtk/graph_analysis.c	(wersja 27690)
+++ gtk/graph_analysis.c	(kopia robocza)
@@ -810,8 +810,7 @@
 	for (i=0; i<user_data->num_nodes; i++){
 		/* print the node identifiers */
 		/* XXX we assign 5 pixels per character in the node identity */
-		g_snprintf(label_string, NODE_WIDTH/5, "%s",
-			get_addr_name(&(user_data->nodes[i])));
+		g_strlcpy(label_string, get_addr_name(&(user_data->nodes[i])), NODE_WIDTH/5);
 		pango_layout_set_text(layout, label_string, -1);
 		pango_layout_get_pixel_size(layout, &label_width, &label_height);
 		if (GDK_IS_DRAWABLE(user_data->dlg.pixmap)) {
Index: gtk/expert_comp_table.c
===================================================================
--- gtk/expert_comp_table.c	(wersja 27690)
+++ gtk/expert_comp_table.c	(kopia robocza)
@@ -164,12 +164,12 @@
         case ACTYPE_SELECTED:
             /* if no expert item was passed */
             if (err->procedures[selection].fvalue_value==NULL) {
-                g_snprintf(str, 255, "%s", err->procedures[selection].entries[2]);
+                g_strlcpy(str, err->procedures[selection].entries[2], 256);
             }
             else
             {
                 /* expert item exists. Use it. */
-                g_snprintf(str, 255, "%s", err->procedures[selection].fvalue_value);
+                g_strlcpy(str, err->procedures[selection].fvalue_value, 256);
             }
             break;
         case ACTYPE_NOT_SELECTED:
@@ -186,13 +186,13 @@
             /* the remaining cases will only exist if the expert item exists so no need to check */
         case ACTYPE_AND_SELECTED:
             if ((!current_filter) || (0 == strlen(current_filter)))
-                g_snprintf(str, 255, "%s", err->procedures[selection].fvalue_value);
+                g_strlcpy(str, err->procedures[selection].fvalue_value, 256);
             else
                 g_snprintf(str, 255, "(%s) && (%s)", current_filter, err->procedures[selection].fvalue_value);
             break;
         case ACTYPE_OR_SELECTED:
             if ((!current_filter) || (0 == strlen(current_filter)))
-                g_snprintf(str, 255, "%s", err->procedures[selection].fvalue_value);
+                g_strlcpy(str, err->procedures[selection].fvalue_value, 256);
             else
                 g_snprintf(str, 255, "(%s) || (%s)", current_filter, err->procedures[selection].fvalue_value);
             break;
Index: gtk/main_welcome.c
===================================================================
--- gtk/main_welcome.c	(wersja 27690)
+++ gtk/main_welcome.c	(kopia robocza)
@@ -740,7 +740,7 @@
         GTK_SIGNAL_FUNC(capture_if_cb), NULL);
     gtk_box_pack_start(GTK_BOX(topic_to_fill), item_hb, FALSE, FALSE, 5);
 
-    label_text =  g_strdup_printf("<span foreground=\"black\">Start capture on interface:</span>");
+    label_text =  g_strdup("<span foreground=\"black\">Start capture on interface:</span>");
     w = gtk_label_new(label_text);
     gtk_label_set_markup(GTK_LABEL(w), label_text);
     g_free (label_text);
@@ -779,7 +779,7 @@
         GTK_SIGNAL_FUNC(topic_menu_cb), GINT_TO_POINTER(ONLINEPAGE_NETWORK_MEDIA));
     gtk_box_pack_start(GTK_BOX(topic_to_fill), item_hb, FALSE, FALSE, 5);
 #else
-    label_text =  g_strdup_printf("<span foreground=\"black\">Capturing is not compiled into this version of Wireshark!</span>");
+    label_text =  g_strdup("<span foreground=\"black\">Capturing is not compiled into this version of Wireshark!</span>");
     w = gtk_label_new(label_text);
     gtk_label_set_markup(GTK_LABEL(w), label_text);
     g_free (label_text);
@@ -804,7 +804,7 @@
     gtk_box_pack_start(GTK_BOX(topic_to_fill), item_hb, FALSE, FALSE, 5);
 
     /* prepare list of recent files (will be filled in later) */
-    label_text =  g_strdup_printf("<span foreground=\"black\">Open Recent:</span>");
+    label_text =  g_strdup("<span foreground=\"black\">Open Recent:</span>");
     w = gtk_label_new(label_text);
     gtk_label_set_markup(GTK_LABEL(w), label_text);
     g_free (label_text);
@@ -864,7 +864,7 @@
     topic_vb = welcome_topic_new("Updates", &topic_to_fill);
     gtk_box_pack_start(GTK_BOX(column_vb), topic_vb, TRUE, TRUE, 0);
 
-    label_text =  g_strdup_printf("<span foreground=\"black\">No updates available!</span>");
+    label_text =  g_strdup("<span foreground=\"black\">No updates available!</span>");
     w = gtk_label_new(label_text);
     gtk_label_set_markup(GTK_LABEL(w), label_text);
     g_free (label_text);
Index: gtk/capture_file_dlg.c
===================================================================
--- gtk/capture_file_dlg.c	(wersja 27690)
+++ gtk/capture_file_dlg.c	(kopia robocza)
@@ -187,7 +187,7 @@
     gtk_label_set_text(GTK_LABEL(label), string_buff);
 
     /* type */
-    g_snprintf(string_buff, PREVIEW_STR_MAX, "%s", wtap_file_type_string(wtap_file_type(wth)));
+    g_strlcpy(string_buff, wtap_file_type_string(wtap_file_type(wth)), PREVIEW_STR_MAX);
     label = g_object_get_data(G_OBJECT(prev), PREVIEW_FORMAT_KEY);
     gtk_label_set_text(GTK_LABEL(label), string_buff);
 
Index: gtk/airpcap_dlg.c
===================================================================
--- gtk/airpcap_dlg.c	(wersja 27690)
+++ gtk/airpcap_dlg.c	(kopia robocza)
@@ -2142,11 +2142,11 @@
     if (airpcap_if_selected != NULL)
     {
 		if (airpcap_if_selected->linkType == AIRPCAP_LT_802_11){
-			capture_s = g_strdup_printf("%s",AIRPCAP_LINK_TYPE_NAME_802_11_ONLY);
+			capture_s = g_strdup(AIRPCAP_LINK_TYPE_NAME_802_11_ONLY);
 		}else if (airpcap_if_selected->linkType == AIRPCAP_LT_802_11_PLUS_RADIO){
-			capture_s = g_strdup_printf("%s",AIRPCAP_LINK_TYPE_NAME_802_11_PLUS_RADIO);
+			capture_s = g_strdup(AIRPCAP_LINK_TYPE_NAME_802_11_PLUS_RADIO);
 		}else if (airpcap_if_selected->linkType == AIRPCAP_LT_802_11_PLUS_PPI){
-			capture_s = g_strdup_printf("%s",AIRPCAP_LINK_TYPE_NAME_802_11_PLUS_PPI);
+			capture_s = g_strdup(AIRPCAP_LINK_TYPE_NAME_802_11_PLUS_PPI);
 		}
 
         if (capture_s != NULL) gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(capture_type_cm)->entry), capture_s);
Index: gtk/voip_calls.c
===================================================================
--- gtk/voip_calls.c	(wersja 27690)
+++ gtk/voip_calls.c	(kopia robocza)
@@ -940,7 +940,7 @@
 
 		if (pi->request_method == NULL){
 			frame_label = g_strdup_printf("%u %s", pi->response_code, pi->reason_phrase );
-			comment = g_strdup_printf("SIP Status");
+			comment = g_strdup("SIP Status");
 
 			if ((tmp_sipinfo && pi->tap_cseq_number == tmp_sipinfo->invite_cseq)&&(ADDRESSES_EQUAL(&tmp_dst,&(callsinfo->initial_speaker)))){
 				if ((pi->response_code > 199) && (pi->response_code<300) && (tmp_sipinfo->sip_state == SIP_INVITE_SENT)){
@@ -965,20 +965,20 @@
 				&&(ADDRESSES_EQUAL(&tmp_src,&(callsinfo->initial_speaker)))&&(tmp_sipinfo->sip_state==SIP_200_REC)
 				&&(callsinfo->call_state == VOIP_CALL_SETUP)){
 				callsinfo->call_state = VOIP_IN_CALL;
-				comment = g_strdup_printf("SIP Request");
+				comment = g_strdup("SIP Request");
 			}
 			else if (strcmp(pi->request_method,"BYE")==0){
 				callsinfo->call_state = VOIP_COMPLETED;
 				tapinfo->completed_calls++;
-				comment = g_strdup_printf("SIP Request");
+				comment = g_strdup("SIP Request");
 			}
 			else if ((strcmp(pi->request_method,"CANCEL")==0)&&(pi->tap_cseq_number == tmp_sipinfo->invite_cseq)
 				&&(ADDRESSES_EQUAL(&tmp_src,&(callsinfo->initial_speaker)))&&(callsinfo->call_state==VOIP_CALL_SETUP)){
 				callsinfo->call_state = VOIP_CANCELLED;
 				tmp_sipinfo->sip_state = SIP_CANCEL_SENT;
-				comment = g_strdup_printf("SIP Request");
+				comment = g_strdup("SIP Request");
 			} else {
-				comment = g_strdup_printf("SIP Request");
+				comment = g_strdup("SIP Request");
 			}
 		}
 
@@ -1174,7 +1174,7 @@
 			frame_label = g_strdup(isup_message_type_value_acro[i].strptr);
 		}
 		else{
-			frame_label = g_strdup_printf("Unknown");
+			frame_label = g_strdup("Unknown");
 		}
 
 		if (callsinfo->npackets == 1){ /* this is the first packet, that must be an IAM */
@@ -1917,7 +1917,7 @@
 			default:
 				comment = g_strdup("H225 RAS");
 			}
-			frame_label = g_strdup_printf("%s", val_to_str(pi->msg_tag, h225_RasMessage_vals, "<unknown>"));
+			frame_label = g_strdup(val_to_str(pi->msg_tag, h225_RasMessage_vals, "<unknown>"));
 		} else {
 			frame_label = g_strdup("H225: Unknown");
 			comment = NULL;
@@ -2499,7 +2499,7 @@
 			}
 		}
 
-		if (frame_label == NULL) frame_label = g_strdup_printf("%s",pi->code);
+		if (frame_label == NULL) frame_label = g_strdup(pi->code);
 		break;
 	case MGCP_RESPONSE:
 		frame_label = g_strdup_printf("%u (%s)",pi->rspcode, pi->code);
@@ -3144,7 +3144,7 @@
 				callsinfo->call_active_state = VOIP_ACTIVE;
 				callsinfo->call_state = VOIP_CALL_SETUP;
 				callsinfo->from_identity=g_strdup_printf("%x",pi->termid);
-				callsinfo->to_identity=g_strdup_printf("UNKNOWN");
+				callsinfo->to_identity=g_strdup("UNKNOWN");
 				COPY_ADDRESS(&(callsinfo->initial_speaker),&(pinfo->src));
 				callsinfo->first_frame_num=pinfo->fd->num;
 				callsinfo->selected=FALSE;
@@ -3221,9 +3221,9 @@
 
 					/* Create new string */
 					if(pi->key_val == 10) {
-						tmp_unistim_info->key_buffer = g_strdup_printf("*");
+						tmp_unistim_info->key_buffer = g_strdup("*");
 					} else if(pi->key_val == 11) {
-						tmp_unistim_info->key_buffer = g_strdup_printf("#");
+						tmp_unistim_info->key_buffer = g_strdup("#");
 					} else {
 						tmp_unistim_info->key_buffer = g_strdup_printf("%d",pi->key_val);
 					}
@@ -3256,7 +3256,7 @@
 					g_string_truncate(g_tmp,g_tmp->len-1);
 
 					/* Insert new data */
-					tmp_unistim_info->key_buffer = g_strdup_printf("%s",g_tmp->str);
+					tmp_unistim_info->key_buffer = g_strdup(g_tmp->str);
 				}
 
 				/* Set label and comment for graph */
@@ -3279,7 +3279,7 @@
 					g_string_truncate(g_tmp,g_tmp->len-1);
 
 					/* Insert new data */
-					tmp_unistim_info->key_buffer = g_strdup_printf("%s",g_tmp->str);
+					tmp_unistim_info->key_buffer = g_strdup(g_tmp->str);
 				}
 
 				/* add label and comment */
@@ -3291,7 +3291,7 @@
 				/* User pressed the soft key 3 */
 				/* Cancel on cs2k so clear buffer */
 				/* On mcs its config which will clear the buffer too */
-				tmp_unistim_info->key_buffer = g_strdup_printf("\n");
+				tmp_unistim_info->key_buffer = g_strdup("\n");
 
 				/* User pressed something, set labels*/
 				comment = g_strdup_printf("Key Input Sent: S3 (%d)", pi->sequence);
@@ -3395,8 +3395,8 @@
 			callsinfo = g_malloc0(sizeof(voip_calls_info_t));
 			callsinfo->call_active_state = VOIP_ACTIVE;
 			callsinfo->call_state = VOIP_CALL_SETUP;
-			callsinfo->from_identity=g_strdup_printf("UNKNOWN");
-			callsinfo->to_identity=g_strdup_printf("UNKNOWN");
+			callsinfo->from_identity=g_strdup("UNKNOWN");
+			callsinfo->to_identity=g_strdup("UNKNOWN");
 			COPY_ADDRESS(&(callsinfo->initial_speaker),&(pinfo->src));
 			callsinfo->first_frame_num=pinfo->fd->num;
 			callsinfo->selected=FALSE;
Index: gtk/main.c
===================================================================
--- gtk/main.c	(wersja 27690)
+++ gtk/main.c	(kopia robocza)
@@ -290,7 +290,7 @@
     /* Don't change the current display filter if we only want to copy the filter */
     if (action&MATCH_SELECTED_COPY_ONLY) {
         GString *gtk_text_str = g_string_new("");
-        g_string_append_printf(gtk_text_str, "%s", new_filter);
+        g_string_append(gtk_text_str, new_filter);
         copy_to_clipboard(gtk_text_str);
         g_string_free(gtk_text_str, TRUE);
     } else {
@@ -531,13 +531,13 @@
     char *stringpointer = labelstring;
 
     if (cfile.finfo_selected->rep->representation != 0) {
-        g_string_append_printf(gtk_text_str, "%s", cfile.finfo_selected->rep->representation);   /* Get the represented data */
+        g_string_append(gtk_text_str, cfile.finfo_selected->rep->representation);   /* Get the represented data */
     }
-    if (gtk_text_str->len == 0) {                                                           /* If no representation then... */
-        proto_item_fill_label(cfile.finfo_selected, stringpointer);                         /* Try to read the value */
-        g_string_append_printf(gtk_text_str, "%s", stringpointer);
+    if (gtk_text_str->len == 0) {                                                   /* If no representation then... */
+        proto_item_fill_label(cfile.finfo_selected, stringpointer);                 /* Try to read the value */
+        g_string_append(gtk_text_str, stringpointer);
     }
-    if (gtk_text_str->len == 0) {                                                           /* Could not get item so display error msg */
+    if (gtk_text_str->len == 0) {                                                   /* Could not get item so display error msg */
         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not acquire information to copy, try expanding or choosing another item");
     }
     else
Index: gtk/rtp_analysis.c
===================================================================
--- gtk/rtp_analysis.c	(wersja 27690)
+++ gtk/rtp_analysis.c	(kopia robocza)
@@ -2910,7 +2910,7 @@
 
 	 switch(bool_col){
 		 case MARKER_COLUMN:
-			 g_snprintf(buf, sizeof(buf), "%s", bool_val? "SET" : "");
+			 g_strlcpy(buf, bool_val ? "SET" : "", sizeof(buf));
 			 break;
 		 default:
 			 g_assert_not_reached();
Index: gtk/wlan_stat_dlg.c
===================================================================
--- gtk/wlan_stat_dlg.c	(wersja 27690)
+++ gtk/wlan_stat_dlg.c	(kopia robocza)
@@ -493,14 +493,14 @@
 		}
 
 		if (hs->resolve_names) {
-			g_snprintf (address, sizeof(address), "%s", get_addr_name(&tmp->address));
+			g_strlcpy (address, get_addr_name(&tmp->address), sizeof(address));
 		} else {
-			g_snprintf (address, sizeof(address), "%s", address_to_str(&tmp->address));
+			g_strlcpy (address, address_to_str(&tmp->address), sizeof(address));
 		}
 		if (basestation) {
-			g_snprintf (comment, sizeof(comment), "Base station");
+			g_strlcpy (comment, "Base station", sizeof(comment));
 		} else {
-			g_snprintf (comment, sizeof(comment), " ");
+			g_strlcpy (comment, " ", sizeof(comment));
 		}
 		g_snprintf (percent, sizeof(percent), "%.2f %%", f);
 
@@ -562,9 +562,9 @@
 		f = (float)(((float)tmp->number_of_packets * 100.0) / hs->number_of_packets);
 
 		if (hs->resolve_names) {
-			g_snprintf (bssid, sizeof(bssid), "%s", get_addr_name(&tmp->bssid));
+			g_strlcpy (bssid, get_addr_name(&tmp->bssid), sizeof(bssid));
 		} else {
-			g_snprintf (bssid, sizeof(bssid), "%s", address_to_str(&tmp->bssid));
+			g_strlcpy (bssid, address_to_str(&tmp->bssid), sizeof(bssid));
 		}
 		if (tmp->stats.channel) {
 			g_snprintf (channel, sizeof(channel), "%u", tmp->stats.channel);
@@ -572,11 +572,11 @@
 			channel[0] = '\0';
 		}
 		if (tmp->stats.ssid_len == 0) {
-			g_snprintf (ssid, sizeof(ssid), "<Broadcast>");
+			g_strlcpy (ssid, "<Broadcast>", sizeof(ssid));
 		} else if (tmp->stats.ssid_len == 1 && tmp->stats.ssid[0] == 0) {
-			g_snprintf (ssid, sizeof(ssid), "<Hidden>");
+			g_strlcpy (ssid, "<Hidden>", sizeof(ssid));
 		} else {
-			g_snprintf (ssid, sizeof(ssid), "%s", format_text(tmp->stats.ssid, tmp->stats.ssid_len));
+			g_strlcpy (ssid, format_text(tmp->stats.ssid, tmp->stats.ssid_len), sizeof(ssid));
 		}
 		g_snprintf (percent, sizeof(percent), "%.2f %%", f);