Use seperated variable for realloc return value 82/138582/2
authorSemun Lee <semun.lee@samsung.com>
Thu, 13 Jul 2017 01:48:13 +0000 (10:48 +0900)
committerSemun Lee <semun.lee@samsung.com>
Thu, 13 Jul 2017 02:38:37 +0000 (11:38 +0900)
Using the same variable for return value and input parameter
of realloc() may cause memory leak when realloc() fails
to allocate new memory.

Change-Id: If2ee2134b272e6fc158fc400dc352704d81b0e24
Signed-off-by: Semun Lee <semun.lee@samsung.com>
preference/preference.c

index 6e78b4e..246f011 100755 (executable)
@@ -830,6 +830,7 @@ static int _preference_get_key_filesys(keynode_t *keynode, int* io_errno)
        double value_dbl = 0;
        char file_buf[BUF_LEN] = {0,};
        char *value = NULL;
+       char *tmp_value;
        int value_size = 0;
 
 retry_open:
@@ -948,11 +949,14 @@ retry:
                while (fgets(file_buf, sizeof(file_buf), fp)) {
                        if (value) {
                                value_size = value_size + strlen(file_buf);
-                               value = (char *) realloc(value, value_size);
-                               if (value == NULL) {
+                               tmp_value = (char *) realloc(value, value_size);
+                               if (tmp_value == NULL) {
                                        func_ret = PREFERENCE_ERROR_OUT_OF_MEMORY;
+                                       free(value);
+                                       value = NULL;
                                        break;
                                }
+                               value = tmp_value;
                                strncat(value, file_buf, strlen(file_buf));
                        } else {
                                value_size = strlen(file_buf) + 1;