]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
client: rework command catenation
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 3 May 2022 11:25:11 +0000 (13:25 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Wed, 4 May 2022 12:17:32 +0000 (14:17 +0200)
Use snprintf() instead of strcat() and don't try to parse commands
longer than 2048 characters to make it consistent with the chrony.conf
parser, avoid memory allocation, and not rely on the system ARG_MAX to
keep the length sane.

client.c

index a2c780fd2e759f7488c3ece08e1a69349c26d4e4..242bd6dfae044caec35c87f6832a5480dcedafaf 100644 (file)
--- a/client.c
+++ b/client.c
@@ -3245,37 +3245,30 @@ process_line(char *line)
 
 /* ================================================== */
 
+#define MAX_LINE_LENGTH 2048
+
 static int
 process_args(int argc, char **argv, int multi)
 {
-  int total_length, i, ret = 0;
-  char *line;
-
-  total_length = 0;
-  for(i=0; i<argc; i++) {
-    total_length += strlen(argv[i]) + 1;
-  }
-
-  line = (char *) Malloc((2 + total_length) * sizeof(char));
+  char line[MAX_LINE_LENGTH];
+  int i, l, ret = 0;
 
-  for (i = 0; i < argc; i++) {
-    line[0] = '\0';
-    if (multi) {
-      strcat(line, argv[i]);
-    } else {
-      for (; i < argc; i++) {
-        strcat(line, argv[i]);
-        if (i + 1 < argc)
-          strcat(line, " ");
-      }
+  for (i = l = 0; i < argc; i++) {
+    l += snprintf(line + l, sizeof (line) - l, "%s ", argv[i]);
+    if (l >= sizeof (line)) {
+      LOG(LOGS_ERR, "Command too long");
+      return 0;
     }
 
+    if (!multi && i + 1 < argc)
+      continue;
+
     ret = process_line(line);
     if (!ret || quit)
       break;
-  }
 
-  Free(line);
+    l = 0;
+  }
 
   return ret;
 }