Fix: Do not use source argument length in strncpy() 36/221536/2 accepted/tizen/unified/20200108.131426 submit/tizen/20200107.112721
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Fri, 3 Jan 2020 11:02:33 +0000 (12:02 +0100)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Fri, 3 Jan 2020 11:16:38 +0000 (12:16 +0100)
This fixes issue found by gcc 9.x, where strncpy's length
argument was based on source argument length.  This commit
fixes possible bufer overflow.

Change-Id: I933e999d4919e1dbce2834138a612c9f97f916a4

src/test/system_info_test.c

index a8178a503520c8ff22fceb5e45cff733951bcb7d..0f308d3e0736dc8a976ea33edad0dc38e432d4e6 100755 (executable)
@@ -16,7 +16,7 @@
 
 #define DEFAULT_OPT 0
 
-#define CONVERT_ARGUMENT(input_str, output_num, table) \
+#define CONVERT_ARGUMENT(input_str, input_str_len, output_num, table)  \
 {      \
        int __idx;      \
        bool __is_converted = false;    \
@@ -30,7 +30,8 @@
        if (!__is_converted) {  \
                if (input_str[0] == '\0') {     \
                        output_num = table[DEFAULT_OPT].num;    \
-                       strncpy(input_str, table[DEFAULT_OPT].str, strlen(table[DEFAULT_OPT].str) + 1); \
+                       strncpy(input_str, table[DEFAULT_OPT].str, input_str_len - 1);  \
+                       input_str[input_str_len - 1] = '\0'; \
                } else {        \
                        printf("Invalid argument %s\n\n", input_str);   \
                        show_help();    \
@@ -160,9 +161,9 @@ int main(int argc, char *argv[])
        }
 
        /* Convert string arguments into the enum value */
-       CONVERT_ARGUMENT(tag_str, tag_num, tag_table);
-       CONVERT_ARGUMENT(type_str, type_num, type_table);
-       CONVERT_ARGUMENT(runtime_str, runtime_num, runtime_table);
+       CONVERT_ARGUMENT(tag_str, sizeof(tag_str), tag_num, tag_table);
+       CONVERT_ARGUMENT(type_str, sizeof(type_str), type_num, type_table);
+       CONVERT_ARGUMENT(runtime_str, sizeof(runtime_str), runtime_num, runtime_table);
 
        /* Set the runtime environment value */
        switch (runtime_num) {