prevent double free 50/45350/2
authorshingil.kang <shingil.kang@samsung.com>
Fri, 31 Jul 2015 03:31:53 +0000 (12:31 +0900)
committershingil.kang <shingil.kang@samsung.com>
Wed, 5 Aug 2015 07:18:16 +0000 (16:18 +0900)
- Use SAFE_FREE macro to free
- SAFE_FREE checks whether if pointer is null and free if not null,

Change-Id: I606cfed410b6d87c382ae6b629fa5542329bccde
Signed-off-by: shingil.kang <shingil.kang@samsung.com>
17 files changed:
src/auto_complete.c
src/command_function.c
src/commandline.c
src/file_sync_client.c
src/file_sync_functions.c
src/linkedlist.c
src/listener.c
src/log.c
src/sdb_client.c
src/sdb_map.c
src/sockets.c
src/strutils.c
src/transport.c
src/usb_darwin.c
src/usb_windows.c
src/utils.h
src/utils_windows.c

index 2ec8813164eaeee1f46318308da0726d9b129072..0acb299466472919a6e2dd82c32804426cd6a9a4 100644 (file)
@@ -361,8 +361,7 @@ static int parse_uninstall(int argc, char** argv) {
                 }
             }
         }
-
-        free(pkg_id_tokens);
+        SAFE_FREE(pkg_id_tokens);
     }
 
     return -1;
@@ -507,7 +506,7 @@ static void print_local_dirlist_with_complete_flag(int argc, char** argv) {
         else {
             print_local_dirlist(NULL, argv);
         }
-        free(src);
+        SAFE_FREE(src);
     }
 }
 
@@ -570,9 +569,7 @@ static void print_local_dirlist(char* src_dir, char** not_complete_char) {
 
 finalize:
     closedir(d);
-    if(pwd_flag) {
-        free(src_dir);
-    }
+    SAFE_FREE(src_dir);
 }
 
 static void print_remote_dirlist_with_complete_flag(int argc, char** argv) {
@@ -586,7 +583,7 @@ static void print_remote_dirlist_with_complete_flag(int argc, char** argv) {
             *(++last) = '\0';
             print_remote_dirlist(src, argv);
         }
-        free(src);
+        SAFE_FREE(src);
     }
 }
 
