From: Karel Zak Date: Fri, 28 Mar 2014 11:25:58 +0000 (+0100) Subject: libsmartcols: always deallocate in scols_reset_cell() X-Git-Tag: v2.25-rc1~352 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=92e0d69c7377b24eadc347451b80e8b59ffe0cb9;p=thirdparty%2Futil-linux.git libsmartcols: always deallocate in scols_reset_cell() Signed-off-by: Karel Zak --- diff --git a/libsmartcols/src/cell.c b/libsmartcols/src/cell.c index e70fb39128..28e2557860 100644 --- a/libsmartcols/src/cell.c +++ b/libsmartcols/src/cell.c @@ -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; } diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index 18ca22a144..585b5e196f 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -49,8 +49,6 @@ struct libscols_symbols { struct libscols_cell { char *data; char *color; - - unsigned int is_ref; /* data is reference to foreign pointer */ };