diff mbox series

[iptables,3/3] libxtables: Register multiple extensions in ascending order

Message ID 20200922225341.8976-4-phil@nwl.cc
State Accepted
Delegated to: Pablo Neira
Headers show
Series libxtables: Fix for pointless socket() calls | expand

Commit Message

Phil Sutter Sept. 22, 2020, 10:53 p.m. UTC
The newly introduced ordered insert algorithm in
xtables_register_{match,target}() works best if extensions of same name
are passed in ascending revisions. Since this is the case in about all
extensions' arrays, iterate over them from beginning to end.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 libxtables/xtables.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

Comments

Pablo Neira Ayuso Oct. 5, 2020, 11:41 p.m. UTC | #1
On Wed, Sep 23, 2020 at 12:53:41AM +0200, Phil Sutter wrote:
> The newly introduced ordered insert algorithm in
> xtables_register_{match,target}() works best if extensions of same name
> are passed in ascending revisions. Since this is the case in about all
> extensions' arrays, iterate over them from beginning to end.

This patch should come first in the series, my understanding is that
1/3 assumes that extensions are registered from lower to higher
revision number.

> Signed-off-by: Phil Sutter <phil@nwl.cc>
> ---
>  libxtables/xtables.c | 14 ++++++++------
>  1 file changed, 8 insertions(+), 6 deletions(-)
> 
> diff --git a/libxtables/xtables.c b/libxtables/xtables.c
> index de74d361a53af..90b1195c45a58 100644
> --- a/libxtables/xtables.c
> +++ b/libxtables/xtables.c
> @@ -1139,9 +1139,10 @@ static bool xtables_fully_register_pending_match(struct xtables_match *me,
>  
>  void xtables_register_matches(struct xtables_match *match, unsigned int n)
>  {
> -	do {
> -		xtables_register_match(&match[--n]);
> -	} while (n > 0);
> +	int i;
> +
> +	for (i = 0; i < n; i++)
> +		xtables_register_match(&match[i]);
>  }
>  
>  void xtables_register_target(struct xtables_target *me)
> @@ -1264,9 +1265,10 @@ static bool xtables_fully_register_pending_target(struct xtables_target *me,
>  
>  void xtables_register_targets(struct xtables_target *target, unsigned int n)
>  {
> -	do {
> -		xtables_register_target(&target[--n]);
> -	} while (n > 0);
> +	int i;
> +
> +	for (i = 0; i < n; i++)
> +		xtables_register_target(&target[i]);
>  }
>  
>  /* receives a list of xtables_rule_match, release them */
> -- 
> 2.28.0
>
Phil Sutter Oct. 6, 2020, 9:29 a.m. UTC | #2
On Tue, Oct 06, 2020 at 01:41:21AM +0200, Pablo Neira Ayuso wrote:
> On Wed, Sep 23, 2020 at 12:53:41AM +0200, Phil Sutter wrote:
> > The newly introduced ordered insert algorithm in
> > xtables_register_{match,target}() works best if extensions of same name
> > are passed in ascending revisions. Since this is the case in about all
> > extensions' arrays, iterate over them from beginning to end.
> 
> This patch should come first in the series, my understanding is that
> 1/3 assumes that extensions are registered from lower to higher
> revision number.

No, the algorithm is supposed to work with arbitrary input. This is
merely an optimization given how extension arrays are typically ordered.

Cheers, Phil
diff mbox series

Patch

diff --git a/libxtables/xtables.c b/libxtables/xtables.c
index de74d361a53af..90b1195c45a58 100644
--- a/libxtables/xtables.c
+++ b/libxtables/xtables.c
@@ -1139,9 +1139,10 @@  static bool xtables_fully_register_pending_match(struct xtables_match *me,
 
 void xtables_register_matches(struct xtables_match *match, unsigned int n)
 {
-	do {
-		xtables_register_match(&match[--n]);
-	} while (n > 0);
+	int i;
+
+	for (i = 0; i < n; i++)
+		xtables_register_match(&match[i]);
 }
 
 void xtables_register_target(struct xtables_target *me)
@@ -1264,9 +1265,10 @@  static bool xtables_fully_register_pending_target(struct xtables_target *me,
 
 void xtables_register_targets(struct xtables_target *target, unsigned int n)
 {
-	do {
-		xtables_register_target(&target[--n]);
-	} while (n > 0);
+	int i;
+
+	for (i = 0; i < n; i++)
+		xtables_register_target(&target[i]);
 }
 
 /* receives a list of xtables_rule_match, release them */