From: Thomas Weißschuh Date: Thu, 28 Sep 2023 06:24:10 +0000 (+0200) Subject: libsmartcols: (cell) consistently handle NULL argument X-Git-Tag: v2.40-rc1~222^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=47a54ac9b37ccf2ff830c61da52b062bd76508f4;p=thirdparty%2Futil-linux.git libsmartcols: (cell) consistently handle NULL argument Some functions allow passing NULL, some don't. Even users internal to util-linux are passing NULL parameters to these functions, even for ones where it is not allowed. Unify the API. As there may be external users that rely on the NULL-accepting behavior for some functions use it everywhere. Signed-off-by: Thomas Weißschuh --- diff --git a/libsmartcols/src/cell.c b/libsmartcols/src/cell.c index 5b831235b4..f1d488fe87 100644 --- a/libsmartcols/src/cell.c +++ b/libsmartcols/src/cell.c @@ -62,6 +62,9 @@ int scols_reset_cell(struct libscols_cell *ce) */ int scols_cell_set_data(struct libscols_cell *ce, const char *data) { + if (!ce) + return -EINVAL; + return strdup_to_struct_member(ce, data, data); } @@ -120,7 +123,7 @@ int scols_cell_set_userdata(struct libscols_cell *ce, void *data) */ void *scols_cell_get_userdata(struct libscols_cell *ce) { - return ce->userdata; + return ce ? ce->userdata : NULL; } /** @@ -166,6 +169,9 @@ int scols_cmpstr_cells(struct libscols_cell *a, */ int scols_cell_set_color(struct libscols_cell *ce, const char *color) { + if (!ce) + return -EINVAL; + if (color && !color_is_sequence(color)) { char *seq = color_get_sequence(color); if (!seq) @@ -185,6 +191,9 @@ int scols_cell_set_color(struct libscols_cell *ce, const char *color) */ const char *scols_cell_get_color(const struct libscols_cell *ce) { + if (!ce) + return NULL; + return ce->color; } @@ -214,7 +223,7 @@ int scols_cell_set_flags(struct libscols_cell *ce, int flags) */ int scols_cell_get_flags(const struct libscols_cell *ce) { - return ce->flags; + return ce ? ce->flags : 0; } /** @@ -227,9 +236,11 @@ int scols_cell_get_flags(const struct libscols_cell *ce) */ int scols_cell_get_alignment(const struct libscols_cell *ce) { - if (ce->flags & SCOLS_CELL_FL_RIGHT) + int flags = scols_cell_get_flags(ce); + + if (flags & SCOLS_CELL_FL_RIGHT) return SCOLS_CELL_FL_RIGHT; - if (ce->flags & SCOLS_CELL_FL_CENTER) + if (flags & SCOLS_CELL_FL_CENTER) return SCOLS_CELL_FL_CENTER; return SCOLS_CELL_FL_LEFT; /* default */ @@ -249,6 +260,9 @@ int scols_cell_copy_content(struct libscols_cell *dest, { int rc; + if (!dest || !src) + return -EINVAL; + rc = scols_cell_set_data(dest, scols_cell_get_data(src)); if (!rc) rc = scols_cell_set_color(dest, scols_cell_get_color(src));