]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: always deallocate in scols_reset_cell()
authorKarel Zak <kzak@redhat.com>
Fri, 28 Mar 2014 11:25:58 +0000 (12:25 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 3 Apr 2014 10:29:19 +0000 (12:29 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/src/cell.c
libsmartcols/src/smartcolsP.h

index e70fb3912806e38fb8589a92db776c62e3c4a89c..28e255786090d5533c262137b954df79c2789b44 100644 (file)
@@ -44,8 +44,7 @@ int scols_reset_cell(struct libscols_cell *ce)
        if (!ce)
                return -EINVAL;
 
-       if (!ce->is_ref)
-               free(ce->data);
+       free(ce->data);
        free(ce->color);
        memset(ce, 0, sizeof(*ce));
        return 0;
@@ -73,10 +72,8 @@ int scols_cell_set_data(struct libscols_cell *ce, const char *str)
                if (!p)
                        return -ENOMEM;
        }
-       if (!ce->is_ref)
-               free(ce->data);
+       free(ce->data);
        ce->data = p;
-       ce->is_ref = 0;
        return 0;
 }
 
@@ -85,22 +82,21 @@ int scols_cell_set_data(struct libscols_cell *ce, const char *str)
  * @ce: a pointer to a struct libscols_cell instance
  * @str: user data
  *
- * Adds a reference to @str to @ce.
+ * Adds a reference to @str to @ce. The pointer is deallocated by
+ * scols_reset_cell() or scols_unref_line(). This function is mostly designed
+ * for situations when the data for the cell are already composed in allocated
+ * memory (e.g. asprintf()) to avoid extra unnecessary strdup().
  *
  * Returns: 0, a negative value in case of an error.
  */
 int scols_cell_refer_data(struct libscols_cell *ce, char *str)
 {
-       char *p = NULL;
-
        assert(ce);
 
        if (!ce)
                return -EINVAL;
-       if (!ce->is_ref)
-               free(ce->data);
-       ce->data = p;
-       ce->is_ref = 1;
+       free(ce->data);
+       ce->data = str;
        return 0;
 }
 
index 18ca22a144d4249a1a41308cc81cd01135e758e2..585b5e196f3c16fbd4fce6a8f9c862f731469d81 100644 (file)
@@ -49,8 +49,6 @@ struct libscols_symbols {
 struct libscols_cell {
        char    *data;
        char    *color;
-
-       unsigned int is_ref;    /* data is reference to foreign pointer */
 };