index 4f03c5bd2852b8ba21e3739fe42ab3f88e782a32..2d02d300604cd707bbdd1e98603ba24d84b41ecb 100644 (file)
@@ -385,10 +385,7 @@ int status_window(int argc, char ** argv) {
     for(;;) {
         sdb_sleep_ms(250);
 
-        if(state) {
-            free(state);
-            state = 0;
-        }
+        SAFE_FREE(state);
 
         D(COMMANDLINE_MSG_FULL_CMD, argv[0], full_cmd);
         state = sdb_query(full_cmd);
index 124be9e10e889f4f06b9597129a24aedd562db78..04adcdd8ded0f26b47d2f1aecc897ba95e29a66e 100755 (executable)
@@ -205,7 +205,7 @@ static void *stdin_read_thread(void *x)
     HANDLE* console_input_handle_ptr = args[2];
     INPUT_RECORD i_record;
     DWORD cNumRead;
-    free(args[0]);
+    SAFE_FREE(args[0]);
 
     if(args[1] != NULL) {
         while(1) {
@@ -629,17 +629,13 @@ int process_cmdline(int argc, char** argv) {
         if(argc < minargs + 1) {
             print_error(SDB_MESSAGE_ERROR, ERR_COMMAND_TOO_FEW_ARGUMENTS , NULL);
             print_info("sdb %s %s", argv[0], command->argdesc);
-            if (serial != NULL) {
-                free(serial);
-            }
+            SAFE_FREE(serial);
             return 1;
         }
         if(argc > maxargs + 1 && maxargs > -1) {
             print_error(SDB_MESSAGE_ERROR, ERR_COMMAND_TOO_MANY_ARGUMENTS , NULL);
             print_info("sdb %s %s", argv[0], command->argdesc);
-            if (serial != NULL) {
-                free(serial);
-            }
+            SAFE_FREE(serial);
             return 1;
         }
         target_serial = serial;
@@ -652,9 +648,7 @@ int process_cmdline(int argc, char** argv) {
     }
 
     print_help(opt_list, cmd_list);
-    if (serial != NULL) {
-        free(serial);
-    }
+    SAFE_FREE(serial);
     return 1;
 }
 
@@ -717,8 +711,8 @@ static void print_help(LIST_NODE* optlist, LIST_NODE* cmdlist) {
         }
     }
 
-    free(append_str);
-    free(help_str);
+    SAFE_FREE(append_str);
+    SAFE_FREE(help_str);
 }
 
 
index fefb57104460393c2b44fac0d4dbde06eea2b87f..0cd47980a9476fae59adfb0feeeda03fbdfbee33 100644 (file)
@@ -162,16 +162,9 @@ error:
 static void free_copyinfo(void* data) {
     COPY_INFO* info = (COPY_INFO*)data;
     if(info != NULL) {
-        if(info->src != NULL) {
-            free(info->src);
-            info->src = NULL;
-        }
-        if(info->dst != NULL) {
-            free(info->dst);
-            info->dst = NULL;
-        }
-        free(info);
-        info = NULL;
+        SAFE_FREE(info->src);
+        SAFE_FREE(info->dst);
+        SAFE_FREE(info);
     }
 }
 
@@ -299,18 +292,18 @@ int do_sync_copy(char* srcp, char* dstp, SYNC_INFO* sync_info, int is_utf8) {
                 else {
                     if(!file_copy(src_fd, dst_fd, copy_info, sync_info)) {
                         sync_info->copied++;
-                        free(copy_info);
-                        free(src_p);
-                        free(dst_p);
+                        SAFE_FREE(copy_info);
+                        SAFE_FREE(src_p);
+                        SAFE_FREE(dst_p);
                         continue;
                     }
                 }
 skip_in:
                 fprintf(stderr,"skipped: %s -> %s\n", src_p, dst_p);
                 sync_info->skipped++;
-                free(copy_info);
-                free(src_p);
-                free(dst_p);
+                SAFE_FREE(copy_info);
+                SAFE_FREE(src_p);
+                SAFE_FREE(dst_p);
             }
             free_list(entry_list, no_free);
         }
index 1e5d1222ea2832cbd03e9c2529b23f0d3c3492ab..9e987b2096d64c22fe8bcdc9f45402f18d0e50f9 100644 (file)
@@ -511,8 +511,8 @@ int getdirlist_local(int fd, char* src_dir, char* dst_dir, LIST_NODE** dirlist,
         else {
             fprintf(stderr,"skipped: %s -> %s\n", src_full_path, dst_full_path);
             sync_info->skipped++;
-            free(src_full_path);
-            free(dst_full_path);
+            SAFE_FREE(src_full_path);
+            SAFE_FREE(dst_full_path);
         }
     }
 
@@ -599,8 +599,8 @@ int getdirlist_remote(int fd, char* src_dir, char* dst_dir, LIST_NODE** dirlist,
             print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_STAT_FAIL, file_name), ERR_GENERAL_UNKNOWN);
             fprintf(stderr,"skipped: %s -> %s\n", src_full_path, dst_full_path);
             sync_info->skipped++;
-            free(src_full_path);
-            free(dst_full_path);
+            SAFE_FREE(src_full_path);
+            SAFE_FREE(dst_full_path);
             continue;
         }
         st.st_size = ltohl(msg.dent.size);
index 4c6beb49abb2314b0f3284aae90d3b6d47a8210b..520aa0e7d4d7bef106152805b76e50071016d41c 100644 (file)
@@ -26,6 +26,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include "utils.h"
 #include "linkedlist.h"
 #include "file_sync_functions.h"
 
@@ -90,7 +91,7 @@ void free_list(LIST_NODE* listptr, void(free_func)(void*)) {
         LIST_NODE* prev = currentptr;
         currentptr = currentptr->next_ptr;
         free_func(prev->data);
-        free(prev);
+        SAFE_FREE(prev);
     }
 }
 
@@ -114,13 +115,11 @@ void remove_node(LIST_NODE** listptr, LIST_NODE* remove_node, void(free_func)(vo
     }
 
     free_func(remove_node->data);
-    free(remove_node);
+    SAFE_FREE(remove_node);
 }
 
 static void default_free(void* data) {
-    if(data != NULL) {
-        free(data);
-    }
+    SAFE_FREE(data);
 }
 
 void remove_first(LIST_NODE** listptr, void(free_func)(void*)) {
@@ -137,6 +136,6 @@ void remove_first(LIST_NODE** listptr, void(free_func)(void*)) {
         LIST_NODE* removeptr = *listptr;
         *listptr = curptr;
         free_func(removeptr->data);
-        free(removeptr);
+        SAFE_FREE(removeptr);
     }
 }
