From f3bd70b246a95c7762c3710afa728f646cafa08f Mon Sep 17 00:00:00 2001 From: Hessam Jalali Date: Thu, 30 Jun 2016 21:41:15 +0430 Subject: [PATCH] fix missing fields for json, ek and pdml when used with -e fields Description: when -T json,ed or pdml used in conjunction with -e fields they would always miss the last field. in case of json and ek, if some fields in the middle are empty, the generated json would be invalid. sample for ek: { "_index": "packets-2016-06-30", "_type": "pcap_file", "_score": null, "_source": { "layers": { "e212.mcc": ["255","262"] "frame.time_epoch": ["1426550400.004751510"], "e212.mnc": ["1","1"] } } } command: tshark -T ek -r C:\a.pcap -e e212.mcc -e frame.comment -e frame.time_epoch -e e212.mnc > C:\test.json note: the comma is missing between e212.mcc and frame.time_epoch Change-Id: I2efae0c48036cf6313e2a064453c8dbc49f38b09 Reviewed-on: https://code.wireshark.org/review/16226 Petri-Dish: Pascal Quantin Reviewed-by: Martin Kacer Tested-by: Petri Dish Buildbot Reviewed-by: Pascal Quantin --- epan/print.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/epan/print.c b/epan/print.c index 7bf298c11a..f197b7b7eb 100644 --- a/epan/print.c +++ b/epan/print.c @@ -2038,6 +2038,7 @@ static void proto_tree_get_node_field_values(proto_node *node, gpointer data) static void write_specified_fields(fields_format format, output_fields_t *fields, epan_dissect_t *edt, column_info *cinfo, FILE *fh) { gsize i; + gboolean first = TRUE; gint col; gchar *col_name; gpointer field_index; @@ -2131,7 +2132,7 @@ static void write_specified_fields(fields_format format, output_fields_t *fields fv_p = fields->field_values[i]; /* Output the array of (partial) field values */ - for (j = 0; j < (g_ptr_array_len(fv_p)) - 1; j+=2 ) { + for (j = 0; j < (g_ptr_array_len(fv_p)); j+=2 ) { str = (gchar *)g_ptr_array_index(fv_p, j); fprintf(fh, " fields->len) && (g_ptr_array_len(fields->field_values[i + 1]) > 1) ) { - fputs(",\n", fh); - } else { - fputs("\n", fh); } } - } + + first = FALSE; g_ptr_array_free(fv_p, TRUE); /* get ready for the next packet */ fields->field_values[i] = NULL; } } + fputc('\n',fh); break; case FORMAT_EK: for(i = 0; i < fields->fields->len; ++i) { @@ -2195,10 +2198,13 @@ static void write_specified_fields(fields_format format, output_fields_t *fields fv_p = fields->field_values[i]; /* Output the array of (partial) field values */ - for (j = 0; j < (g_ptr_array_len(fv_p)) - 1; j+=2 ) { + for (j = 0; j < (g_ptr_array_len(fv_p)); j += 2) { str = (gchar *)g_ptr_array_index(fv_p, j); if (j == 0) { + if (!first) { + fputs(",", fh); + } fputs("\"", fh); print_escaped_ek(fh, field); fputs("\": [", fh); @@ -2208,17 +2214,16 @@ static void write_specified_fields(fields_format format, output_fields_t *fields fputs("\"", fh); g_free(str); - if (j + 2 < (g_ptr_array_len(fv_p)) - 1) { + if (j + 2 < (g_ptr_array_len(fv_p))) { fputs(",", fh); - } else { + } + else { fputs("]", fh); - if ( (i + 1 < fields->fields->len) && (g_ptr_array_len(fields->field_values[i + 1]) > 1) ) { - fputs(",", fh); - } else { } } - } + + first = FALSE; g_ptr_array_free(fv_p, TRUE); /* get ready for the next packet */ fields->field_values[i] = NULL; }