As suggested by Chris: use val_to_str_const() instead of match_strval() plus a

NULL-return check.

Use val_to_str_const instead of val_to_str() in a couple places where the string
is constant.

Use val_to_str() instead of blindly passing the return value from match_strval()
into a format routine (to ensure a non-NULL string pointer).  A couple of these
were cases where it could not actually return NULL, but I changed it for
consistency.

Store the return value of match_strval() rather than calling it repeatedly.

svn path=/trunk/; revision=37204
This commit is contained in:
Jeff Morriss 2011-05-17 18:58:40 +00:00
parent c204df4719
commit cdedb31e94
10 changed files with 131 additions and 158 deletions

View File

@ -2353,9 +2353,8 @@ dissect_amqp_0_10_connection(tvbuff_t *tvb,
const gchar* method_name;
method = tvb_get_guint8(tvb, offset+1);
method_name = match_strval(method, amqp_0_10_connection_methods);
if (method_name == NULL)
method_name = "<invalid connection method>";
method_name = val_to_str_const(method, amqp_0_10_connection_methods,
"<invalid connection method>");
if (check_col(pinfo->cinfo, COL_INFO)) {
col_append_str(pinfo->cinfo, COL_INFO, method_name);
col_append_str(pinfo->cinfo, COL_INFO, " ");
@ -2695,9 +2694,8 @@ dissect_amqp_0_10_session(tvbuff_t *tvb,
const gchar* method_name;
method = tvb_get_guint8(tvb, offset+1);
method_name = match_strval(method, amqp_0_10_session_methods);
if (method_name == NULL)
method_name = "<invalid session method>";
method_name = val_to_str_const(method, amqp_0_10_session_methods,
"<invalid session method>");
if (check_col(pinfo->cinfo, COL_INFO)) {
col_append_str(pinfo->cinfo, COL_INFO, method_name);
col_append_str(pinfo->cinfo, COL_INFO, " ");
@ -2969,9 +2967,8 @@ dissect_amqp_0_10_execution(tvbuff_t *tvb,
const gchar* method_name;
method = tvb_get_guint8(tvb, offset+1);
method_name = match_strval(method, amqp_0_10_execution_methods);
if (method_name == NULL)
method_name = "<invalid execution method>";
method_name = val_to_str_const(method, amqp_0_10_execution_methods,
"<invalid execution method>");
if (check_col(pinfo->cinfo, COL_INFO)) {
col_append_str(pinfo->cinfo, COL_INFO, method_name);
col_append_str(pinfo->cinfo, COL_INFO, " ");
@ -3160,9 +3157,8 @@ dissect_amqp_0_10_message(tvbuff_t *tvb,
const gchar* method_name;
method = tvb_get_guint8(tvb, offset+1);
method_name = match_strval(method, amqp_0_10_message_methods);
if (method_name == NULL)
method_name = "<invalid message method>";
method_name = val_to_str_const(method, amqp_0_10_message_methods,
"<invalid message method>");
if (check_col(pinfo->cinfo, COL_INFO)) {
col_append_str(pinfo->cinfo, COL_INFO, method_name);
col_append_str(pinfo->cinfo, COL_INFO, " ");
@ -3512,9 +3508,8 @@ dissect_amqp_0_10_tx(tvbuff_t *tvb,
const gchar* method_name;
method = tvb_get_guint8(tvb, offset+1);
method_name = match_strval(method, amqp_0_10_tx_methods);
if (method_name == NULL)
method_name = "<invalid tx method>";
method_name = val_to_str_const(method, amqp_0_10_tx_methods,
"<invalid tx method>");
if (check_col(pinfo->cinfo, COL_INFO)) {
col_append_str(pinfo->cinfo, COL_INFO, method_name);
col_append_str(pinfo->cinfo, COL_INFO, " ");
@ -3556,9 +3551,8 @@ dissect_amqp_0_10_dtx(tvbuff_t *tvb,
const gchar* method_name;
method = tvb_get_guint8(tvb, offset+1);
method_name = match_strval(method, amqp_0_10_dtx_methods);
if (method_name == NULL)
method_name = "<invalid dtx method>";
method_name = val_to_str_const(method, amqp_0_10_dtx_methods,
"<invalid dtx method>");
if (check_col(pinfo->cinfo, COL_INFO)) {
col_append_str(pinfo->cinfo, COL_INFO, method_name);
col_append_str(pinfo->cinfo, COL_INFO, " ");
@ -3757,9 +3751,8 @@ dissect_amqp_0_10_exchange(tvbuff_t *tvb,
const gchar* method_name;
method = tvb_get_guint8(tvb, offset+1);
method_name = match_strval(method, amqp_0_10_exchange_methods);
if (method_name == NULL)
method_name = "<invalid exchange method>";
method_name = val_to_str_const(method, amqp_0_10_exchange_methods,
"<invalid exchange method>");
if (check_col(pinfo->cinfo, COL_INFO)) {
col_append_str(pinfo->cinfo, COL_INFO, method_name);
col_append_str(pinfo->cinfo, COL_INFO, " ");
@ -4026,9 +4019,8 @@ dissect_amqp_0_10_queue(tvbuff_t *tvb,
const gchar* method_name;
method = tvb_get_guint8(tvb, offset+1);
method_name = match_strval(method, amqp_0_10_queue_methods);
if (method_name == NULL)
method_name = "<invalid queue method>";
method_name = val_to_str_const(method, amqp_0_10_queue_methods,
"<invalid queue method>");
if (check_col(pinfo->cinfo, COL_INFO)) {
col_append_str(pinfo->cinfo, COL_INFO, method_name);
col_append_str(pinfo->cinfo, COL_INFO, " ");
@ -4187,9 +4179,8 @@ dissect_amqp_0_10_file(tvbuff_t *tvb,
const gchar* method_name;
method = tvb_get_guint8(tvb, offset+1);
method_name = match_strval(method, amqp_0_10_file_methods);
if (method_name == NULL)
method_name = "<invalid file method>";
method_name = val_to_str_const(method, amqp_0_10_file_methods,
"<invalid file method>");
if (check_col(pinfo->cinfo, COL_INFO)) {
col_append_str(pinfo->cinfo, COL_INFO, method_name);
col_append_str(pinfo->cinfo, COL_INFO, " ");
@ -4528,9 +4519,8 @@ dissect_amqp_0_10_stream(tvbuff_t *tvb,
const gchar* method_name;
method = tvb_get_guint8(tvb, offset+1);
method_name = match_strval(method, amqp_0_10_stream_methods);
if (method_name == NULL)
method_name = "<invalid stream method>";
method_name = val_to_str_const(method, amqp_0_10_stream_methods,
"<invalid stream method>");
if (check_col(pinfo->cinfo, COL_INFO)) {
col_append_str(pinfo->cinfo, COL_INFO, method_name);
col_append_str(pinfo->cinfo, COL_INFO, " ");

View File

@ -1339,9 +1339,7 @@ trans_param_srvc_cat(tvbuff_t *tvb, proto_tree *tree, guint len, guint32 offset,
value = tvb_get_ntohs(tvb, offset);
str = match_strval(value, ansi_srvc_cat_strings);
if (NULL == str) str = "Reserved";
str = val_to_str_const(value, ansi_srvc_cat_strings, "Reserved");
proto_tree_add_text(tree, tvb, offset, 2,
"%s", str);

View File

@ -3958,11 +3958,10 @@ de_sm_cause(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo _U_, guint32 off
const gchar *str;
oct = tvb_get_guint8(tvb, offset);
str = match_strval(oct, gsm_a_sm_cause_vals);
/* SM Cause can be sent in both directions */
if (!str)
str = "Protocol error, unspecified / Service option temporarily out of order";
str = val_to_str_const(oct, gsm_a_sm_cause_vals,
"Protocol error, unspecified / Service option temporarily out of order");
proto_tree_add_uint_format_value(tree, hf_gsm_a_sm_cause, tvb,
offset, 1, oct, "%s (%u)", str, oct);
@ -3981,11 +3980,10 @@ de_sm_cause_2(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo _U_, guint32 o
const gchar *str;
oct = tvb_get_guint8(tvb, offset);
str = match_strval(oct, gsm_a_sm_cause_vals);
/* SM Cause 2 is sent only in the Network-to-MS direction */
if (!str)
str = "Service option temporarily out of order";
str = val_to_str_const(oct, gsm_a_sm_cause_vals,
"Service option temporarily out of order");
proto_tree_add_uint_format_value(tree, hf_gsm_a_sm_cause_2, tvb,
offset, 1, oct, "%s (%u)", str, oct);

View File

@ -75,8 +75,8 @@ dissect_hci_h1(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
if(pinfo->p2p_dir == P2P_DIR_SENT ||
pinfo->p2p_dir == P2P_DIR_RECV)
proto_item_append_text(hci_h1_tree, " %s %s",
match_strval(pinfo->p2p_dir,
hci_h1_direction_vals),
val_to_str(pinfo->p2p_dir,
hci_h1_direction_vals, "Unknown: %d"),
val_to_str(type,
hci_h1_type_vals,
"Unknown 0x%02x"));
@ -91,8 +91,8 @@ dissect_hci_h1(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
if(pinfo->p2p_dir == P2P_DIR_SENT ||
pinfo->p2p_dir == P2P_DIR_RECV)
col_add_fstr(pinfo->cinfo, COL_INFO, "%s %s",
match_strval(pinfo->p2p_dir,
hci_h1_direction_vals),
val_to_str(pinfo->p2p_dir,
hci_h1_direction_vals, "Unknown: %d"),
val_to_str(type, hci_h1_type_vals,
"Unknown 0x%02x"));
else

View File

@ -4699,7 +4699,7 @@ dissect_vendor_ie_wpawme(proto_item * item, proto_tree * tree, tvbuff_t * tag_tv
byte2 = tvb_get_guint8(tag_tvb, tag_off + 1);
g_snprintf(out_buff, SHORT_STR,
"WME AC Parameters: ACI %u (%s), Admission Control %sMandatory, AIFSN %u, ECWmin %u, ECWmax %u, TXOP %u",
(byte1 & 0x60) >> 5, match_strval((byte1 & 0x60) >> 5, wme_acs),
(byte1 & 0x60) >> 5, val_to_str((byte1 & 0x60) >> 5, wme_acs, "(Unknown: %d)"),
(byte1 & 0x10) ? "" : "not ", byte1 & 0x0f,
byte2 & 0x0f, (byte2 & 0xf0) >> 4,
tvb_get_letohs(tag_tvb, tag_off + 2));
@ -5058,7 +5058,7 @@ dissect_vendor_ie_aironet(proto_item * aironet_item, proto_tree * ietree,
txop = tvb_get_letohs(tvb, offset + 2);
proto_tree_add_bytes_format(ietree, hf_ieee80211_aironet_ie_qos_val, tvb, offset, 4, NULL,
"CCX QoS Parameters??: ACI %u (%s), Admission Control %sMandatory, AIFSN %u, ECWmin %u, ECWmax %u, TXOP %u",
(byte1 & 0x60) >> 5, match_strval((byte1 & 0x60) >> 5, wme_acs),
(byte1 & 0x60) >> 5, val_to_str((byte1 & 0x60) >> 5, wme_acs, "(Unknown: %d)"),
(byte1 & 0x10) ? "" : "not ", byte1 & 0x0f,
byte2 & 0x0f, (byte2 & 0xf0) >> 4,
txop);

View File

@ -484,6 +484,7 @@ dissect_nmas_reply(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ncp_tree, guin
proto_tree *atree;
proto_item *aitem;
proto_item *expert_item;
const gchar *str;
foffset = 8;
@ -595,13 +596,12 @@ dissect_nmas_reply(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ncp_tree, guin
break;
case 7:
encrypt_error = tvb_get_ntohl(tvb, foffset);
if (match_strval(encrypt_error, nmas_errors_enum)!=NULL)
str = match_strval(encrypt_error, nmas_errors_enum);
if (str)
{
if (check_col(pinfo->cinfo, COL_INFO)) {
col_add_fstr(pinfo->cinfo, COL_INFO, "R Payload Error - %s", match_strval(encrypt_error, nmas_errors_enum));
}
col_add_fstr(pinfo->cinfo, COL_INFO, "R Payload Error - %s", str);
expert_item = proto_tree_add_item(atree, hf_encrypt_error, tvb, foffset, 4, FALSE);
expert_add_info_format(pinfo, expert_item, PI_RESPONSE_CODE, PI_ERROR, "NMAS Payload Error: %s", match_strval(encrypt_error, nmas_errors_enum));
expert_add_info_format(pinfo, expert_item, PI_RESPONSE_CODE, PI_ERROR, "NMAS Payload Error: %s", str);
}
else
{
@ -620,13 +620,12 @@ dissect_nmas_reply(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ncp_tree, guin
break;
}
}
if (match_strval(return_code, nmas_errors_enum)!=NULL)
str = match_strval(return_code, nmas_errors_enum);
if (str)
{
expert_item = proto_tree_add_item(atree, hf_return_code, tvb, roffset, 4, TRUE);
expert_add_info_format(pinfo, expert_item, PI_RESPONSE_CODE, PI_ERROR, "NMAS Error: 0x%08x %s", return_code, match_strval(return_code, nmas_errors_enum));
if (check_col(pinfo->cinfo, COL_INFO)) {
col_add_fstr(pinfo->cinfo, COL_INFO, "R Error - %s", match_strval(return_code, nmas_errors_enum));
}
expert_add_info_format(pinfo, expert_item, PI_RESPONSE_CODE, PI_ERROR, "NMAS Error: 0x%08x %s", return_code, str);
col_add_fstr(pinfo->cinfo, COL_INFO, "R Error - %s", str);
}
else
{

View File

@ -675,6 +675,7 @@ dissect_sss_reply(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ncp_tree, guint
guint32 number_of_items=0;
gint32 length_of_string=0;
guint32 i = 0;
const gchar *str;
proto_tree *atree;
proto_item *aitem;
@ -698,8 +699,9 @@ dissect_sss_reply(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ncp_tree, guint
case 2:
if (request_value) {
subverb = request_value->req_nds_flags;
if (match_strval(subverb, sss_verb_enum)) {
proto_tree_add_text(atree, tvb, foffset, tvb_length_remaining(tvb, foffset), "Verb: %s", match_strval(subverb, sss_verb_enum));
str = match_strval(subverb, sss_verb_enum);
if (str) {
proto_tree_add_text(atree, tvb, foffset, tvb_length_remaining(tvb, foffset), "Verb: %s", str);
}
}
proto_tree_add_item(atree, hf_length, tvb, foffset, 4, TRUE);
@ -713,10 +715,11 @@ dissect_sss_reply(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ncp_tree, guint
{
foffset += 4;
return_code = tvb_get_letohl(tvb, foffset);
if ( match_strval(return_code, sss_errors_enum) != NULL )
str = match_strval(return_code, sss_errors_enum);
if (str)
{
expert_item = proto_tree_add_item(atree, hf_return_code, tvb, foffset, 4, TRUE);
expert_add_info_format(pinfo, expert_item, PI_RESPONSE_CODE, PI_ERROR, "SSS Error: %s", match_strval(return_code, sss_errors_enum));
expert_add_info_format(pinfo, expert_item, PI_RESPONSE_CODE, PI_ERROR, "SSS Error: %s", str);
if (check_col(pinfo->cinfo, COL_INFO)) {
col_add_fstr(pinfo->cinfo, COL_INFO, "R Error - %s", val_to_str(return_code, sss_errors_enum, "Unknown (%d)"));
}

View File

@ -2242,13 +2242,9 @@ dissect_rpc_message(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
* AUTH_GSSAPI procname.
*/
if (flavor == FLAVOR_AUTHGSSAPI_MSG) {
procname = (char *)match_strval(gss_proc, rpc_authgssapi_proc);
procname = (char *)val_to_str_const(gss_proc, rpc_authgssapi_proc, "(null)");
}
/* Don't pass NULL string pointers to the format routines below */
if (!procname)
procname = "(null)";
rpc_prog_key.prog = prog;
if ((rpc_prog = g_hash_table_lookup(rpc_progs,&rpc_prog_key)) == NULL) {
proto = NULL;

View File

@ -873,10 +873,9 @@ dissect_parameter_group(tvbuff_t *tvb, int offset, proto_tree *tree,
val_to_str(param_type, param_vals,
"Unknown parameter type (0x%02x)"));
param_tree = proto_item_add_subtree(ti, ett_ses_param);
param_str = match_strval(param_type, param_vals);
param_str = val_to_str_const(param_type, param_vals, "Unknown");
proto_tree_add_text(param_tree, tvb, offset, 1,
"Parameter type: %s",
param_str != NULL ? param_str : "Unknown");
"Parameter type: %s", param_str);
offset++;
pg_len--;
param_len = get_item_len(tvb, offset, &len_len);
@ -952,10 +951,9 @@ dissect_parameters(tvbuff_t *tvb, int offset, guint16 len, proto_tree *tree,
val_to_str(param_type, param_vals,
"Unknown parameter type (0x%02x)"));
param_tree = proto_item_add_subtree(ti, ett_ses_param);
param_str = match_strval(param_type, param_vals);
param_str = val_to_str_const(param_type, param_vals, "Unknown");
proto_tree_add_text(param_tree, tvb, offset, 1,
"Parameter type: %s",
param_str != NULL ? param_str : "Unknown");
"Parameter type: %s", param_str);
offset++;
len--;
param_len = get_item_len(tvb, offset, &len_len);

View File

@ -69,8 +69,6 @@ static dissector_table_t spp_socket_dissector_table;
static const char*
spp_conn_ctrl(guint8 ctrl)
{
const char *p;
static const value_string conn_vals[] = {
{ 0x00, "Data, No Ack Required" },
{ SPP_EOM, "End-of-Message" },
@ -82,14 +80,7 @@ spp_conn_ctrl(guint8 ctrl)
{ 0x00, NULL }
};
p = match_strval((ctrl & 0xf0), conn_vals );
if (p) {
return p;
}
else {
return "Unknown";
}
return val_to_str_const((ctrl & 0xf0), conn_vals, "Unknown");
}
static const char*