diff --git a/lib/nghttp2_frame.c b/lib/nghttp2_frame.c index 12e9a36f..01bd5e77 100644 --- a/lib/nghttp2_frame.c +++ b/lib/nghttp2_frame.c @@ -697,7 +697,8 @@ ssize_t nghttp2_nv_array_from_cstr(nghttp2_nv **nva_ptr, const char **nv) buflen += len; } nvlen = i/2; - if(nvlen == 0) { + /* If all name/value pair is 0-length, remove them */ + if(nvlen == 0 || buflen == 0) { *nva_ptr = NULL; return 0; } diff --git a/tests/main.c b/tests/main.c index b8f814f2..b72236ad 100644 --- a/tests/main.c +++ b/tests/main.c @@ -187,6 +187,8 @@ int main(int argc, char* argv[]) test_nghttp2_frame_pack_goaway) || !CU_add_test(pSuite, "frame_pack_window_update", test_nghttp2_frame_pack_window_update) || + !CU_add_test(pSuite, "nv_array_from_cstr", + test_nghttp2_nv_array_from_cstr) || !CU_add_test(pSuite, "hd_deflate", test_nghttp2_hd_deflate) || !CU_add_test(pSuite, "hd_inflate_indname_inc", test_nghttp2_hd_inflate_indname_inc) || diff --git a/tests/nghttp2_frame_test.c b/tests/nghttp2_frame_test.c index a99be49c..b5463168 100644 --- a/tests/nghttp2_frame_test.c +++ b/tests/nghttp2_frame_test.c @@ -334,3 +334,45 @@ void test_nghttp2_frame_pack_window_update(void) nghttp2_frame_window_update_free(&oframe); nghttp2_frame_window_update_free(&frame); } + +void test_nghttp2_nv_array_from_cstr(void) +{ + const char *empty[] = {NULL}; + const char *emptynv[] = {"", "", "", "", NULL}; + const char *nv[] = {"alpha", "bravo", "charlie", "delta", NULL}; + const char *bignv[] = {"echo", NULL, NULL}; + size_t bigvallen = 64*1024; + char *bigval = malloc(bigvallen+1); + nghttp2_nv *nva; + ssize_t rv; + + memset(bigval, '0', bigvallen); + bigval[bigvallen] = '\0'; + bignv[1] = bigval; + + rv = nghttp2_nv_array_from_cstr(&nva, empty); + CU_ASSERT(0 == rv); + CU_ASSERT(NULL == nva); + + rv = nghttp2_nv_array_from_cstr(&nva, emptynv); + CU_ASSERT(0 == rv); + CU_ASSERT(NULL == nva); + + rv = nghttp2_nv_array_from_cstr(&nva, nv); + CU_ASSERT(2 == rv); + CU_ASSERT(nva[0].namelen == 5); + CU_ASSERT(0 == memcmp("alpha", nva[0].name, 5)); + CU_ASSERT(nva[0].valuelen = 5); + CU_ASSERT(0 == memcmp("bravo", nva[0].value, 5)); + CU_ASSERT(nva[1].namelen == 7); + CU_ASSERT(0 == memcmp("charlie", nva[1].name, 7)); + CU_ASSERT(nva[1].valuelen == 5); + CU_ASSERT(0 == memcmp("delta", nva[1].value, 5)); + + nghttp2_nv_array_del(nva); + + rv = nghttp2_nv_array_from_cstr(&nva, bignv); + CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT == rv); + + free(bigval); +} diff --git a/tests/nghttp2_frame_test.h b/tests/nghttp2_frame_test.h index 680433d6..dd6e2bdb 100644 --- a/tests/nghttp2_frame_test.h +++ b/tests/nghttp2_frame_test.h @@ -36,5 +36,6 @@ void test_nghttp2_frame_pack_settings(void); void test_nghttp2_frame_pack_ping(void); void test_nghttp2_frame_pack_goaway(void); void test_nghttp2_frame_pack_window_update(void); +void test_nghttp2_nv_array_from_cstr(void); #endif /* NGHTTP2_FRAME_TEST_H */