From 9089ef1f4f40ee62edc0b214d95d9c182c07deeb Mon Sep 17 00:00:00 2001 From: Ethan Everett <84542561+eeverettrbx@users.noreply.github.com> Date: Fri, 8 Nov 2024 11:49:32 -0800 Subject: [PATCH] nghttp2: use custom memory functions Provide libcurl's memory functions as callbacks to replace nghttp2's own memory functions. This allows custom memory callbacks provided by users of libcurl to be used by nghttp2 as well. Closes #15527 --- lib/cf-h2-proxy.c | 4 +++- lib/http2.c | 28 +++++++++++++++++++++++++++- lib/http2.h | 5 +++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/cf-h2-proxy.c b/lib/cf-h2-proxy.c index 038952d641..e687dd1050 100644 --- a/lib/cf-h2-proxy.c +++ b/lib/cf-h2-proxy.c @@ -277,6 +277,8 @@ static int proxy_h2_client_new(struct Curl_cfilter *cf, { struct cf_h2_proxy_ctx *ctx = cf->ctx; nghttp2_option *o; + nghttp2_mem mem = {NULL, Curl_nghttp2_malloc, Curl_nghttp2_free, + Curl_nghttp2_calloc, Curl_nghttp2_realloc}; int rc = nghttp2_option_new(&o); if(rc) @@ -289,7 +291,7 @@ static int proxy_h2_client_new(struct Curl_cfilter *cf, HTTP field value. */ nghttp2_option_set_no_rfc9113_leading_and_trailing_ws_validation(o, 1); #endif - rc = nghttp2_session_client_new2(&ctx->h2, cbs, cf, o); + rc = nghttp2_session_client_new3(&ctx->h2, cbs, cf, o, &mem); nghttp2_option_del(o); return rc; } diff --git a/lib/http2.c b/lib/http2.c index f1ee0cf9ec..dbe6f1aaae 100644 --- a/lib/http2.c +++ b/lib/http2.c @@ -433,6 +433,8 @@ static int h2_client_new(struct Curl_cfilter *cf, { struct cf_h2_ctx *ctx = cf->ctx; nghttp2_option *o; + nghttp2_mem mem = {NULL, Curl_nghttp2_malloc, Curl_nghttp2_free, + Curl_nghttp2_calloc, Curl_nghttp2_realloc}; int rc = nghttp2_option_new(&o); if(rc) @@ -445,7 +447,7 @@ static int h2_client_new(struct Curl_cfilter *cf, HTTP field value. */ nghttp2_option_set_no_rfc9113_leading_and_trailing_ws_validation(o, 1); #endif - rc = nghttp2_session_client_new2(&ctx->h2, cbs, cf, o); + rc = nghttp2_session_client_new3(&ctx->h2, cbs, cf, o, &mem); nghttp2_option_del(o); return rc; } @@ -2960,6 +2962,30 @@ bool Curl_h2_http_1_1_error(struct Curl_easy *data) return FALSE; } +void *Curl_nghttp2_malloc(size_t size, void *user_data) +{ + (void)user_data; + return Curl_cmalloc(size); +} + +void Curl_nghttp2_free(void *ptr, void *user_data) +{ + (void)user_data; + Curl_cfree(ptr); +} + +void *Curl_nghttp2_calloc(size_t nmemb, size_t size, void *user_data) +{ + (void)user_data; + return Curl_ccalloc(nmemb, size); +} + +void *Curl_nghttp2_realloc(void *ptr, size_t size, void *user_data) +{ + (void)user_data; + return Curl_crealloc(ptr, size); +} + #else /* !USE_NGHTTP2 */ /* Satisfy external references even if http2 is not compiled in. */ diff --git a/lib/http2.h b/lib/http2.h index 80e183480a..dbb1784661 100644 --- a/lib/http2.h +++ b/lib/http2.h @@ -60,6 +60,11 @@ CURLcode Curl_http2_upgrade(struct Curl_easy *data, struct connectdata *conn, int sockindex, const char *ptr, size_t nread); +void *Curl_nghttp2_malloc(size_t size, void *user_data); +void Curl_nghttp2_free(void *ptr, void *user_data); +void *Curl_nghttp2_calloc(size_t nmemb, size_t size, void *user_data); +void *Curl_nghttp2_realloc(void *ptr, size_t size, void *user_data); + extern struct Curl_cftype Curl_cft_nghttp2; #else /* USE_NGHTTP2 */