Skip to content

Commit 0a8713d

Browse files
committed
Update xshared.c
1 parent 34e6659 commit 0a8713d

1 file changed

Lines changed: 33 additions & 6 deletions

File tree

ipthelper/xshared.c

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,33 @@
1818
#include <math.h>
1919
#include "xshared.h"
2020

21+
static void ensure_heap_opts(struct xtables_globals *globals)
22+
{
23+
struct option *orig;
24+
size_t count;
25+
struct option *copy;
26+
27+
if (globals == NULL)
28+
return;
29+
30+
if (globals->opts == NULL)
31+
globals->opts = globals->orig_opts;
32+
33+
orig = globals->orig_opts;
34+
if (orig == NULL || globals->opts != orig)
35+
return;
36+
37+
for (count = 0; orig[count].name != NULL; ++count)
38+
;
39+
copy = malloc(sizeof(*copy) * (count + 1));
40+
if (copy == NULL)
41+
xtables_error(RESOURCE_PROBLEM, "malloc");
42+
memcpy(copy, orig, sizeof(*copy) * (count + 1));
43+
globals->opts = copy;
44+
if (xt_params == globals)
45+
xt_params->opts = copy;
46+
}
47+
2148
/*
2249
* Print out any special helps. A user might like to be able to add a --help
2350
* to the commandline, and see expected results. So we call help for all
@@ -157,6 +184,7 @@ int command_default(struct iptables_command_state *cs,
157184
m->m->u.user.revision = m->revision;
158185
xs_init_match(m);
159186

187+
ensure_heap_opts(gl);
160188
if (m->x6_options != NULL)
161189
gl->opts = xtables_options_xfrm(gl->orig_opts,
162190
gl->opts,
@@ -539,6 +567,7 @@ void print_ifaces(const char *iniface, const char *outiface, uint8_t invflags,
539567

540568
void command_match(struct iptables_command_state *cs)
541569
{
570+
ensure_heap_opts(xt_params);
542571
struct option *opts = xt_params->opts;
543572
struct xtables_match *m;
544573
size_t size;
@@ -564,16 +593,13 @@ void command_match(struct iptables_command_state *cs)
564593
if (m == m->next)
565594
return;
566595
/* Merge options for non-cloned matches */
567-
if (m->x6_options != NULL){
596+
if (m->x6_options != NULL) {
568597
opts = xtables_options_xfrm(xt_params->orig_opts, opts,
569598
m->x6_options, &m->option_offset);
570-
int num_orig;
571-
for (num_orig = 0; opts[num_orig].name != NULL; ++num_orig) {}
572-
573-
}
574-
else if (m->extra_opts != NULL)
599+
} else if (m->extra_opts != NULL) {
575600
opts = xtables_merge_options(xt_params->orig_opts, opts,
576601
m->extra_opts, &m->option_offset);
602+
}
577603
if (opts == NULL)
578604
xtables_error(OTHER_PROBLEM, "can't alloc memory!");
579605
xt_params->opts = opts;
@@ -601,6 +627,7 @@ const char *xt_parse_target(const char *targetname)
601627

602628
void command_jump(struct iptables_command_state *cs)
603629
{
630+
ensure_heap_opts(xt_params);
604631
struct option *opts = xt_params->opts;
605632
size_t size;
606633

0 commit comments

Comments
 (0)