libplugin: add helpers for multi-string options
What changed, and why it matters
This commit is a routine code cleanup: it moves two small helper functions for handling plugin options that can be set multiple times from a test-only file into the shared plugin library so other plugins can reuse them. There is no security-relevant change.
No security action needed. Treat as normal refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch relocates multi_string_option() and string_array_jsonfmt() from tests/plugins/test_libplugin.c into plugins/libplugin.c and exposes them via plugins/libplugin.h. The test plugin is updated to call the now-public helpers. The code behavior is identical; only the location and visibility of the helpers changed.
Changed components
plugins/libplugin.cplugins/libplugin.htests/plugins/test_libplugin.cInspect captured patch +24 / −21
diff --git a/plugins/libplugin.c b/plugins/libplugin.c
index b39058d4..db2e53fb 100644
--- a/plugins/libplugin.c
+++ b/plugins/libplugin.c
@@ -1688,6 +1688,14 @@ char *charp_option(struct command *cmd, const char *arg, bool check_only, char *
return NULL;
}
+char *multi_string_option(struct command *cmd, const char *arg, bool check_only,
+ const char ***arr)
+{
+ if (!check_only)
+ tal_arr_expand(arr, tal_strdup(*arr, arg));
+ return NULL;
+}
+
bool u64_jsonfmt(struct command *cmd, struct json_stream *js, const char *fieldname, u64 *i)
{
json_add_u64(js, fieldname, *i);
@@ -1720,6 +1728,16 @@ bool charp_jsonfmt(struct command *cmd, struct json_stream *js, const char *fiel
return true;
}
+bool string_array_jsonfmt(struct command *cmd, struct json_stream *js,
+ const char *fieldname, const char ***arr)
+{
+ json_array_start(js, fieldname);
+ for (size_t i = 0; i < tal_count(*arr); i++)
+ json_add_string(js, NULL, (*arr)[i]);
+ json_array_end(js);
+ return true;
+}
+
bool flag_jsonfmt(struct command *cmd, struct json_stream *js, const char *fieldname, bool *i)
{
/* Don't print if the default (false) */
diff --git a/plugins/libplugin.h b/plugins/libplugin.h
index bddba28f..3e435f65 100644
--- a/plugins/libplugin.h
+++ b/plugins/libplugin.h
@@ -638,6 +638,8 @@ char *u32_option(struct command *cmd, const char *arg, bool check_only, u32 *i);
char *u16_option(struct command *cmd, const char *arg, bool check_only, u16 *i);
char *bool_option(struct command *cmd, const char *arg, bool check_only, bool *i);
char *charp_option(struct command *cmd, const char *arg, bool check_only, char **p);
+char *multi_string_option(struct command *cmd, const char *arg, bool check_only,
+ const char ***arr);
char *flag_option(struct command *cmd, const char *arg, bool check_only, bool *i);
bool u64_jsonfmt(struct command *cmd, struct json_stream *js, const char *fieldname,
@@ -650,6 +652,8 @@ bool bool_jsonfmt(struct command *cmd, struct json_stream *js, const char *field
bool *i);
bool charp_jsonfmt(struct command *cmd, struct json_stream *js, const char *fieldname,
char **p);
+bool string_array_jsonfmt(struct command *cmd, struct json_stream *js,
+ const char *fieldname, const char ***arr);
/* Usually equivalent to NULL, since flag must default to false be useful! */
bool flag_jsonfmt(struct command *cmd, struct json_stream *js, const char *fieldname,
diff --git a/tests/plugins/test_libplugin.c b/tests/plugins/test_libplugin.c
index a89b6164..907d5c4b 100644
--- a/tests/plugins/test_libplugin.c
+++ b/tests/plugins/test_libplugin.c
@@ -372,25 +372,6 @@ static const struct plugin_notification notifs[] = { {
}
};
-static char *set_multi_string_option(struct command *cmd,
- const char *arg,
- bool check_only,
- const char ***arr)
-{
- if (!check_only)
- tal_arr_expand(arr, tal_strdup(*arr, arg));
- return NULL;
-}
-
-static bool multi_string_jsonfmt(struct command *cmd, struct json_stream *js, const char *fieldname, const char ***arr)
-{
- json_array_start(js, fieldname);
- for (size_t i = 0; i < tal_count(*arr); i++)
- json_add_string(js, NULL, (*arr)[i]);
- json_array_end(js);
- return true;
-}
-
int main(int argc, char *argv[])
{
setup_locale();
@@ -436,8 +417,8 @@ int main(int argc, char *argv[])
plugin_option_multi("multiopt",
"string",
"Set me multiple times!",
- set_multi_string_option,
- multi_string_jsonfmt,
+ multi_string_option,
+ string_array_jsonfmt,
&tlp->strarr),
NULL);
}
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.