monitor: fix buffer overflow when terminal width > 255
authorCeleste Liu <coelacanthushex@gmail.com>
Tue, 17 Sep 2024 06:30:46 +0000 (14:30 +0800)
committerWootak Jung <wootak.jung@samsung.com>
Thu, 20 Feb 2025 07:43:23 +0000 (16:43 +0900)
In current code, we create line buffer with size 256, which can contains
255 ASCII characters. But in modern system, terminal can have larger
width. It may cause buffer overflow in snprintf() text.

limits.h provides constant LINE_MAX.

    {LINE_MAX}
        Unless otherwise noted, the maximum length, in bytes, of a
        utility's input line (either standard input or another
        file), when the utility is described as processing text
        files. The length includes room for the trailing <newline>.
        Minimum Acceptable Value: {_POSIX2_LINE_MAX}

Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
monitor/packet.c

index 4078eb62ec1bbe6d0a54b9f7e3b2cb16296805a3..22f8624f0d34f2b6d234e657773e5c0c9f22dedc 100755 (executable)
@@ -25,6 +25,7 @@
 #include <time.h>
 #include <sys/time.h>
 #include <sys/socket.h>
+#include <limits.h>
 
 #include "lib/bluetooth.h"
 #include "lib/uuid.h"
@@ -375,7 +376,7 @@ static void print_packet(struct timeval *tv, struct ucred *cred, char ident,
                                        const char *text, const char *extra)
 {
        int col = num_columns();
-       char line[256], ts_str[96], pid_str[140];
+       char line[LINE_MAX], ts_str[96], pid_str[140];
        int n, ts_len = 0, ts_pos = 0, len = 0, pos = 0;
        static size_t last_frame;