Fix: logutil: pipe: Do not depend on snprintf() return value for calculations 99/144599/4
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Thu, 17 Aug 2017 08:13:31 +0000 (10:13 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Thu, 17 Aug 2017 11:20:45 +0000 (13:20 +0200)
snprintf() can return numers of characters that "would have been
written if buffer would be large enough", not actual characters
written.

This commits reworks logger request command construction to
avoid snprintf().

Problem pointed out by κΉ€μ›μ˜ <won0.kim@samsung.com> (thank you!)

Change-Id: I664cc0ce779eaff3c004e47fd5102b0576295630

src/logutil/logutil.c

index 61a6679..69e23ce 100755 (executable)
@@ -366,14 +366,15 @@ static int send_logger_request(int argc, char **argv, int sock_fd)
        int len = strlen(request_string);
 
        for (int i = 1; i < argc; i++) {
-               int r = snprintf(request_string + len, sizeof(request_string) - len, " %s", argv[i]);
-               if (r > 0)
-                       len += r;
-               else
+               int arglen = strnlen(argv[i], MAX_LOGGER_REQUEST_LEN);
+               int needed = arglen + 1 /* space */ + 1 /* null byte */;
+               if (len + needed > sizeof(request_string))
                        return 0;
-       }
 
-       len += 1; // for terminating null byte
+               strncat(request_string, " ", 1);
+               strncat(request_string, argv[i], arglen + 1);
+               len += needed;
+       }
 
        ctrl.length += len;
        iov[1].iov_len = len;