From 0c989ec195a1683c0557a4c43a2f51c4530db8b0 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Fri, 11 Sep 2026 22:25:53 -0700 Subject: [PATCH] CHB: avoid incompatible function pointer calls Use adapters for memory pool allocators instead of casting their function pointers to the generic chain buffer callback types. Casting does not make an indirect call through an incompatible function pointer type valid. UndefinedBehaviorSanitizer reports these calls. --- src/njs_chb.c | 14 ++++++++++++++ src/njs_chb.h | 8 ++++---- src/qjs.c | 14 ++++++++++++++ src/qjs.h | 2 ++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/njs_chb.c b/src/njs_chb.c index 87e2e2b3..eace09fe 100644 --- a/src/njs_chb.c +++ b/src/njs_chb.c @@ -26,6 +26,20 @@ njs_chb_init(njs_chb_t *chain, void *pool, njs_chb_alloc_t alloc, } +void * +njs_chb_mp_alloc(void *pool, size_t size) +{ + return njs_mp_alloc(pool, size); +} + + +void +njs_chb_mp_free(void *pool, void *p) +{ + njs_mp_free(pool, p); +} + + void njs_chb_append0(njs_chb_t *chain, const char *msg, size_t len) { diff --git a/src/njs_chb.h b/src/njs_chb.h index 4d24a0eb..6e0bab65 100644 --- a/src/njs_chb.h +++ b/src/njs_chb.h @@ -41,12 +41,12 @@ typedef struct { void njs_chb_init(njs_chb_t *chain, void *pool, njs_chb_alloc_t alloc, njs_chb_free_t free); +void *njs_chb_mp_alloc(void *pool, size_t size); +void njs_chb_mp_free(void *pool, void *p); #define NJS_CHB_MP_INIT(chain, mp) \ - njs_chb_init(chain, mp, (njs_chb_alloc_t) njs_mp_alloc, \ - (njs_chb_free_t) njs_mp_free) + njs_chb_init(chain, mp, njs_chb_mp_alloc, njs_chb_mp_free) #define NJS_CHB_CTX_INIT(chain, ctx) \ - njs_chb_init(chain, ctx, (njs_chb_alloc_t) js_malloc, \ - (njs_chb_free_t) js_free) + njs_chb_init(chain, ctx, qjs_chb_ctx_alloc, qjs_chb_ctx_free) #define NJS_CHB_MP_INIT_MAX(chain, mp, max) \ (NJS_CHB_MP_INIT(chain, mp), (chain)->max_size = (max)) #define NJS_CHB_CTX_INIT_MAX(chain, ctx, max) \ diff --git a/src/qjs.c b/src/qjs.c index 90a464d6..78d8f3fd 100644 --- a/src/qjs.c +++ b/src/qjs.c @@ -324,6 +324,20 @@ failed: } +void * +qjs_chb_ctx_alloc(void *ctx, size_t size) +{ + return js_malloc(ctx, size); +} + + +void +qjs_chb_ctx_free(void *ctx, void *p) +{ + js_free(ctx, p); +} + + JSValue qjs_call_exit_hook(JSContext *ctx) { diff --git a/src/qjs.h b/src/qjs.h index 32aa80d9..69c593ab 100644 --- a/src/qjs.h +++ b/src/qjs.h @@ -58,6 +58,8 @@ typedef struct { JSContext *qjs_new_context(JSRuntime *rt, qjs_module_t **addons); JSValue qjs_call_exit_hook(JSContext *ctx); +void *qjs_chb_ctx_alloc(void *ctx, size_t size); +void qjs_chb_ctx_free(void *ctx, void *p); JSValue qjs_new_uint8_array(JSContext *ctx, int argc, JSValueConst *argv); -- 2.47.3