Skip to content

Commit 34d3c5a

Browse files
authored
Wireshark: minor tweaks to wmem_strbuf use (#241)
Minor changes to some cases of how wmem strbufs are used. There is minimal performance benefit, but hopefully improved code clarity. Replace `wmem_strbuf_append_printf(buf, "%s", str)` with `wmem_strbuf_append(buf, str)`. Replace `wmem_strbuf_append_printf(buf, "%c", c)` with `wmem_strbuf_append_c(buf, c)`. These changes avoid the overhead of parsing a printf format string when it's redundant. Use `wmem_strbuf_dup()` to duplicate a strbuf instead of `wmem_strbuf_new(..., wmem_strbuf_get_str(oldbuf))` for slight improvement in readability. (This change may cause a conflict with #240 that is trivial to resolve.) Use `wmem_strbuf_finalize()` when getting the final value of a strbuf. For strings allocated in the file scope, this reduces the memory used by the final string. The function is effectively a no-op for strings allocated in the packet scope, but still makes clear to future readers when a string is expected to undergo no further changes.
1 parent 67571c9 commit 34d3c5a

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

wireshark/source/packet-ja4.c

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -418,12 +418,12 @@ void decode_http_version(wmem_strbuf_t **out, const char *val) {
418418
if (strings[1] != NULL) {
419419
for (int i = 0; i < (int)strlen(strings[1]); i++) {
420420
if (strings[1][i] != '.') {
421-
wmem_strbuf_append_printf(*out, "%c", g_ascii_tolower(strings[1][i]));
421+
wmem_strbuf_append_c(*out, g_ascii_tolower(strings[1][i]));
422422
}
423423
}
424424

425425
if (wmem_strbuf_get_len(*out) <= 1) {
426-
wmem_strbuf_append_printf(*out, "%s", "0");
426+
wmem_strbuf_append(*out, "0");
427427
}
428428
}
429429
}
@@ -443,7 +443,7 @@ void create_sorted_cookies(wmem_strbuf_t **fields, wmem_strbuf_t **values, wmem_
443443

444444
// Append last entry without a trailing comma
445445
curr_cookie = wmem_list_frame_data(curr_entry);
446-
wmem_strbuf_append_printf(*fields, "%s", wmem_strbuf_get_str(curr_cookie->field));
446+
wmem_strbuf_append(*fields, wmem_strbuf_get_str(curr_cookie->field));
447447
wmem_strbuf_append_printf(
448448
*values, "%s=%s", wmem_strbuf_get_str(curr_cookie->field),
449449
wmem_strbuf_get_str(curr_cookie->value)
@@ -461,7 +461,7 @@ char *ja4s_r(wmem_allocator_t *scope, ja4_info_t *data) {
461461
: '0',
462462
wmem_strbuf_get_str(data->ciphers), wmem_strbuf_get_str(data->extensions)
463463
);
464-
return (char *)wmem_strbuf_get_str(display);
464+
return wmem_strbuf_finalize(display);
465465
}
466466

467467
char *ja4s(wmem_allocator_t *scope, ja4_info_t *data) {
@@ -480,7 +480,7 @@ char *ja4s(wmem_allocator_t *scope, ja4_info_t *data) {
480480
);
481481
if (_hash != NULL)
482482
g_free(_hash);
483-
return (char *)wmem_strbuf_get_str(display);
483+
return wmem_strbuf_finalize(display);
484484
}
485485

486486
char *ja4x(wmem_allocator_t *scope, cert_t *cert) {
@@ -503,7 +503,7 @@ char *ja4x(wmem_allocator_t *scope, cert_t *cert) {
503503
g_free(hash2);
504504
if (hash3 != NULL)
505505
g_free(hash3);
506-
return (char *)wmem_strbuf_get_str(display);
506+
return wmem_strbuf_finalize(display);
507507
}
508508

509509
char *ja4h_r(wmem_allocator_t *scope, ja4h_info_t *data) {
@@ -515,7 +515,7 @@ char *ja4h_r(wmem_allocator_t *scope, ja4h_info_t *data) {
515515
wmem_strbuf_get_str(data->sorted_cookie_fields),
516516
wmem_strbuf_get_str(data->sorted_cookie_values)
517517
);
518-
return (char *)wmem_strbuf_get_str(display);
518+
return wmem_strbuf_finalize(display);
519519
}
520520

521521
char *ja4h_ro(wmem_allocator_t *scope, ja4h_info_t *data) {
@@ -527,7 +527,7 @@ char *ja4h_ro(wmem_allocator_t *scope, ja4h_info_t *data) {
527527
wmem_strbuf_get_str(data->unsorted_cookie_fields),
528528
wmem_strbuf_get_str(data->unsorted_cookie_values)
529529
);
530-
return (char *)wmem_strbuf_get_str(display);
530+
return wmem_strbuf_finalize(display);
531531
}
532532

533533
char *ja4h(wmem_allocator_t *scope, ja4h_info_t *data) {
@@ -553,7 +553,7 @@ char *ja4h(wmem_allocator_t *scope, ja4h_info_t *data) {
553553
g_free(hash2);
554554
if (hash3 != NULL)
555555
g_free(hash3);
556-
return (char *)wmem_strbuf_get_str(display);
556+
return wmem_strbuf_finalize(display);
557557
}
558558

559559
char *ja4ssh(wmem_allocator_t *scope, conn_info_t *conn) {
@@ -563,7 +563,7 @@ char *ja4ssh(wmem_allocator_t *scope, conn_info_t *conn) {
563563
get_max_mode(scope, conn->server_mode), conn->client_pkts, conn->server_pkts,
564564
conn->tcp_client_acks, conn->tcp_server_acks
565565
);
566-
return (char *)wmem_strbuf_get_str(display);
566+
return wmem_strbuf_finalize(display);
567567
}
568568

569569
// Compute JA4T
@@ -588,12 +588,12 @@ char *ja4t(wmem_allocator_t *scope, ja4t_info_t *data, conn_info_t *conn) {
588588
}
589589

590590
if ((conn != NULL) && (conn->syn_ack_count > 1)) {
591-
wmem_strbuf_append_printf(display, "%c", '_');
591+
wmem_strbuf_append_c(display, '_');
592592
for (int i = 1; i < conn->syn_ack_count; i++) {
593593
int64_t diff = timediff(&conn->syn_ack_times[i], &conn->syn_ack_times[i - 1]);
594594
wmem_strbuf_append_printf(display, "%" PRId64, diff);
595595
if (i < (conn->syn_ack_count - 1)) {
596-
wmem_strbuf_append_printf(display, "%c", '-');
596+
wmem_strbuf_append_c(display, '-');
597597
}
598598
}
599599
if (!nstime_is_zero(&conn->rst_time)) {
@@ -602,7 +602,7 @@ char *ja4t(wmem_allocator_t *scope, ja4t_info_t *data, conn_info_t *conn) {
602602
}
603603
}
604604

605-
return (char *)wmem_strbuf_get_str(display);
605+
return wmem_strbuf_finalize(display);
606606
}
607607

608608
char *ja4d(wmem_allocator_t *scope, ja4d_info_t *data) {
@@ -621,7 +621,7 @@ char *ja4d(wmem_allocator_t *scope, ja4d_info_t *data) {
621621
wmem_strbuf_get_str(data->options),
622622
wmem_strbuf_get_str(data->request_list)
623623
);
624-
return (char *)wmem_strbuf_get_str(display);
624+
return wmem_strbuf_finalize(display);
625625
}
626626

627627
static void init_ja4_data(packet_info *pinfo, ja4_info_t *ja4_data) {
@@ -850,9 +850,9 @@ static int dissect_ja4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void
850850
if (!alpn_visited) {
851851
const char *alpn_str = fvalue_get_string(get_value_ptr(field));
852852
if (!g_ascii_isalnum(alpn_str[0])) {
853-
wmem_strbuf_append_printf(ja4_data.alpn, "%s", "99");
853+
wmem_strbuf_append(ja4_data.alpn, "99");
854854
} else {
855-
wmem_strbuf_append_printf(ja4_data.alpn, "%s", alpn_str);
855+
wmem_strbuf_append(ja4_data.alpn, alpn_str);
856856
}
857857
alpn_visited = true;
858858
}
@@ -970,13 +970,13 @@ static int dissect_ja4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void
970970
}
971971
}
972972

973-
wmem_strbuf_append_printf(ja4h_data.method, "%s", ja4h_code);
973+
wmem_strbuf_append(ja4h_data.method, ja4h_code);
974974

975975
http_req = field->hfinfo->parent;
976976
}
977977

978978
if (strcmp(field->hfinfo->abbrev, "http2.headers.method") == 0) {
979-
wmem_strbuf_append_printf(ja4h_data.version, "20");
979+
wmem_strbuf_append(ja4h_data.version, "20");
980980
ja4h_data.http2 = true;
981981
}
982982

@@ -1158,7 +1158,7 @@ static int dissect_ja4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void
11581158
);
11591159
update_tree_item(
11601160
tvb, tree, &ja4_tree, hf_ja4ls,
1161-
wmem_strbuf_get_str(display), "tcp"
1161+
wmem_strbuf_finalize(display), "tcp"
11621162
);
11631163

11641164
nstime_delta(&latency, &conn->timestamp_C, &conn->timestamp_B);
@@ -1168,7 +1168,7 @@ static int dissect_ja4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void
11681168
);
11691169
update_tree_item(
11701170
tvb, tree, &ja4_tree, hf_ja4l,
1171-
wmem_strbuf_get_str(display2), "tcp"
1171+
wmem_strbuf_finalize(display2), "tcp"
11721172
);
11731173
}
11741174
}
@@ -1187,7 +1187,7 @@ static int dissect_ja4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void
11871187
);
11881188
update_tree_item(
11891189
tvb, tree, &ja4_tree, hf_ja4ls,
1190-
wmem_strbuf_get_str(display), "tcp"
1190+
wmem_strbuf_finalize(display), "tcp"
11911191
);
11921192

