delta-verifier.c: Fix minor bugs 25/287325/3 accepted/tizen/unified/20230130.165637
authorAntoni Adaszkiewicz <a.adaszkiewi@samsung.com>
Thu, 26 Jan 2023 15:10:22 +0000 (16:10 +0100)
committerAntoni Adaszkiewicz <a.adaszkiewi@samsung.com>
Thu, 26 Jan 2023 15:23:31 +0000 (16:23 +0100)
Use strncpy() rather than strcpy() and fix memory leaks.

Change-Id: I389ecadac9aa3bd7c9ba41e8c8a411e833609165

delta-verifier/delta-verifier.c

index bab4f6e15bc38b580473834813ac66ccda4d96d7..635fb6f033fee8703a92b749684d26c40e73c5c4 100644 (file)
@@ -172,13 +172,15 @@ static int write_info_from_file(FILE* read_file, FILE* write_file)
                                        read_word = strtok(NULL, separators);
 
                                        char lowercase_name[MAX_STRING];
-                                       strcpy(lowercase_name, full_var_names[i]);
+                                       strncpy(lowercase_name, full_var_names[i], MAX_STRING);
+                                       lowercase_name[MAX_STRING - 1] = '\0';
                                        for (int j = 0; j < strlen(lowercase_name); j++)
                                                lowercase_name[j] = tolower(lowercase_name[j]);
 
                                        char to_write[MAX_STRING];
                                        if (snprintf(to_write, MAX_STRING, "%s=%s\n", lowercase_name, read_word) < 0) {
                                                ERR_ERRNO("snprintf()");
+                                               free(read_buf);
                                                return -1;
                                        }
 
@@ -223,7 +225,7 @@ static char** alloc_string_array(const int lines)
                arr[i] = (char*)malloc(MAX_STRING);
                if (arr[i] == NULL) {
                        for (int j = 0; j < i; j++)
-                               free(arr[i]);
+                               free(arr[j]);
 
                        free(arr);
                        ERR_ERRNO("malloc()");
@@ -256,7 +258,8 @@ static char** get_string_array(char* buff, const int lines)
 
        while (curr_line != NULL)
        {
-               strcpy(string_array[id], curr_line);
+               strncpy(string_array[id], curr_line, MAX_STRING);
+               string_array[id][MAX_STRING - 1] = '\0';
                id++;
                curr_line = strtok(NULL, separator);
        }