]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: (filter) fix dereferences and leaks [coverity scann]
authorKarel Zak <kzak@redhat.com>
Fri, 24 Nov 2023 09:20:54 +0000 (10:20 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 24 Nov 2023 09:20:54 +0000 (10:20 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/src/filter-expr.c
libsmartcols/src/filter-param.c
libsmartcols/src/filter-parser.y

index 16a476bde8efe4150972cac9d657644412b66f44..7e559c408a4e4c71ead3e9be57375cc0c8542cc4 100644 (file)
@@ -22,7 +22,11 @@ struct filter_node *filter_new_expr(
        struct filter_expr *n = (struct filter_expr *) __filter_new_node(
                                        F_NODE_EXPR, sizeof(struct filter_expr));
 
+       if (!n)
+               return NULL;
+
        n->type = type;
+
        switch (type) {
        case F_EXPR_AND:
        case F_EXPR_OR:
@@ -41,6 +45,7 @@ struct filter_node *filter_new_expr(
                n->right = right;
                break;
        }
+
        return (struct filter_node *) n;
 }
 
index 6cce77a082688808add0691b0a7502a056e47a5f..f7d243d426f2850a6176d3b070faa922aae2f453 100644 (file)
@@ -98,12 +98,17 @@ struct filter_node *filter_new_param(
        struct filter_param *n = (struct filter_param *) __filter_new_node(
                                        F_NODE_PARAM,
                                        sizeof(struct filter_param));
+       if (!n)
+               return NULL;
+
        n->type = type;
        n->holder = holder;
        INIT_LIST_HEAD(&n->pr_params);
 
-       if (param_set_data(n, type, data) != 0)
+       if (param_set_data(n, type, data) != 0) {
+               filter_free_param(n);
                return NULL;
+       }
 
        if (holder == F_HOLDER_COLUMN) {
                n->holder_name = strdup((char *) data);
@@ -357,8 +362,7 @@ int filter_count_param(struct libscols_filter *fltr,
                struct libscols_line *ln,
                struct libscols_counter *ct)
 {
-       unsigned long long num;
-
+       unsigned long long num = 0;
 
        if (ct->func == SCOLS_COUNTER_COUNT) {
                ct->result++;
@@ -372,11 +376,12 @@ int filter_count_param(struct libscols_filter *fltr,
                rc = fetch_holder_data(fltr, ct->param, ln);
                if (rc)
                        return rc;
-       }
-       if (ct->param->empty)
-               return -EINVAL;
 
-       num = ct->param->val.num;
+               if (ct->param->empty)
+                       return -EINVAL;
+
+               num = ct->param->val.num;
+       }
 
        switch (ct->func) {
        case SCOLS_COUNTER_MAX:
index 6c32b3f0d2e848631a469fb6eddf34a435ad261e..4cddf26034e8fb04c30f361a62b97c2f0c865c38 100644 (file)
@@ -117,16 +117,16 @@ void yyerror (yyscan_t *locp __attribute__((__unused__)),
 
                if (fltr->errmsg)
                        free(fltr->errmsg);
+
                fltr->errmsg = strdup(msg);
+               if (!fltr->errmsg)
+                       return;
 
                p = strstr(fltr->errmsg, "T_");
                if (p) {
                        size_t sz = strlen(fltr->errmsg);
                        memmove(p, p + 2, sz - 1 - (p - fltr->errmsg));
                }
-
-               if (!fltr->errmsg)
-                       return;
        }
        errno = EINVAL;
 }