11931193
nstime_delta(&latency, &conn->timestamp_C, &conn->timestamp_B);
@@ -1198,7 +1198,7 @@ static int dissect_ja4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void
11981198
);
11991199
update_tree_item(
12001200
tvb, tree, &ja4_tree, hf_ja4l,
1201-
wmem_strbuf_get_str(display2), "tcp"
1201+
wmem_strbuf_finalize(display2), "tcp"
12021202
);
12031203
}
12041204
}
@@ -1253,7 +1253,7 @@ static int dissect_ja4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void
12531253
);
12541254
update_tree_item(
12551255
tvb, tree, &ja4_tree, hf_ja4ls,
1256-
wmem_strbuf_get_str(display), "quic"
1256+
wmem_strbuf_finalize(display), "quic"
12571257
);
12581258

12591259
nstime_delta(&latency, &conn->timestamp_D, &conn->timestamp_C);
@@ -1262,7 +1262,7 @@ static int dissect_ja4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void
12621262
);
12631263
update_tree_item(
12641264
tvb, tree, &ja4_tree, hf_ja4l,
1265-
wmem_strbuf_get_str(display2), "quic"
1265+
wmem_strbuf_finalize(display2), "quic"
12661266
);
12671267
}
12681268
}
@@ -1371,7 +1371,7 @@ static int dissect_ja4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void
13711371
conn->mss_val = ja4t_data.mss_val;
13721372
if (conn->tcp_options == NULL)
13731373
conn->tcp_options =
1374-
wmem_strbuf_new(wmem_file_scope(), wmem_strbuf_get_str(ja4t_data.tcp_options));
1374+
wmem_strbuf_dup(wmem_file_scope(), ja4t_data.tcp_options);
13751375
update_tree_item(tvb, tree, &ja4_tree, hf_ja4ts, ja4t(pinfo->pool, &ja4t_data, conn), "tcp");
13761376
}
13771377

@@ -1381,8 +1381,8 @@ static int dissect_ja4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void
13811381
ja4t_data.window_size = conn->window_size;
13821382
ja4t_data.mss_val = conn->mss_val;
13831383
if (conn->tcp_options != NULL)
1384-
wmem_strbuf_append_printf(
1385-
ja4t_data.tcp_options, "%s", wmem_strbuf_get_str(conn->tcp_options)
1384+
wmem_strbuf_append(
1385+
ja4t_data.tcp_options, wmem_strbuf_get_str(conn->tcp_options)
13861386
);
13871387
update_tree_item(tvb, tree, &ja4_tree, hf_ja4ts, ja4t(pinfo->pool, &ja4t_data, conn), "tcp");
13881388
}
@@ -1425,7 +1425,7 @@ static int dissect_ja4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void
14251425
}
14261426

14271427
if (wmem_strbuf_get_len(ja4h_data.lang) == 0) {
1428-
wmem_strbuf_append_printf(ja4h_data.lang, "%s", "0000");
1428+
wmem_strbuf_append(ja4h_data.lang, "0000");
14291429
}
14301430

14311431
char *http_proto = "http";

0 commit comments

Comments
 (0)