index b5b2d369fb49fed0a8f48ae9956b1fd91d1c0b65..057090c6ce9ca9c8496bad64ccd701584143af44 100755 (executable)
@@ -44,7 +44,7 @@ void  free_listener(void* data)
 {
     LISTENER* listener = data;
     fdevent_remove(&(listener->fde));
-    free(listener);
+    SAFE_FREE(listener);
 }
 
 int install_listener(int local_port, int connect_port, TRANSPORT* transport, LISTENER_TYPE ltype)
index 54ee740ea4c7278e7693198cc9ee65162f112b3d..f17ee03e5142425bd52b3c86eebc654a67bc07ac 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -210,7 +210,7 @@ static void log_parse(char* args) {
             {
                 if (!strcmp("all",log_levels[i].name)) {
                     loglevel_mask = ~0;
-                    free(levels);
+                    SAFE_FREE(levels);
                     return;
                 }
                 loglevel_mask |= 1 << log_levels[i].level;
@@ -218,7 +218,7 @@ static void log_parse(char* args) {
             }
         }
     }
-    free(levels);
+    SAFE_FREE(levels);
 }
 
 void log_init(void)
index 0e4af6209222f5d210709731b780645dfd4c7213..5d4505be831454caf0d36f3a0e4fd216d494bd58 100644 (file)
@@ -507,7 +507,7 @@ char *sdb_query(const char *service)
         sdb_close(fd);
         return tmp;
     }
-    free(tmp);
+    SAFE_FREE(tmp);
 
 oops:
     sdb_close(fd);
index 58577bbf05a8603c7006d7cbf7462a0497c4297a..2651c6a97ed05109ac4e69d0eabc093242046edc 100644 (file)
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include "sdb_map.h"
 #include "linkedlist.h"
+#include "utils.h"
 
 static LIST_NODE* find_in_list(MAP* this, LIST_NODE* list, MAP_KEY key);
 static int default_hash(MAP* this, MAP_KEY key);
@@ -73,7 +74,7 @@ static void default_free(void* node) {
         if(_node->value != NULL ) {
 //            free(_node->value);
         }
-        free(_node);
+        SAFE_FREE(_node);
     }
 }
 
@@ -149,6 +150,6 @@ void map_clear(MAP* this) {
             free_list(this->map_node_list[i], this->freedata);
         }
 
-        free(this->map_node_list);
+        SAFE_FREE(this->map_node_list);
     }
 }
index 58a931e68376426fe605a413eff757aa56fa2fb7..66d69a5aafbfd20e8a8470443e958d01d49b0fce 100755 (executable)
@@ -170,7 +170,7 @@ static void destroy_socket(void* data) {
 //        remote_con_l_table[id] = 0;
 //    }
     socket->local_id = 0;
-    free(socket);
+    SAFE_FREE(socket);
 }
 
 // be sure to hold the socket list lock when calling this
@@ -869,7 +869,7 @@ static int handle_host_request(char *service, SDB_SOCKET* socket)
             LOG_ERROR("found more than one devices matched: %d\n", ret);
             sendfailmsg(socket->fd, serial);
         }
-        free(serial);
+        SAFE_FREE(serial);
         return 0;
     }
     // return a list of all devices
index 987851ae57665ee23e1f702c12b24ce3759b4687..96ac0120c33bb1064ea7b7f7b6ae539a7cd336c5 100755 (executable)
@@ -35,9 +35,7 @@ void free_strings(char **array, int n)
     int i;
 
     for(i = 0; i < n; i++) {
-        if (array[i] != NULL) {
-            free(array[i]);
-        }
+        SAFE_FREE(array[i]);
     }
 }
 
index eaec0879222f22efc8d25cc09d3109c361258c9f..8caf9098403ddb6e5ae70f45f5c4073ed8e92b11 100755 (executable)
@@ -257,7 +257,7 @@ static void handle_packet(PACKET *p, TRANSPORT *t)
     }
     D("Unknown packet command %08x\n", p->msg.command);
     put_apacket(p);
-    free(t_packet);
+    SAFE_FREE(t_packet);
 }
 
 #define CNXN_DATA_MAX_TOKENS 3
@@ -478,12 +478,9 @@ static void remove_transport(TRANSPORT *t)
 
     run_transport_close(t);
 
-    if (t->serial)
-        free(t->serial);
-    if (t->device_name)
-        free(t->device_name);
-
-    free(t);
+    SAFE_FREE(t->serial);
+    SAFE_FREE(t->device_name);
+    SAFE_FREE(t);
 }
 
 
