From eb1ab4455976e7744d5a114b9767fbd3b280bf3c Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 23 Dec 2024 19:14:01 +0900 Subject: [PATCH] C99 designated initializers Prefer {0} to memset. --- lib/nghttp3_conn.c | 2 +- lib/nghttp3_gaptr.c | 20 ++++- lib/nghttp3_ksl.c | 2 +- lib/nghttp3_map.c | 6 +- lib/nghttp3_qpack.c | 65 +++++++++++----- lib/nghttp3_qpack_huffman.c | 4 +- lib/nghttp3_range.c | 2 +- lib/nghttp3_stream.c | 13 +++- lib/nghttp3_version.c | 7 +- tests/main.c | 10 +-- tests/nghttp3_conn_test.c | 144 +++++++++++++++--------------------- tests/nghttp3_http_test.c | 108 +++++++++++++++++++++------ tests/nghttp3_qpack_test.c | 3 +- tests/nghttp3_test_helper.c | 11 ++- tests/nghttp3_test_helper.h | 6 +- tests/nghttp3_tnode_test.c | 3 +- 16 files changed, 253 insertions(+), 153 deletions(-) diff --git a/lib/nghttp3_conn.c b/lib/nghttp3_conn.c index 0cf9f7e..1f6c857 100644 --- a/lib/nghttp3_conn.c +++ b/lib/nghttp3_conn.c @@ -1836,7 +1836,7 @@ int nghttp3_conn_create_stream(nghttp3_conn *conn, nghttp3_stream **pstream, nghttp3_stream *stream; int rv; nghttp3_stream_callbacks callbacks = { - conn_stream_acked_data, + .acked_data = conn_stream_acked_data, }; rv = nghttp3_stream_new(&stream, stream_id, &callbacks, diff --git a/lib/nghttp3_gaptr.c b/lib/nghttp3_gaptr.c index 9ad12af..8ed5185 100644 --- a/lib/nghttp3_gaptr.c +++ b/lib/nghttp3_gaptr.c @@ -36,7 +36,9 @@ void nghttp3_gaptr_init(nghttp3_gaptr *gaptr, const nghttp3_mem *mem) { } static int gaptr_gap_init(nghttp3_gaptr *gaptr) { - nghttp3_range range = {0, UINT64_MAX}; + nghttp3_range range = { + .end = UINT64_MAX, + }; return nghttp3_ksl_insert(&gaptr->gap, NULL, &range, NULL); } @@ -52,7 +54,11 @@ void nghttp3_gaptr_free(nghttp3_gaptr *gaptr) { int nghttp3_gaptr_push(nghttp3_gaptr *gaptr, uint64_t offset, uint64_t datalen) { int rv; - nghttp3_range k, m, l, r, q = {offset, offset + datalen}; + nghttp3_range k, m, l, r; + nghttp3_range q = { + .begin = offset, + .end = offset + datalen, + }; nghttp3_ksl_it it; if (nghttp3_ksl_len(&gaptr->gap) == 0) { @@ -112,7 +118,10 @@ uint64_t nghttp3_gaptr_first_gap_offset(nghttp3_gaptr *gaptr) { nghttp3_range nghttp3_gaptr_get_first_gap_after(nghttp3_gaptr *gaptr, uint64_t offset) { - nghttp3_range q = {offset, offset + 1}; + nghttp3_range q = { + .begin = offset, + .end = offset + 1, + }; nghttp3_ksl_it it; if (nghttp3_ksl_len(&gaptr->gap) == 0) { @@ -130,7 +139,10 @@ nghttp3_range nghttp3_gaptr_get_first_gap_after(nghttp3_gaptr *gaptr, int nghttp3_gaptr_is_pushed(nghttp3_gaptr *gaptr, uint64_t offset, uint64_t datalen) { - nghttp3_range q = {offset, offset + datalen}; + nghttp3_range q = { + .begin = offset, + .end = offset + datalen, + }; nghttp3_ksl_it it; nghttp3_range m; diff --git a/lib/nghttp3_ksl.c b/lib/nghttp3_ksl.c index f7bc6a3..33dec06 100644 --- a/lib/nghttp3_ksl.c +++ b/lib/nghttp3_ksl.c @@ -34,7 +34,7 @@ #include "nghttp3_mem.h" #include "nghttp3_range.h" -static nghttp3_ksl_blk null_blk = {{{NULL, NULL, 0, 0, {0}}}}; +static nghttp3_ksl_blk null_blk; nghttp3_objalloc_def(ksl_blk, nghttp3_ksl_blk, oplent) diff --git a/lib/nghttp3_map.c b/lib/nghttp3_map.c index cc5e42a..829f9e8 100644 --- a/lib/nghttp3_map.c +++ b/lib/nghttp3_map.c @@ -120,7 +120,11 @@ void nghttp3_map_print_distance(const nghttp3_map *map) { static int insert(nghttp3_map_bucket *table, size_t hashbits, nghttp3_map_key_type key, void *data) { size_t idx = hash(key, hashbits); - nghttp3_map_bucket b = {0, key, data}, *bkt; + nghttp3_map_bucket b = { + .key = key, + .data = data, + }; + nghttp3_map_bucket *bkt; size_t mask = (1u << hashbits) - 1; for (;;) { diff --git a/lib/nghttp3_qpack.c b/lib/nghttp3_qpack.c index 5dab00d..ff77431 100644 --- a/lib/nghttp3_qpack.c +++ b/lib/nghttp3_qpack.c @@ -41,7 +41,12 @@ #define NGHTTP3_QPACK_MAX_QPACK_STREAMS 2000 /* Make scalar initialization form of nghttp3_qpack_static_entry */ -#define MAKE_STATIC_ENT(I, T, H) {I, T, H} +#define MAKE_STATIC_ENT(I, T, H) \ + { \ + .absidx = I, \ + .token = T, \ + .hash = H, \ + } /* Generated by mkstatichdtbl.py */ static nghttp3_qpack_static_entry token_stable[] = { @@ -166,9 +171,19 @@ static nghttp3_qpack_static_entry token_stable[] = { /* Make scalar initialization form of nghttp3_qpack_static_entry */ #define MAKE_STATIC_HD(N, V, T) \ { \ - {NULL, (uint8_t *)(N), sizeof((N)) - 1, -1}, \ - {NULL, (uint8_t *)(V), sizeof((V)) - 1, -1}, \ - T, \ + .name = \ + { \ + .base = (uint8_t *)(N), \ + .len = sizeof((N)) - 1, \ + .ref = -1, \ + }, \ + .value = \ + { \ + .base = (uint8_t *)(V), \ + .len = sizeof((V)) - 1, \ + .ref = -1, \ + }, \ + .token = T, \ } static nghttp3_qpack_static_header stable[] = { @@ -1447,7 +1462,14 @@ int nghttp3_qpack_encoder_encode_nv(nghttp3_qpack_encoder *encoder, uint32_t hash = 0; int32_t token; nghttp3_qpack_indexing_mode indexing_mode; - nghttp3_qpack_lookup_result sres = {-1, 0, -1}, dres = {-1, 0, -1}; + nghttp3_qpack_lookup_result sres = { + .index = -1, + .pb_index = -1, + }; + nghttp3_qpack_lookup_result dres = { + .index = -1, + .pb_index = -1, + }; nghttp3_qpack_entry *new_ent = NULL; int static_entry; int just_index = 0; @@ -1611,8 +1633,10 @@ int nghttp3_qpack_encoder_encode_nv(nghttp3_qpack_encoder *encoder, nghttp3_qpack_lookup_result nghttp3_qpack_lookup_stable(const nghttp3_nv *nv, int32_t token, nghttp3_qpack_indexing_mode indexing_mode) { - nghttp3_qpack_lookup_result res = {(nghttp3_ssize)token_stable[token].absidx, - 0, -1}; + nghttp3_qpack_lookup_result res = { + .index = (nghttp3_ssize)token_stable[token].absidx, + .pb_index = -1, + }; nghttp3_qpack_static_entry *ent; nghttp3_qpack_static_header *hdr; size_t i; @@ -1642,7 +1666,10 @@ nghttp3_qpack_lookup_result nghttp3_qpack_encoder_lookup_dtable( nghttp3_qpack_encoder *encoder, const nghttp3_nv *nv, int32_t token, uint32_t hash, nghttp3_qpack_indexing_mode indexing_mode, uint64_t krcnt, int allow_blocking) { - nghttp3_qpack_lookup_result res = {-1, 0, -1}; + nghttp3_qpack_lookup_result res = { + .index = -1, + .pb_index = -1, + }; int exact_match = 0; nghttp3_qpack_entry *match, *pb_match; @@ -2262,10 +2289,11 @@ void nghttp3_qpack_entry_free(nghttp3_qpack_entry *ent) { int nghttp3_qpack_encoder_block_stream(nghttp3_qpack_encoder *encoder, nghttp3_qpack_stream *stream) { nghttp3_blocked_streams_key bsk = { - nghttp3_struct_of(nghttp3_pq_top(&stream->max_cnts), - nghttp3_qpack_header_block_ref, max_cnts_pe) - ->max_cnt, - (uint64_t)stream->stream_id}; + .max_cnt = nghttp3_struct_of(nghttp3_pq_top(&stream->max_cnts), + nghttp3_qpack_header_block_ref, max_cnts_pe) + ->max_cnt, + .id = (uint64_t)stream->stream_id, + }; return nghttp3_ksl_insert(&encoder->blocked_streams, NULL, &bsk, stream); } @@ -2273,10 +2301,11 @@ int nghttp3_qpack_encoder_block_stream(nghttp3_qpack_encoder *encoder, void nghttp3_qpack_encoder_unblock_stream(nghttp3_qpack_encoder *encoder, nghttp3_qpack_stream *stream) { nghttp3_blocked_streams_key bsk = { - nghttp3_struct_of(nghttp3_pq_top(&stream->max_cnts), - nghttp3_qpack_header_block_ref, max_cnts_pe) - ->max_cnt, - (uint64_t)stream->stream_id}; + .max_cnt = nghttp3_struct_of(nghttp3_pq_top(&stream->max_cnts), + nghttp3_qpack_header_block_ref, max_cnts_pe) + ->max_cnt, + .id = (uint64_t)stream->stream_id, + }; nghttp3_ksl_it it; /* This is purely debugging purpose only */ @@ -2290,7 +2319,9 @@ void nghttp3_qpack_encoder_unblock_stream(nghttp3_qpack_encoder *encoder, void nghttp3_qpack_encoder_unblock(nghttp3_qpack_encoder *encoder, uint64_t max_cnt) { - nghttp3_blocked_streams_key bsk = {max_cnt, 0}; + nghttp3_blocked_streams_key bsk = { + .max_cnt = max_cnt, + }; nghttp3_ksl_it it; it = nghttp3_ksl_lower_bound(&encoder->blocked_streams, &bsk); diff --git a/lib/nghttp3_qpack_huffman.c b/lib/nghttp3_qpack_huffman.c index 3398f3f..dbc9a76 100644 --- a/lib/nghttp3_qpack_huffman.c +++ b/lib/nghttp3_qpack_huffman.c @@ -88,7 +88,9 @@ nghttp3_qpack_huffman_decode(nghttp3_qpack_huffman_decode_context *ctx, int fin) { uint8_t *p = dest; const uint8_t *end = src + srclen; - nghttp3_qpack_huffman_decode_node node = {ctx->fstate, 0}; + nghttp3_qpack_huffman_decode_node node = { + .fstate = ctx->fstate, + }; const nghttp3_qpack_huffman_decode_node *t = &node; uint8_t c; diff --git a/lib/nghttp3_range.c b/lib/nghttp3_range.c index af810a2..b5f95bb 100644 --- a/lib/nghttp3_range.c +++ b/lib/nghttp3_range.c @@ -33,7 +33,7 @@ void nghttp3_range_init(nghttp3_range *r, uint64_t begin, uint64_t end) { nghttp3_range nghttp3_range_intersect(const nghttp3_range *a, const nghttp3_range *b) { - nghttp3_range r = {0, 0}; + nghttp3_range r = {0}; uint64_t begin = nghttp3_max_uint64(a->begin, b->begin); uint64_t end = nghttp3_min_uint64(a->end, b->end); diff --git a/lib/nghttp3_stream.c b/lib/nghttp3_stream.c index 4967f93..f34c635 100644 --- a/lib/nghttp3_stream.c +++ b/lib/nghttp3_stream.c @@ -336,12 +336,19 @@ int nghttp3_stream_write_settings(nghttp3_stream *stream, struct { nghttp3_frame_settings settings; nghttp3_settings_entry iv[15]; - } fr; + } fr = { + .settings = + { + .hd = + { + .type = NGHTTP3_FRAME_SETTINGS, + }, + .niv = 3, + }, + }; nghttp3_settings_entry *iv; nghttp3_settings *local_settings = frent->aux.settings.local_settings; - fr.settings.hd.type = NGHTTP3_FRAME_SETTINGS; - fr.settings.niv = 3; iv = &fr.settings.iv[0]; iv[0].id = NGHTTP3_SETTINGS_ID_MAX_FIELD_SECTION_SIZE; diff --git a/lib/nghttp3_version.c b/lib/nghttp3_version.c index 939821d..b88e225 100644 --- a/lib/nghttp3_version.c +++ b/lib/nghttp3_version.c @@ -28,8 +28,11 @@ #include -static nghttp3_info version = {NGHTTP3_VERSION_AGE, NGHTTP3_VERSION_NUM, - NGHTTP3_VERSION}; +static nghttp3_info version = { + .age = NGHTTP3_VERSION_AGE, + .version_num = NGHTTP3_VERSION_NUM, + .version_str = NGHTTP3_VERSION, +}; const nghttp3_info *nghttp3_version(int least_version) { if (least_version > NGHTTP3_VERSION_NUM) { diff --git a/tests/main.c b/tests/main.c index 009fc8c..2ac01b1 100644 --- a/tests/main.c +++ b/tests/main.c @@ -39,14 +39,12 @@ int main(int argc, char **argv) { const MunitSuite suites[] = { - qpack_suite, - conn_suite, - tnode_suite, - http_suite, - {NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE}, + qpack_suite, conn_suite, tnode_suite, http_suite, {0}, }; const MunitSuite suite = { - "", NULL, suites, 1, MUNIT_SUITE_OPTION_NONE, + .prefix = "", + .suites = suites, + .iterations = 1, }; return munit_suite_main(&suite, NULL, argc, argv); diff --git a/tests/nghttp3_conn_test.c b/tests/nghttp3_conn_test.c index 215c1d6..a6d58a8 100644 --- a/tests/nghttp3_conn_test.c +++ b/tests/nghttp3_conn_test.c @@ -67,7 +67,8 @@ static const MunitTest tests[] = { }; const MunitSuite conn_suite = { - "/conn", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE, + .prefix = "/conn", + .tests = tests, }; static uint8_t nulldata[4096]; @@ -321,7 +322,9 @@ static int recv_settings(nghttp3_conn *conn, const nghttp3_settings *settings, void test_nghttp3_conn_read_control(void) { const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = { + .recv_settings = recv_settings, + }; nghttp3_settings settings; int rv; uint8_t rawbuf[2048]; @@ -335,8 +338,6 @@ void test_nghttp3_conn_read_control(void) { size_t i; userdata ud; - memset(&callbacks, 0, sizeof(callbacks)); - callbacks.recv_settings = recv_settings; nghttp3_settings_default(&settings); nghttp3_buf_wrap_init(&buf, rawbuf, sizeof(rawbuf)); @@ -647,7 +648,7 @@ void test_nghttp3_conn_read_control(void) { void test_nghttp3_conn_write_control(void) { const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = {0}; nghttp3_settings settings; nghttp3_vec vec[256]; nghttp3_ssize sveccnt; @@ -655,7 +656,6 @@ void test_nghttp3_conn_write_control(void) { int64_t stream_id; int fin; - memset(&callbacks, 0, sizeof(callbacks)); nghttp3_settings_default(&settings); rv = nghttp3_conn_server_new(&conn, &callbacks, &settings, mem, NULL); @@ -682,7 +682,9 @@ void test_nghttp3_conn_write_control(void) { void test_nghttp3_conn_submit_request(void) { const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = { + .acked_stream_data = acked_stream_data, + }; nghttp3_settings settings; nghttp3_vec vec[256]; nghttp3_ssize sveccnt; @@ -697,16 +699,12 @@ void test_nghttp3_conn_submit_request(void) { uint64_t len; size_t i; nghttp3_stream *stream; - userdata ud; + userdata ud = {0}; nghttp3_data_reader dr; int fin; - memset(&callbacks, 0, sizeof(callbacks)); - memset(&ud, 0, sizeof(ud)); nghttp3_settings_default(&settings); - callbacks.acked_stream_data = acked_stream_data; - ud.data.left = 2000; ud.data.step = 1200; @@ -827,7 +825,11 @@ void test_nghttp3_conn_submit_request(void) { void test_nghttp3_conn_http_request(void) { const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *cl, *sv; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = { + .begin_headers = begin_headers, + .recv_header = recv_header, + .end_headers = end_headers, + }; nghttp3_settings settings; nghttp3_vec vec[256]; nghttp3_ssize sveccnt; @@ -847,18 +849,12 @@ void test_nghttp3_conn_http_request(void) { }; nghttp3_data_reader dr; int fin; - userdata clud, svud; + userdata clud = {0}, svud = {0}; size_t i; size_t nconsumed; size_t nread; - memset(&callbacks, 0, sizeof(callbacks)); nghttp3_settings_default(&settings); - memset(&clud, 0, sizeof(clud)); - - callbacks.begin_headers = begin_headers; - callbacks.recv_header = recv_header; - callbacks.end_headers = end_headers; settings.qpack_max_dtable_capacity = 4096; settings.qpack_blocked_streams = 100; @@ -971,14 +967,13 @@ static void check_http_header(const nghttp3_nv *nva, size_t nvlen, int request, nghttp3_buf buf; nghttp3_frame_headers fr; nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = {0}; nghttp3_settings settings; const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_ssize sconsumed; nghttp3_qpack_encoder qenc; nghttp3_stream *stream; - memset(&callbacks, 0, sizeof(callbacks)); nghttp3_settings_default(&settings); settings.enable_connect_protocol = 1; @@ -1318,7 +1313,7 @@ void test_nghttp3_conn_http_content_length(void) { nghttp3_buf buf; nghttp3_frame_headers fr; nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = {0}; nghttp3_settings settings; const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_ssize sconsumed; @@ -1335,7 +1330,6 @@ void test_nghttp3_conn_http_content_length(void) { MAKE_NV("content-length", "9000000000"), }; - memset(&callbacks, 0, sizeof(callbacks)); nghttp3_settings_default(&settings); /* client */ @@ -1393,7 +1387,7 @@ void test_nghttp3_conn_http_content_length_mismatch(void) { nghttp3_buf buf; nghttp3_frame_headers fr; nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = {0}; nghttp3_settings settings; const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_ssize sconsumed; @@ -1412,7 +1406,6 @@ void test_nghttp3_conn_http_content_length_mismatch(void) { int rv; nghttp3_stream *stream; - memset(&callbacks, 0, sizeof(callbacks)); nghttp3_settings_default(&settings); /* content-length is 20, but no DATA is present and see fin */ @@ -1572,7 +1565,7 @@ void test_nghttp3_conn_http_non_final_response(void) { nghttp3_buf buf; nghttp3_frame_headers fr; nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = {0}; nghttp3_settings settings; const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_ssize sconsumed; @@ -1588,7 +1581,6 @@ void test_nghttp3_conn_http_non_final_response(void) { }; nghttp3_stream *stream; - memset(&callbacks, 0, sizeof(callbacks)); nghttp3_settings_default(&settings); /* non-final followed by DATA is illegal. */ @@ -1683,7 +1675,7 @@ void test_nghttp3_conn_http_trailers(void) { nghttp3_buf buf; nghttp3_frame_headers fr; nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = {0}; nghttp3_settings settings; const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_ssize sconsumed; @@ -1706,7 +1698,6 @@ void test_nghttp3_conn_http_trailers(void) { }; nghttp3_stream *stream; - memset(&callbacks, 0, sizeof(callbacks)); nghttp3_settings_default(&settings); /* final response followed by trailers */ @@ -2003,7 +1994,7 @@ void test_nghttp3_conn_http_ignore_content_length(void) { nghttp3_buf buf; nghttp3_frame_headers fr; nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = {0}; nghttp3_settings settings; const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_ssize sconsumed; @@ -2024,7 +2015,6 @@ void test_nghttp3_conn_http_ignore_content_length(void) { int rv; nghttp3_stream *stream; - memset(&callbacks, 0, sizeof(callbacks)); nghttp3_settings_default(&settings); /* If status code is 304, content-length must be ignored. */ @@ -2117,7 +2107,7 @@ void test_nghttp3_conn_http_record_request_method(void) { nghttp3_buf buf; nghttp3_frame_headers fr; nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = {0}; nghttp3_settings settings; const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_ssize sconsumed; @@ -2138,7 +2128,6 @@ void test_nghttp3_conn_http_record_request_method(void) { }; nghttp3_stream *stream; - memset(&callbacks, 0, sizeof(callbacks)); nghttp3_settings_default(&settings); /* content-length is not allowed with 200 status code in response to @@ -2199,7 +2188,10 @@ void test_nghttp3_conn_http_error(void) { nghttp3_buf buf, ebuf; nghttp3_frame_headers fr; nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = { + .stop_sending = stop_sending, + .reset_stream = reset_stream, + }; const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_ssize sconsumed; nghttp3_qpack_encoder qenc; @@ -2216,12 +2208,9 @@ void test_nghttp3_conn_http_error(void) { MAKE_NV(":method", "GET"), MAKE_NV(":authority", "localhost"), }; - userdata ud; + userdata ud = {0}; nghttp3_stream *stream; - memset(&callbacks, 0, sizeof(callbacks)); - callbacks.stop_sending = stop_sending; - callbacks.reset_stream = reset_stream; nghttp3_settings_default(&settings); settings.qpack_max_dtable_capacity = 4096; settings.qpack_blocked_streams = 100; @@ -2229,7 +2218,6 @@ void test_nghttp3_conn_http_error(void) { /* duplicated :scheme */ nghttp3_buf_wrap_init(&buf, rawbuf, sizeof(rawbuf)); nghttp3_qpack_encoder_init(&qenc, 0, mem); - memset(&ud, 0, sizeof(ud)); fr.hd.type = NGHTTP3_FRAME_HEADERS; fr.nva = (nghttp3_nv *)dupschemenv; @@ -2382,7 +2370,7 @@ void test_nghttp3_conn_http_error(void) { void test_nghttp3_conn_qpack_blocked_stream(void) { const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = {0}; nghttp3_settings settings; nghttp3_qpack_encoder qenc; int rv; @@ -2403,7 +2391,6 @@ void test_nghttp3_conn_qpack_blocked_stream(void) { nghttp3_ssize sconsumed; nghttp3_stream *stream; - memset(&callbacks, 0, sizeof(callbacks)); nghttp3_settings_default(&settings); settings.qpack_max_dtable_capacity = 4096; settings.qpack_blocked_streams = 100; @@ -2536,7 +2523,7 @@ void test_nghttp3_conn_qpack_blocked_stream(void) { void test_nghttp3_conn_just_fin(void) { const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = {0}; nghttp3_settings settings; nghttp3_vec vec[256]; nghttp3_ssize sveccnt; @@ -2550,11 +2537,9 @@ void test_nghttp3_conn_just_fin(void) { }; nghttp3_data_reader dr; int fin; - userdata ud; + userdata ud = {0}; - memset(&callbacks, 0, sizeof(callbacks)); nghttp3_settings_default(&settings); - memset(&ud, 0, sizeof(ud)); nghttp3_conn_client_new(&conn, &callbacks, &settings, mem, &ud); @@ -2646,7 +2631,7 @@ void test_nghttp3_conn_just_fin(void) { void test_nghttp3_conn_submit_response_read_blocked(void) { const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = {0}; nghttp3_settings settings; const nghttp3_nv nva[] = { MAKE_NV(":status", "200"), @@ -2660,7 +2645,6 @@ void test_nghttp3_conn_submit_response_read_blocked(void) { nghttp3_data_reader dr = {step_then_block_read_data}; userdata ud; - memset(&callbacks, 0, sizeof(callbacks)); nghttp3_settings_default(&settings); /* Make sure that flushing serialized data while @@ -2699,12 +2683,11 @@ void test_nghttp3_conn_submit_response_read_blocked(void) { void test_nghttp3_conn_recv_uni(void) { const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = {0}; nghttp3_settings settings; nghttp3_ssize nread; uint8_t buf[256]; - memset(&callbacks, 0, sizeof(callbacks)); nghttp3_settings_default(&settings); /* 0 length unidirectional stream must be ignored */ @@ -2753,7 +2736,9 @@ void test_nghttp3_conn_recv_uni(void) { void test_nghttp3_conn_recv_goaway(void) { const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = { + .shutdown = conn_shutdown, + }; nghttp3_settings settings; nghttp3_frame fr; uint8_t rawbuf[1024]; @@ -2766,11 +2751,8 @@ void test_nghttp3_conn_recv_goaway(void) { MAKE_NV(":method", "GET"), }; int rv; - userdata ud; + userdata ud = {0}; - memset(&callbacks, 0, sizeof(callbacks)); - callbacks.shutdown = conn_shutdown; - memset(&ud, 0, sizeof(ud)); nghttp3_settings_default(&settings); nghttp3_buf_wrap_init(&buf, rawbuf, sizeof(rawbuf)); @@ -2883,7 +2865,10 @@ void test_nghttp3_conn_recv_goaway(void) { void test_nghttp3_conn_shutdown_server(void) { const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = { + .stop_sending = stop_sending, + .reset_stream = reset_stream, + }; nghttp3_settings settings; nghttp3_frame fr; uint8_t rawbuf[1024]; @@ -2904,9 +2889,6 @@ void test_nghttp3_conn_shutdown_server(void) { int64_t stream_id; int fin; - memset(&callbacks, 0, sizeof(callbacks)); - callbacks.stop_sending = stop_sending; - callbacks.reset_stream = reset_stream; nghttp3_settings_default(&settings); nghttp3_buf_wrap_init(&buf, rawbuf, sizeof(rawbuf)); @@ -2975,7 +2957,10 @@ void test_nghttp3_conn_shutdown_server(void) { void test_nghttp3_conn_shutdown_client(void) { const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = { + .stop_sending = stop_sending, + .reset_stream = reset_stream, + }; nghttp3_settings settings; uint8_t rawbuf[1024]; nghttp3_buf buf; @@ -2992,9 +2977,6 @@ void test_nghttp3_conn_shutdown_client(void) { int64_t stream_id; int fin; - memset(&callbacks, 0, sizeof(callbacks)); - callbacks.stop_sending = stop_sending; - callbacks.reset_stream = reset_stream; nghttp3_settings_default(&settings); nghttp3_buf_wrap_init(&buf, rawbuf, sizeof(rawbuf)); @@ -3030,7 +3012,7 @@ void test_nghttp3_conn_shutdown_client(void) { void test_nghttp3_conn_priority_update(void) { const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = {0}; nghttp3_settings settings; nghttp3_frame fr; nghttp3_ssize nconsumed; @@ -3038,7 +3020,7 @@ void test_nghttp3_conn_priority_update(void) { nghttp3_buf buf; nghttp3_stream *stream; int rv; - userdata ud; + userdata ud = {0}; nghttp3_qpack_encoder qenc; const nghttp3_nv nva[] = { MAKE_NV(":path", "/"), MAKE_NV(":authority", "example.com"), @@ -3046,8 +3028,6 @@ void test_nghttp3_conn_priority_update(void) { MAKE_NV("priority", "u=5, i"), }; - memset(&callbacks, 0, sizeof(callbacks)); - memset(&ud, 0, sizeof(ud)); nghttp3_settings_default(&settings); nghttp3_buf_wrap_init(&buf, rawbuf, sizeof(rawbuf)); @@ -3214,14 +3194,13 @@ void test_nghttp3_conn_priority_update(void) { void test_nghttp3_conn_request_priority(void) { const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = {0}; nghttp3_settings settings; nghttp3_frame fr; nghttp3_ssize nconsumed; uint8_t rawbuf[2048]; nghttp3_buf buf; nghttp3_stream *stream; - userdata ud; nghttp3_qpack_encoder qenc; const nghttp3_nv nva[] = { MAKE_NV(":path", "/"), MAKE_NV(":authority", "example.com"), @@ -3234,8 +3213,6 @@ void test_nghttp3_conn_request_priority(void) { MAKE_NV("priority", "u=5, i"), MAKE_NV("priority", "i, u=x"), }; - memset(&callbacks, 0, sizeof(callbacks)); - memset(&ud, 0, sizeof(ud)); nghttp3_settings_default(&settings); nghttp3_buf_wrap_init(&buf, rawbuf, sizeof(rawbuf)); @@ -3323,7 +3300,7 @@ void test_nghttp3_conn_request_priority(void) { void test_nghttp3_conn_set_stream_priority(void) { const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = {0}; nghttp3_settings settings; const nghttp3_nv nva[] = { MAKE_NV(":path", "/"), @@ -3337,7 +3314,6 @@ void test_nghttp3_conn_set_stream_priority(void) { nghttp3_stream *stream; size_t i; - memset(&callbacks, 0, sizeof(callbacks)); nghttp3_settings_default(&settings); /* Update stream priority by client */ @@ -3422,7 +3398,9 @@ void test_nghttp3_conn_set_stream_priority(void) { void test_nghttp3_conn_shutdown_stream_read(void) { const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = { + .deferred_consume = deferred_consume, + }; nghttp3_settings settings; nghttp3_qpack_encoder qenc; int rv; @@ -3445,8 +3423,6 @@ void test_nghttp3_conn_shutdown_stream_read(void) { userdata ud; size_t indatalen; - memset(&callbacks, 0, sizeof(callbacks)); - callbacks.deferred_consume = deferred_consume; nghttp3_settings_default(&settings); settings.qpack_max_dtable_capacity = 4096; settings.qpack_blocked_streams = 100; @@ -3533,7 +3509,7 @@ void test_nghttp3_conn_stream_data_overflow(void) { #if SIZE_MAX > UINT32_MAX const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = {0}; nghttp3_settings settings; const nghttp3_nv nva[] = { MAKE_NV(":path", "/"), @@ -3548,7 +3524,6 @@ void test_nghttp3_conn_stream_data_overflow(void) { nghttp3_data_reader dr; int fin; - memset(&callbacks, 0, sizeof(callbacks)); nghttp3_settings_default(&settings); /* Specify NGHTTP3_MAX_VARINT + 1 bytes data in @@ -3631,7 +3606,7 @@ void test_nghttp3_conn_stream_data_overflow(void) { void test_nghttp3_conn_get_frame_payload_left(void) { const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = {0}; nghttp3_settings settings; struct { nghttp3_frame_settings settings; @@ -3649,7 +3624,6 @@ void test_nghttp3_conn_get_frame_payload_left(void) { }; nghttp3_qpack_encoder qenc; - memset(&callbacks, 0, sizeof(callbacks)); nghttp3_settings_default(&settings); nghttp3_buf_wrap_init(&buf, rawbuf, sizeof(rawbuf)); @@ -3726,7 +3700,9 @@ void test_nghttp3_conn_get_frame_payload_left(void) { void test_nghttp3_conn_update_ack_offset(void) { const nghttp3_mem *mem = nghttp3_mem_default(); nghttp3_conn *conn; - nghttp3_callbacks callbacks; + nghttp3_callbacks callbacks = { + .acked_stream_data = acked_stream_data, + }; nghttp3_settings settings; nghttp3_vec vec[256]; nghttp3_ssize sveccnt; @@ -3740,17 +3716,13 @@ void test_nghttp3_conn_update_ack_offset(void) { }; uint64_t len; nghttp3_stream *stream; - userdata ud; + userdata ud = {0}; nghttp3_data_reader dr; int fin; uint64_t ack_offset; - memset(&callbacks, 0, sizeof(callbacks)); - memset(&ud, 0, sizeof(ud)); nghttp3_settings_default(&settings); - callbacks.acked_stream_data = acked_stream_data; - ud.data.left = 2000; ud.data.step = 1333; diff --git a/tests/nghttp3_http_test.c b/tests/nghttp3_http_test.c index 8669d1c..b87a556 100644 --- a/tests/nghttp3_http_test.c +++ b/tests/nghttp3_http_test.c @@ -38,14 +38,18 @@ static const MunitTest tests[] = { }; const MunitSuite http_suite = { - "/http", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE, + .prefix = "/http", + .tests = tests, }; void test_nghttp3_http_parse_priority(void) { int rv; { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = ""; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -56,7 +60,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = ""; rv = nghttp3_pri_parse_priority(&pri, v, sizeof(v) - 1); @@ -67,7 +74,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "u=1"; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -78,7 +88,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "i"; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -89,7 +102,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "i=?0"; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -100,7 +116,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "u=7,i"; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -111,7 +130,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "u=0,i=?0"; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -122,7 +144,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "u=3, i"; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -133,7 +158,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "u=0, i, i=?0, u=6"; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -144,7 +172,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "u=0,"; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -153,7 +184,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "u=0, "; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -162,7 +196,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "u="; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -171,7 +208,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "u"; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -180,7 +220,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "i=?1"; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -191,7 +234,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "i=?2"; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -200,7 +246,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "i=?"; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -209,7 +258,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "i="; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -218,7 +270,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "u=-1"; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -227,7 +282,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "u=8"; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v) - 1); @@ -236,7 +294,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = "i=?0, u=1, a=(x y z), u=2; i=?0;foo=\",,,\", i=?1;i=?0; u=6"; @@ -248,7 +309,10 @@ void test_nghttp3_http_parse_priority(void) { } { - nghttp3_pri pri = {(uint32_t)-1, UINT8_MAX}; + nghttp3_pri pri = { + .urgency = (uint32_t)-1, + .inc = UINT8_MAX, + }; const uint8_t v[] = {'u', '='}; rv = nghttp3_http_parse_priority(&pri, v, sizeof(v)); diff --git a/tests/nghttp3_qpack_test.c b/tests/nghttp3_qpack_test.c index 96b7387..deab4e0 100644 --- a/tests/nghttp3_qpack_test.c +++ b/tests/nghttp3_qpack_test.c @@ -47,7 +47,8 @@ static const MunitTest tests[] = { }; const MunitSuite qpack_suite = { - "/qpack", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE, + .prefix = "/qpack", + .tests = tests, }; static void check_decode_header(nghttp3_qpack_decoder *dec, nghttp3_buf *pbuf, diff --git a/tests/nghttp3_test_helper.c b/tests/nghttp3_test_helper.c index ef3bf2a..1462182 100644 --- a/tests/nghttp3_test_helper.c +++ b/tests/nghttp3_test_helper.c @@ -128,10 +128,13 @@ void nghttp3_write_frame_qpack_dyn(nghttp3_buf *dest, nghttp3_buf *ebuf, } void nghttp3_write_frame_data(nghttp3_buf *dest, size_t len) { - nghttp3_frame_data fr; - - fr.hd.type = NGHTTP3_FRAME_DATA; - fr.hd.length = (int64_t)len; + nghttp3_frame_data fr = { + .hd = + { + .type = NGHTTP3_FRAME_DATA, + .length = (int64_t)len, + }, + }; dest->last = nghttp3_frame_write_hd(dest->last, &fr.hd); memset(dest->last, 0, len); diff --git a/tests/nghttp3_test_helper.h b/tests/nghttp3_test_helper.h index 9579d38..28c7c3d 100644 --- a/tests/nghttp3_test_helper.h +++ b/tests/nghttp3_test_helper.h @@ -35,8 +35,10 @@ #define MAKE_NV(NAME, VALUE) \ { \ - (uint8_t *)(NAME), (uint8_t *)(VALUE), sizeof((NAME)) - 1, \ - sizeof((VALUE)) - 1, NGHTTP3_NV_FLAG_NONE, \ + .name = (uint8_t *)(NAME), \ + .value = (uint8_t *)(VALUE), \ + .namelen = sizeof((NAME)) - 1, \ + .valuelen = sizeof((VALUE)) - 1, \ } /* diff --git a/tests/nghttp3_tnode_test.c b/tests/nghttp3_tnode_test.c index 72548ec..ec667f1 100644 --- a/tests/nghttp3_tnode_test.c +++ b/tests/nghttp3_tnode_test.c @@ -36,7 +36,8 @@ static const MunitTest tests[] = { }; const MunitSuite tnode_suite = { - "/tnode", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE, + .prefix = "/tnode", + .tests = tests, }; static int cycle_less(const nghttp3_pq_entry *lhsx,