common: export helper membuf_tal_realloc.
What changed, and why it matters
This commit simply moves an existing memory-resizing helper function from two separate source files into a shared utility file so both places can call the same copy. The actual behavior of the code does not change; it is a routine code cleanup (refactoring).
No security action required; treat as normal refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch removes duplicate static definitions of membuf_tal_realloc() from common/msg_queue.c and plugins/libplugin.c, adds a single exported copy named membuf_tal_resize() to common/utils.c with a declaration in common/utils.h, and updates the two call sites. The function body is identical: it wraps tal_resize() for use as a membuf realloc callback. No functional or security-relevant change is introduced.
Changed components
common/msg_queue.cplugins/libplugin.ccommon/utils.ccommon/utils.hInspect captured patch +17 / −23
diff --git a/common/msg_queue.c b/common/msg_queue.c
index 566bb3bf..091b8380 100644
--- a/common/msg_queue.c
+++ b/common/msg_queue.c
@@ -2,6 +2,7 @@
#include <ccan/membuf/membuf.h>
#include <common/daemon.h>
#include <common/msg_queue.h>
+#include <common/utils.h>
#include <wire/wire.h>
static bool warned_once;
@@ -33,21 +34,11 @@ static void destroy_msg_queue(struct msg_queue *q)
}
}
-/* Realloc helper for tal membufs */
-static void *membuf_tal_realloc(struct membuf *mb, void *rawelems,
- size_t newsize)
-{
- char *p = rawelems;
-
- tal_resize(&p, newsize);
- return p;
-}
-
struct msg_queue *msg_queue_new(const tal_t *ctx, bool fd_passing)
{
struct msg_queue *q = tal(ctx, struct msg_queue);
q->fd_passing = fd_passing;
- membuf_init(&q->mb, tal_arr(q, const u8 *, 0), 0, membuf_tal_realloc);
+ membuf_init(&q->mb, tal_arr(q, const u8 *, 0), 0, membuf_tal_resize);
if (q->fd_passing)
tal_add_destructor(q, destroy_msg_queue);
diff --git a/common/utils.c b/common/utils.c
index 01047ea5..e467c19d 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -194,3 +194,13 @@ char *str_lowering(const void *ctx, const char *string TAKES)
for (char *p = ret; *p; p++) *p = tolower(*p);
return ret;
}
+
+/* Realloc helper for tal membufs */
+void *membuf_tal_resize(struct membuf *mb, void *rawelems, size_t newsize)
+{
+ char *p = rawelems;
+
+ tal_resize(&p, newsize);
+ return p;
+}
+
diff --git a/common/utils.h b/common/utils.h
index 45b91f87..a4f3ed97 100644
--- a/common/utils.h
+++ b/common/utils.h
@@ -8,8 +8,8 @@
#include <ccan/tal/tal.h>
#include <secp256k1.h>
+struct membuf;
extern secp256k1_context *secp256k1_ctx;
-
extern const struct chainparams *chainparams;
/* Unsigned min/max macros: BUILD_ASSERT make sure types are unsigned */
@@ -163,6 +163,9 @@ extern const tal_t *wally_tal_ctx;
* Returns created temporary path name at *created if successful. */
int tmpdir_mkstemp(const tal_t *ctx, const char *template TAKES, char **created);
+/* For use with membuf_init */
+void *membuf_tal_resize(struct membuf *mb, void *rawelems, size_t newsize);
+
/**
* tal_strlowering - return the same string by in lower case.
* @ctx: the context to tal from (often NULL)
diff --git a/plugins/libplugin.c b/plugins/libplugin.c
index 5b5c2f4e..836e1768 100644
--- a/plugins/libplugin.c
+++ b/plugins/libplugin.c
@@ -538,16 +538,6 @@ struct json_out *json_out_obj(const tal_t *ctx,
return jout;
}
-/* Realloc helper for tal membufs */
-static void *membuf_tal_realloc(struct membuf *mb, void *rawelems,
- size_t newsize)
-{
- char *p = rawelems;
-
- tal_resize(&p, newsize);
- return p;
-}
-
static int read_json_from_rpc(struct plugin *p)
{
char *end;
@@ -1597,7 +1587,7 @@ static struct command_result *handle_init(struct command *cmd,
with_rpc = true;
membuf_init(&p->rpc_conn->mb, tal_arr(p, char, READ_CHUNKSIZE),
- READ_CHUNKSIZE, membuf_tal_realloc);
+ READ_CHUNKSIZE, membuf_tal_resize);
} else
with_rpc = false;
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.