@@ -511,7 +508,7 @@ static void transport_unref(TRANSPORT *t)
         curptr = curptr->next_ptr;
         if (tmp->type == kTransportUsb) {
             if (tmp->device_name && sscanf(tmp->device_name, "device-%d", &nr) == 1) {
-                free(tmp->device_name);
+                SAFE_FREE(tmp->device_name);
                 asprintf(&tmp->device_name, "device-%d", nr - 1);
             }
         }
@@ -791,7 +788,7 @@ void wakeup_select_func(int _fd, unsigned ev, void *data) {
     TRANSPORT* t= t_packet->t;
     D("T(%s)\n", t->serial);
     PACKET* p = t_packet->p;
-    free(t_packet);
+    SAFE_FREE(t_packet);
 
     if(p == NULL) {
         D("T(%S) packet NULL\n", t->serial);
@@ -941,5 +938,5 @@ PACKET *get_apacket(void)
 void put_apacket(void *p)
 {
     PACKET* packet = p;
-    free(packet);
+    SAFE_FREE(packet);
 }
index 03c16e0c92480640f622e9e0c8a04b13ac5efe5f..e84120c39c0a739aae33130b74b3625978ad9790 100755 (executable)
@@ -318,7 +318,7 @@ void DeviceAdded(void *refCon, io_iterator_t iterator) {
 
         kr = FindInterfaces((IOUSBInterfaceInterface**) interface, usbVendor, usbProduct, handle);
         if (kIOReturnSuccess != kr) {
-            free(handle);
+            SAFE_FREE(handle);
             handle = NULL;
             (*interface)->Release(interface);
             continue;
index a07aeeffd0c359f9eb2f158aaf967cf62db27619..a8126d9ee55d6ad33936955df6ab759e9f1e561c 100644 (file)
@@ -359,18 +359,14 @@ int usb_find_devices(GUID deviceClassID) {
         //Check for some other error
         if (!bResult) {
             LOG_DEBUG("fail to setdup get device interface detail: %d\n", GetLastError());
-            if (detailData != NULL) {
-                free(detailData);
-            }
+               SAFE_FREE(detailData);
             break;
         }
 
         //copy device path
         s_strncpy(devicePath, detailData->DevicePath, sizeof(devicePath));
 
-        if (detailData != NULL) {
-            free(detailData);
-        }
+        SAFE_FREE(detailData);
 
         if (!is_device_registered(devicePath)) {
             struct usb_handle *hnd = usb_open(devicePath);
@@ -383,12 +379,12 @@ int usb_find_devices(GUID deviceClassID) {
                     } else {
                         LOG_DEBUG("fail to register usb\n");
                         win_usb_close(hnd);
-                        free(hnd);
+                        SAFE_FREE(hnd);
                     }
                 } else {
                     LOG_DEBUG("fail to get usb serial name: kick? close?\n");
                     win_usb_close(hnd);
-                    free(hnd);
+                    SAFE_FREE(hnd);
                 }
             }
         }
index 2889f57334b042b26a01f19226e8bcd97a6cde3b..8068d2df31ad80486e8b6cfbee5f7ddb3ee63b11 100755 (executable)
@@ -139,5 +139,6 @@ int sdb_port_listen(uint32_t inet, int port, int type);
 #define DEVICENAME_MAX 256
 #define VMS_PATH OS_PATH_SEPARATOR_STR "vms" OS_PATH_SEPARATOR_STR
 #define DEFAULT_DEVICENAME "<unknown>"
+#define SAFE_FREE(x) if ((x) != NULL) { free(x); x=NULL; }
 
 #endif /* _SDB_UTILS_H */
index 04c322504a59626ad8d4bff81027043846d4d669..c0322dbb007507e9010dcdb9189a1a53b201b811 100755 (executable)
@@ -306,7 +306,7 @@ static int _fh_close(SDB_HANDLE* _h) {
            _h->u.file_handle = INVALID_HANDLE_VALUE;
        }
        sdb_handle_map_remove(_h->fd);
-       free(_h);
+       SAFE_FREE(_h);
        sdb_mutex_lock(&_win32_lock, "_fh_close");
        total_handle_number--;
        sdb_mutex_unlock(&_win32_lock, "_fh_close");
@@ -972,7 +972,7 @@ static char* _ansi_to_utf8(const char *str) {
     utf8 = (char *) calloc(len + 1, sizeof(char));
 
     WideCharToMultiByte(CP_UTF8, 0, unicode, -1, utf8, len, NULL, NULL);
-    free(unicode);
+    SAFE_FREE(unicode);
 
     return utf8;
 }