Skip to content

Commit

Permalink
expr.c: Use expr_destroy and expr_clone instead of free and xmemdup.
Browse files Browse the repository at this point in the history
Use the functions for safer memory operation.

Signed-off-by: Han Zhou <hzhou@ovn.org>
Acked-by: Mark Michelson <mmichels@redhat.com>
Acked-by: Numan Siddique <numans@ovn.org>
  • Loading branch information
hzhou8 committed Feb 24, 2022
1 parent 979fecc commit 4b4cadc
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions lib/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ expr_combine(enum expr_type type, struct expr *a, struct expr *b)
} else if (a->type == type) {
if (b->type == type) {
ovs_list_splice(&a->andor, b->andor.next, &b->andor);
free(b);
expr_destroy(b);
} else {
ovs_list_push_back(&a->andor, &b->node);
}
Expand All @@ -210,7 +210,7 @@ expr_insert_andor(struct expr *andor, struct expr *before, struct expr *new)
/* Conjunction junction, what's your function? */
}
ovs_list_splice(&before->node, new->andor.next, &new->andor);
free(new);
expr_destroy(new);
} else {
ovs_list_insert(&before->node, &new->node);
}
Expand Down Expand Up @@ -276,11 +276,11 @@ expr_fix_andor(struct expr *expr, bool short_circuit)

if (ovs_list_is_short(&expr->andor)) {
if (ovs_list_is_empty(&expr->andor)) {
free(expr);
expr_destroy(expr);
return expr_create_boolean(!short_circuit);
} else {
sub = expr_from_node(ovs_list_front(&expr->andor));
free(expr);
sub = expr_from_node(ovs_list_pop_front(&expr->andor));
expr_destroy(expr);
return sub;
}
} else {
Expand Down Expand Up @@ -2096,7 +2096,7 @@ expr_simplify_relational(struct expr *expr)
* and similarly for "tcp.dst <= 1234". */
struct expr *new = NULL;
if (eq) {
new = xmemdup(expr, sizeof *expr);
new = expr_clone(expr);
new->cmp.relop = EXPR_R_EQ;
}

Expand All @@ -2105,7 +2105,7 @@ expr_simplify_relational(struct expr *expr)
z = bitwise_scan(value, sizeof *value, lt, z + 1, end)) {
struct expr *e;

e = xmemdup(expr, sizeof *expr);
e = expr_clone(expr);
e->cmp.relop = EXPR_R_EQ;
bitwise_toggle_bit(&e->cmp.value, sizeof e->cmp.value, z);
bitwise_zero(&e->cmp.value, sizeof e->cmp.value, start, z - start);
Expand Down Expand Up @@ -2324,7 +2324,7 @@ crush_and_string(struct expr *expr, const struct expr_symbol *symbol)
expr_destroy(expr);
return new;
}
free(new);
expr_destroy(new);
break;
case EXPR_T_CONDITION:
OVS_NOT_REACHED();
Expand Down Expand Up @@ -2463,14 +2463,14 @@ crush_and_numeric(struct expr *expr, const struct expr_symbol *symbol)
expr_destroy(sub);
}
}
free(disjuncts);
free(expr);
expr_destroy(disjuncts);
expr_destroy(expr);
if (ovs_list_is_empty(&or->andor)) {
free(or);
expr_destroy(or);
return expr_create_boolean(false);
} else if (ovs_list_is_short(&or->andor)) {
struct expr *cmp = expr_from_node(ovs_list_pop_front(&or->andor));
free(or);
expr_destroy(or);
return cmp;
} else {
return crush_cmps(or, symbol);
Expand Down Expand Up @@ -2517,15 +2517,15 @@ crush_and_numeric(struct expr *expr, const struct expr_symbol *symbol)
}
expr_destroy(as);
expr_destroy(bs);
free(new);
expr_destroy(new);

if (ovs_list_is_empty(&or->andor)) {
expr_destroy(expr);
free(or);
expr_destroy(or);
return expr_create_boolean(false);
} else if (ovs_list_is_short(&or->andor)) {
struct expr *cmp = expr_from_node(ovs_list_pop_front(&or->andor));
free(or);
expr_destroy(or);
if (ovs_list_is_empty(&expr->andor)) {
expr_destroy(expr);
return crush_cmps(cmp, symbol);
Expand Down Expand Up @@ -2675,7 +2675,7 @@ expr_sort(struct expr *expr)
qsort(subs, n, sizeof *subs, compare_expr_sort);

ovs_list_init(&expr->andor);
free(expr);
expr_destroy(expr);
expr = NULL;

for (i = 0; i < n; ) {
Expand Down Expand Up @@ -2708,7 +2708,7 @@ expr_sort(struct expr *expr)
expr = crushed;
break;
} else {
free(crushed);
expr_destroy(crushed);
}
} else {
expr = expr_combine(EXPR_T_AND, expr, crushed);
Expand Down Expand Up @@ -2754,8 +2754,8 @@ expr_normalize_and(struct expr *expr)
}
}
if (ovs_list_is_short(&expr->andor)) {
struct expr *sub = expr_from_node(ovs_list_front(&expr->andor));
free(expr);
struct expr *sub = expr_from_node(ovs_list_pop_front(&expr->andor));
expr_destroy(expr);
return sub;
}

Expand Down Expand Up @@ -2813,7 +2813,7 @@ expr_normalize_or(struct expr *expr)
expr_destroy(expr);
return new;
}
free(new);
expr_destroy(new);
} else {
expr_insert_andor(expr, next, new);
}
Expand All @@ -2823,12 +2823,12 @@ expr_normalize_or(struct expr *expr)
}
}
if (ovs_list_is_empty(&expr->andor)) {
free(expr);
expr_destroy(expr);
return expr_create_boolean(false);
}
if (ovs_list_is_short(&expr->andor)) {
struct expr *e = expr_from_node(ovs_list_pop_front(&expr->andor));
free(expr);
expr_destroy(expr);
return e;
}

Expand Down

0 comments on commit 4b4cadc

Please sign in to comment.