Skip to content

Commit 8322628

Browse files
committed
fixing with new libxtables
1 parent 423a86e commit 8322628

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

ipthelper/ip6tables.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ static const unsigned int inverse_for_options[NUMBER_OF_OPT] =
216216
0,
217217
};
218218

219-
#define opts iptables_globals.opts
220219
#define prog_name iptables_globals.program_name
221220
#define prog_vers iptables_globals.program_version
222221
/* A few hardcoded protocols for 'all' and in case the user has no
@@ -1439,10 +1438,27 @@ int do_command6(int argc, char *argv[], char **table, void **handle)
14391438
demand-load a protocol. */
14401439
opterr = 0;
14411440

1442-
opts = xt_params->orig_opts;
1441+
/* Create a malloc'd copy of orig_opts */
1442+
if (iptables_globals.opts == NULL) {
1443+
size_t num_opts = 0;
1444+
struct option *orig_opts = iptables_globals.orig_opts;
1445+
1446+
/* Count the number of options (including the NULL terminator) */
1447+
while (orig_opts[num_opts].name != NULL) {
1448+
num_opts++;
1449+
}
1450+
num_opts++; /* Include the NULL terminator */
1451+
1452+
/* Allocate memory and copy the options */
1453+
iptables_globals.opts = malloc(num_opts * sizeof(struct option));
1454+
if (iptables_globals.opts == NULL) {
1455+
xtables_error(OTHER_PROBLEM, "malloc failed for options array");
1456+
}
1457+
memcpy(iptables_globals.opts, iptables_globals.orig_opts, num_opts * sizeof(struct option));
1458+
}
14431459
while ((cs.c = getopt_long(argc, argv,
14441460
"-:A:C:D:R:I:L::S::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:g:46",
1445-
opts, NULL)) != -1) {
1461+
iptables_globals.opts?: iptables_globals.orig_opts, NULL)) != -1) {
14461462
switch (cs.c) {
14471463
/*
14481464
* Command selection
@@ -2017,5 +2033,11 @@ int do_command6(int argc, char *argv[], char **table, void **handle)
20172033
free(dmasks);
20182034
xtables_free_opts(1);
20192035

2036+
/* Free the malloc'd copy of opts if it was allocated */
2037+
if (iptables_globals.opts != iptables_globals.orig_opts) {
2038+
free(iptables_globals.opts);
2039+
iptables_globals.opts = NULL;
2040+
}
2041+
20202042
return ret;
20212043
}

ipthelper/iptables.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,10 +1017,28 @@ int do_command4(int argc, char *argv[], char **table, void **handle)
10171017
demand-load a protocol. */
10181018
opterr = 0;
10191019

1020-
iptables_globals.opts = iptables_globals.orig_opts;
1020+
/* Create a malloc'd copy of orig_opts */
1021+
if (iptables_globals.opts == NULL) {
1022+
size_t num_opts = 0;
1023+
struct option *orig_opts = iptables_globals.orig_opts;
1024+
1025+
/* Count the number of options (including the NULL terminator) */
1026+
while (orig_opts[num_opts].name != NULL) {
1027+
num_opts++;
1028+
}
1029+
num_opts++; /* Include the NULL terminator */
1030+
1031+
/* Allocate memory and copy the options */
1032+
iptables_globals.opts = malloc(num_opts * sizeof(struct option));
1033+
if (iptables_globals.opts == NULL) {
1034+
xtables_error(OTHER_PROBLEM, "malloc failed for options array");
1035+
}
1036+
memcpy(iptables_globals.opts, iptables_globals.orig_opts, num_opts * sizeof(struct option));
1037+
}
10211038
while ((cs.c = getopt_long(argc, argv,
10221039
"-:A:C:D:R:I:L::S::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:g:46",
1023-
iptables_globals.opts, NULL)) != -1) {
1040+
iptables_globals.opts?: iptables_globals.orig_opts,
1041+
NULL)) != -1) {
10241042
switch (cs.c) {
10251043
/*
10261044
* Command selection
@@ -1523,5 +1541,11 @@ int do_command4(int argc, char *argv[], char **table, void **handle)
15231541
free(dmasks);
15241542
//xtables_free_opts(1);
15251543

1544+
/* Free the malloc'd copy of opts if it was allocated */
1545+
if (iptables_globals.opts != iptables_globals.orig_opts) {
1546+
free(iptables_globals.opts);
1547+
iptables_globals.opts = NULL;
1548+
}
1549+
15261550
return ret;
15271551
}

ipthelper/xshared.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -567,16 +567,14 @@ void command_match(struct iptables_command_state *cs)
567567
if (m->x6_options != NULL){
568568
opts = xtables_options_xfrm(xt_params->orig_opts, opts,
569569
m->x6_options, &m->option_offset);
570-
int num_orig;
571-
for (num_orig = 0; opts[num_orig].name != NULL; ++num_orig) {}
572-
573570
}
574571
else if (m->extra_opts != NULL)
575572
opts = xtables_merge_options(xt_params->orig_opts, opts,
576573
m->extra_opts, &m->option_offset);
574+
else
575+
return;
577576
if (opts == NULL)
578577
xtables_error(OTHER_PROBLEM, "can't alloc memory!");
579-
xt_params->opts = opts;
580578
}
581579

582580
const char *xt_parse_target(const char *targetname)
@@ -632,10 +630,12 @@ void command_jump(struct iptables_command_state *cs)
632630
opts = xtables_options_xfrm(xt_params->orig_opts, opts,
633631
cs->target->x6_options,
634632
&cs->target->option_offset);
635-
else
633+
else if (cs->target->extra_opts != NULL)
636634
opts = xtables_merge_options(xt_params->orig_opts, opts,
637635
cs->target->extra_opts,
638636
&cs->target->option_offset);
637+
else
638+
return;
639639
if (opts == NULL)
640640
xtables_error(OTHER_PROBLEM, "can't alloc memory!");
641641
xt_params->opts = opts;

0 commit comments

Comments
 (0)