shared: fix multiple memory ownership problems 01/199001/1
authorMichal Bloch <m.bloch@samsung.com>
Thu, 31 Jan 2019 19:35:42 +0000 (20:35 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Thu, 31 Jan 2019 19:35:42 +0000 (20:35 +0100)
The function was supposed to create a deep copy
of a filterspec list, which it generally seemed
to do if everything went fine.

However:
 * if copying the first filterspec element failed,
   the *original* elements were all freed, which
   resulted in dangling pointers.
 * if copying the N+1'th filterspec failed, any
   subsequent filterspecs were removed from the
   *original* as well.
 * if everything succeeded, the original object
   actually got a copy of the list, and the new
   object got the original list.

Change-Id: I064b54d505c471fb042b036cf72398fb0fa5297c
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
include/logprint.h
src/shared/logprint.c

index 1fa265e..6529b85 100644 (file)
@@ -56,7 +56,7 @@ void log_set_print_format(log_format *p_format, log_print_format format);
 /**
  * Returns a deep copy of the passed object.
  */
-log_format *log_format_from_format(log_format *p_format);
+log_format *log_format_from_format(const log_format *p_format);
 
 /**
  * Returns FORMAT_OFF on invalid string
index bea95b7..a3dab60 100644 (file)
@@ -302,7 +302,7 @@ log_format *log_format_new(void)
  * @return The new structure (or NULL if allocation failed)
  * @see log_format_free
  */
-log_format *log_format_from_format(log_format *p_format)
+log_format *log_format_from_format(const log_format *p_format)
 {
        log_format *p_ret;
        FilterInfo *p_info, *p_info_old = NULL;
@@ -310,7 +310,10 @@ log_format *log_format_from_format(log_format *p_format)
        if (!(p_ret = log_format_new()))
                return NULL;
 
-       *p_ret = *p_format;
+       p_ret->format = p_format->format;
+       p_ret->global_pri = p_format->global_pri;
+       p_ret->exact_global_pri = p_format->exact_global_pri;
+       p_ret->filters = NULL;
 
        p_info = p_format->filters;
 
@@ -323,7 +326,7 @@ log_format *log_format_from_format(log_format *p_format)
                }
 
                if (!p_info_old)
-                       p_format->filters = p_tmp;
+                       p_ret->filters = p_tmp;
                else
                        p_info_old->p_next = p_tmp;