Skip to content

Commit d207cf4

Browse files
Daniel Borkmanndavem330
authored andcommitted
bpf_exp: free duplicated labels at exit time
Valgrind found that extracted labels that are passed from the lexer weren't freed upon exit. Therefore, add a small helper function that walks label tables and frees them. Since also NULL can be passed to free(3), we do not need to take care of that here. While at it, fix up a spacing error in bpf_set_curr_label(). Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 8b138da commit d207cf4

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

tools/net/bpf_exp.y

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ extern void yyerror(const char *str);
4040

4141
extern void bpf_asm_compile(FILE *fp, bool cstyle);
4242
static void bpf_set_curr_instr(uint16_t op, uint8_t jt, uint8_t jf, uint32_t k);
43-
static void bpf_set_curr_label(const char *label);
44-
static void bpf_set_jmp_label(const char *label, enum jmp_type type);
43+
static void bpf_set_curr_label(char *label);
44+
static void bpf_set_jmp_label(char *label, enum jmp_type type);
4545

4646
%}
4747

@@ -573,7 +573,7 @@ txa
573573

574574
static int curr_instr = 0;
575575
static struct sock_filter out[BPF_MAXINSNS];
576-
static const char **labels, **labels_jt, **labels_jf, **labels_k;
576+
static char **labels, **labels_jt, **labels_jf, **labels_k;
577577

578578
static void bpf_assert_max(void)
579579
{
@@ -594,13 +594,13 @@ static void bpf_set_curr_instr(uint16_t code, uint8_t jt, uint8_t jf,
594594
curr_instr++;
595595
}
596596

597-
static void bpf_set_curr_label(const char *label)
597+
static void bpf_set_curr_label(char *label)
598598
{
599599
bpf_assert_max();
600-
labels[curr_instr] = label;
600+
labels[curr_instr] = label;
601601
}
602602

603-
static void bpf_set_jmp_label(const char *label, enum jmp_type type)
603+
static void bpf_set_jmp_label(char *label, enum jmp_type type)
604604
{
605605
bpf_assert_max();
606606
switch (type) {
@@ -717,12 +717,25 @@ static void bpf_init(void)
717717
assert(labels_k);
718718
}
719719

720+
static void bpf_destroy_labels(void)
721+
{
722+
int i;
723+
724+
for (i = 0; i < curr_instr; i++) {
725+
free(labels_jf[i]);
726+
free(labels_jt[i]);
727+
free(labels_k[i]);
728+
free(labels[i]);
729+
}
730+
}
731+
720732
static void bpf_destroy(void)
721733
{
722-
free(labels);
734+
bpf_destroy_labels();
723735
free(labels_jt);
724736
free(labels_jf);
725737
free(labels_k);
738+
free(labels);
726739
}
727740

728741
void bpf_asm_compile(FILE *fp, bool cstyle)

0 commit comments

Comments
 (0)