From c9b0e9c81335e098cdb256b1c58597cda96703b1 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Thu, 25 May 2017 00:28:45 +0200 Subject: Report syntax errors in UAT prefs parsing Add the cause for a syntax error while parsing UATs. Example output: $ tshark -ouat:ssl_keys:, tshark: Invalid -o flag "uat:ssl_keys:,": ssl_keys:1: No IP address given. $ tshark -ouat:unknown:, tshark: Invalid -o flag "uat:unknown:,": Unknown preference Change-Id: I549406c4e31a81d29f487ef47bdb3c22da084947 Reviewed-on: https://code.wireshark.org/review/21748 Petri-Dish: Peter Wu Tested-by: Petri Dish Buildbot Reviewed-by: Jakub Zawadzki Reviewed-by: Michael Mann --- epan/prefs.c | 13 +++++++------ epan/prefs.h | 21 ++++++++++++--------- 2 files changed, 19 insertions(+), 15 deletions(-) (limited to 'epan') diff --git a/epan/prefs.c b/epan/prefs.c index 18fdc15c31..35ff3fa2a2 100644 --- a/epan/prefs.c +++ b/epan/prefs.c @@ -4510,10 +4510,9 @@ read_prefs_file(const char *pf_path, FILE *pf, * a valid uat entry. */ static gboolean -prefs_set_uat_pref(char *uat_entry) { +prefs_set_uat_pref(char *uat_entry, char **errmsg) { gchar *p, *colonp; uat_t *uat; - gchar *err = NULL; gboolean ret; colonp = strchr(uat_entry, ':'); @@ -4543,11 +4542,11 @@ prefs_set_uat_pref(char *uat_entry) { uat = uat_find(uat_entry); *colonp = ':'; if (uat == NULL) { + *errmsg = g_strdup("Unknown preference"); return FALSE; } - ret = uat_load_str(uat, p, &err); - g_free(err); + ret = uat_load_str(uat, p, errmsg); return ret; } @@ -4558,7 +4557,7 @@ prefs_set_uat_pref(char *uat_entry) { * in some fashion. */ prefs_set_pref_e -prefs_set_pref(char *prefarg) +prefs_set_pref(char *prefarg, char **errmsg) { gchar *p, *colonp; prefs_set_pref_e ret; @@ -4573,6 +4572,8 @@ prefs_set_pref(char *prefarg) mgcp_tcp_port_count = -1; mgcp_udp_port_count = -1; + *errmsg = NULL; + colonp = strchr(prefarg, ':'); if (colonp == NULL) return PREFS_SET_SYNTAX_ERR; @@ -4599,7 +4600,7 @@ prefs_set_pref(char *prefarg) if (strcmp(prefarg, "uat")) { ret = set_pref(prefarg, p, NULL, TRUE); } else { - ret = prefs_set_uat_pref(p) ? PREFS_SET_OK : PREFS_SET_SYNTAX_ERR; + ret = prefs_set_uat_pref(p, errmsg) ? PREFS_SET_OK : PREFS_SET_SYNTAX_ERR; } *colonp = ':'; /* put the colon back */ return ret; diff --git a/epan/prefs.h b/epan/prefs.h index 85d66b6cf6..e757a25533 100644 --- a/epan/prefs.h +++ b/epan/prefs.h @@ -575,14 +575,8 @@ extern e_prefs *read_prefs(void); into "*pf_path_return", and return the errno. */ WS_DLL_PUBLIC int write_prefs(char **); -/* - * Given a string of the form ":", as might appear - * as an argument to a "-o" option, parse it and set the preference in - * question. Return an indication of whether it succeeded or failed - * in some fashion. - * - * XXX - should supply, for syntax errors, a detailed explanation of - * the syntax error. +/** + * Result of setting a preference. */ typedef enum { PREFS_SET_OK, /* succeeded */ @@ -591,7 +585,16 @@ typedef enum { PREFS_SET_OBSOLETE /* preference used to exist but no longer does */ } prefs_set_pref_e; -WS_DLL_PUBLIC prefs_set_pref_e prefs_set_pref(char *prefarg); +/* + * Given a string of the form ":", as might appear + * as an argument to a "-o" option, parse it and set the preference in + * question. Return an indication of whether it succeeded or failed + * in some fashion. + * + * For syntax errors (return value PREFS_SET_SYNTAX_ERR), details (when + * available) are written into "errmsg" which must be freed with g_free. + */ +WS_DLL_PUBLIC prefs_set_pref_e prefs_set_pref(char *prefarg, char **errmsg); /* * Get or set a preference's obsolete status. These can be used to make a -- cgit v1.2.3