[PATCH] tools/xenstore: use talloc_asprintf_append() in do_control_help()

Juergen Gross posted 1 patch 2 years, 3 months ago
Test gitlab-ci failed
Patches applied successfully (tree, apply log)
git fetch https://gitlab.com/xen-project/patchew/xen tags/patchew/20220120065947.31587-1-jgross@suse.com
tools/xenstore/xenstored_control.c | 27 +++++++--------------------
1 file changed, 7 insertions(+), 20 deletions(-)
[PATCH] tools/xenstore: use talloc_asprintf_append() in do_control_help()
Posted by Juergen Gross 2 years, 3 months ago
Instead of calculating the length of all help output and then
allocating the space for it, just use talloc_asprintf_append() to
expand the text as needed.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 tools/xenstore/xenstored_control.c | 27 +++++++--------------------
 1 file changed, 7 insertions(+), 20 deletions(-)

diff --git a/tools/xenstore/xenstored_control.c b/tools/xenstore/xenstored_control.c
index adb8d51b04..61bcbc069d 100644
--- a/tools/xenstore/xenstored_control.c
+++ b/tools/xenstore/xenstored_control.c
@@ -853,36 +853,23 @@ static struct cmd_s cmds[] = {
 static int do_control_help(void *ctx, struct connection *conn,
 			   char **vec, int num)
 {
-	int cmd, len = 0;
+	int cmd;
 	char *resp;
 
 	if (num)
 		return EINVAL;
 
-	for (cmd = 0; cmd < ARRAY_SIZE(cmds); cmd++) {
-		len += strlen(cmds[cmd].cmd) + 1;
-		len += strlen(cmds[cmd].pars) + 1;
-	}
-	len++;
-
-	resp = talloc_array(ctx, char, len);
+	resp = talloc_asprintf(ctx, "%s", "");
 	if (!resp)
 		return ENOMEM;
-
-	len = 0;
 	for (cmd = 0; cmd < ARRAY_SIZE(cmds); cmd++) {
-		strcpy(resp + len, cmds[cmd].cmd);
-		len += strlen(cmds[cmd].cmd);
-		resp[len] = '\t';
-		len++;
-		strcpy(resp + len, cmds[cmd].pars);
-		len += strlen(cmds[cmd].pars);
-		resp[len] = '\n';
-		len++;
+		resp = talloc_asprintf_append(resp, "%s\t%s\n",
+					      cmds[cmd].cmd, cmds[cmd].pars);
+		if (!resp)
+			return ENOMEM;
 	}
-	resp[len] = 0;
 
-	send_reply(conn, XS_CONTROL, resp, len);
+	send_reply(conn, XS_CONTROL, resp, strlen(resp) + 1);
 	return 0;
 }
 
-- 
2.31.1


Re: [PATCH] tools/xenstore: use talloc_asprintf_append() in do_control_help()
Posted by Anthony PERARD 2 years, 3 months ago
On Thu, Jan 20, 2022 at 07:59:47AM +0100, Juergen Gross wrote:
> Instead of calculating the length of all help output and then
> allocating the space for it, just use talloc_asprintf_append() to
> expand the text as needed.
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>

Reviewed-by: Anthony PERARD <anthony.perard@citrix.com>

Thanks,

-- 
Anthony PERARD