]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: (cell) consistently handle NULL argument
authorThomas Weißschuh <thomas@t-8ch.de>
Thu, 28 Sep 2023 06:24:10 +0000 (08:24 +0200)
committerThomas Weißschuh <thomas@t-8ch.de>
Thu, 28 Sep 2023 06:30:26 +0000 (08:30 +0200)
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 <thomas@t-8ch.de>
libsmartcols/src/cell.c

index 5b831235b4a73e563442d055a5931909864f3d86..f1d488fe87a058e63f872d07df38011e141c9cf0 100644 (file)
@@ -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));