]> git.kaiwu.me - njs.git/commitdiff
CHB: avoid incompatible function pointer calls
authorDmitry Volyntsev <xeioex@nginx.com>
Sat, 12 Sep 2026 05:25:53 +0000 (22:25 -0700)
committerDmitry Volyntsev <xeioexception@gmail.com>
Mon, 14 Sep 2026 17:53:52 +0000 (10:53 -0700)
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
src/njs_chb.h
src/qjs.c
src/qjs.h

index 87e2e2b3ee8c87199a71911448919b0bc57515c9..eace09fe7ef5baeb381467a2bb5ad73d67847f23 100644 (file)
@@ -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)
 {
index 4d24a0ebf2ec2f5d3b25ff13681371491206bef0..6e0bab6590bc85cf4a9e2d586a13b07c47a35094 100644 (file)
@@ -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)                                \
index 90a464d6e5c9eb663b9c688123f6fbb0e76244d5..78d8f3fdfb15c6720f3fd521bca3a547c0715907 100644 (file)
--- 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)
 {
index 32aa80d992be67e472e92d7083080332d38080b8..69c593ab093eedbd07f72d5a037590ac28f16393 100644 (file)
--- 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);