From 40ec1adfb06b463292c52a586fffef190e9cc8f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Valverde?= Date: Tue, 25 Oct 2022 03:04:45 +0100 Subject: [PATCH] S7Comm: Fix invalid UTF-8 value string chars Fixes #18533. --- epan/dissectors/packet-s7comm.c | 6 +++--- epan/proto.c | 3 +-- epan/proto.h | 3 +++ epan/value_string.c | 16 ++++++++++++++++ epan/value_string.h | 4 ++++ 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/epan/dissectors/packet-s7comm.c b/epan/dissectors/packet-s7comm.c index 576f640605..d2c308eafd 100644 --- a/epan/dissectors/packet-s7comm.c +++ b/epan/dissectors/packet-s7comm.c @@ -3308,7 +3308,7 @@ s7comm_decode_pi_service(tvbuff_t *tvb, col_append_str(pinfo->cinfo, COL_INFO, ", "); } itemadd = proto_tree_add_item(file_tree, hf_s7comm_data_blockcontrol_dest_filesys, tvb, paramoffset, 1, ENC_ASCII); - proto_item_append_text(itemadd, " (%s)", val_to_str(tvb_get_guint8(tvb, paramoffset), blocktype_attribute2_names, "Unknown filesys: %c")); + proto_item_append_text(itemadd, " (%s)", char_val_to_str(tvb_get_guint8(tvb, paramoffset), blocktype_attribute2_names, "Unknown filesys")); paramoffset += 1; } col_append_str(pinfo->cinfo, COL_INFO, ")"); @@ -3667,7 +3667,7 @@ s7comm_decode_plc_controls_filename(tvbuff_t *tvb, col_append_str(pinfo->cinfo, COL_INFO, "NaN]"); } itemadd = proto_tree_add_item(file_tree, hf_s7comm_data_blockcontrol_dest_filesys, tvb, offset, 1, ENC_ASCII); - proto_item_append_text(itemadd, " (%s)", val_to_str(tvb_get_guint8(tvb, offset), blocktype_attribute2_names, "Unknown filesys: %c")); + proto_item_append_text(itemadd, " (%s)", char_val_to_str(tvb_get_guint8(tvb, offset), blocktype_attribute2_names, "Unknown filesys")); offset += 1; } } @@ -6221,7 +6221,7 @@ s7comm_decode_ud_block_subfunc(tvbuff_t *tvb, } offset += 5; itemadd = proto_tree_add_item(data_tree, hf_s7comm_ud_blockinfo_filesys, tvb, offset, 1, ENC_ASCII); - proto_item_append_text(itemadd, " (%s)", val_to_str(tvb_get_guint8(tvb, offset), blocktype_attribute2_names, "Unknown filesys: %c")); + proto_item_append_text(itemadd, " (%s)", char_val_to_str(tvb_get_guint8(tvb, offset), blocktype_attribute2_names, "Unknown filesys")); offset += 1; } know_data = TRUE; diff --git a/epan/proto.c b/epan/proto.c index 8c37095c7e..43ecd038ff 100644 --- a/epan/proto.c +++ b/epan/proto.c @@ -237,7 +237,6 @@ static void fill_label_number64(field_info *fi, gchar *label_str, gboolean is_si static size_t fill_display_label_float(field_info *fi, gchar *label_str); static void fill_label_float(field_info *fi, gchar *label_str); -static const char *hfinfo_char_value_format_display(int display, char buf[7], guint32 value); static const char *hfinfo_number_value_format_display(const header_field_info *hfinfo, int display, char buf[32], guint32 value); static const char *hfinfo_number_value_format_display64(const header_field_info *hfinfo, int display, char buf[48], guint64 value); static const char *hfinfo_char_vals_format(const header_field_info *hfinfo, char buf[32], guint32 value); @@ -10103,7 +10102,7 @@ hfinfo_hex_digits(const header_field_info *hfinfo) return (bitwidth + 3) / 4; } -static const char * +const char * hfinfo_char_value_format_display(int display, char buf[7], guint32 value) { char *ptr = &buf[6]; diff --git a/epan/proto.h b/epan/proto.h index ef58a9666d..475fdb37ff 100644 --- a/epan/proto.h +++ b/epan/proto.h @@ -3357,6 +3357,9 @@ proto_custom_set(proto_tree* tree, GSList *field_id, /** @} */ +const char * +hfinfo_char_value_format_display(int display, char buf[7], guint32 value); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/epan/value_string.c b/epan/value_string.c index 841eed789f..82eb833ff4 100644 --- a/epan/value_string.c +++ b/epan/value_string.c @@ -106,6 +106,22 @@ try_val_to_str(const guint32 val, const value_string *vs) return try_val_to_str_idx(val, vs, &ignore_me); } +const gchar * +char_val_to_str(char val, const value_string *vs, const char *msg) +{ + const gchar *ret; + char buf[7]; + + DISSECTOR_ASSERT(msg != NULL); + + ret = try_val_to_str(val, vs); + if (ret != NULL) + return ret; + + return wmem_strdup_printf(wmem_packet_scope(), "%s: %s", + msg, hfinfo_char_value_format_display(BASE_HEX, buf, val)); +} + /* 64-BIT VALUE STRING */ const gchar * diff --git a/epan/value_string.h b/epan/value_string.h index a8c3a31743..c0ee2551b7 100644 --- a/epan/value_string.h +++ b/epan/value_string.h @@ -124,6 +124,10 @@ WS_DLL_PUBLIC const gchar * try_val_to_str_idx(const guint32 val, const value_string *vs, gint *idx); +WS_DLL_PUBLIC +const gchar * +char_val_to_str(char val, const value_string *vs, const char *msg); + /* 64-BIT VALUE TO STRING MATCHING */ typedef struct _val64_string {