netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [libnftnl PATCH] rule: Don't append a newline when printing a rule
@ 2024-10-01 17:45 Phil Sutter
  2024-10-01 18:46 ` Pablo Neira Ayuso
  2024-10-01 19:49 ` Phil Sutter
  0 siblings, 2 replies; 3+ messages in thread
From: Phil Sutter @ 2024-10-01 17:45 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: netfilter-devel

Since commit c759027a526ac, printed rules may or may not end with a
newline depending on whether userdata was present or not. Deal with this
inconsistency by avoiding the trailing newline in all cases.

Fixes: c759027a526ac ("rule, set_elem: remove trailing \n in userdata snprintf")
Signed-off-by: Phil Sutter <phil@nwl.cc>
---
This supersedes the previous patch with subject: Partially revert "rule,
set_elem: remove trailing \n in userdata snprintf" by solving the
problem in the opposite direction. As correctly assessed by Pablo, this
way is consistent with other printers.

I tested the change with nftables and iptables testsuites. It breaks a
single test in the latter which compares full ruleset debug output
against a record. To fix that, one could just pass --ignore-blank-lines
parameter to the diff call.
---
 src/rule.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/src/rule.c b/src/rule.c
index 811d5a213f835..c22918a8f3527 100644
--- a/src/rule.c
+++ b/src/rule.c
@@ -573,23 +573,21 @@ static int nftnl_rule_snprintf_default(char *buf, size_t remain,
 		sep = " ";
 	}
 
-	ret = snprintf(buf + offset, remain, "\n");
-	SNPRINTF_BUFFER_SIZE(ret, remain, offset);
-
 	list_for_each_entry(expr, &r->expr_list, head) {
-		ret = snprintf(buf + offset, remain, "  [ %s ", expr->ops->name);
+		ret = snprintf(buf + offset, remain,
+			       "\n  [ %s ", expr->ops->name);
 		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 
 		ret = nftnl_expr_snprintf(buf + offset, remain, expr,
 					     type, flags);
 		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 
-		ret = snprintf(buf + offset, remain, "]\n");
+		ret = snprintf(buf + offset, remain, "]");
 		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 	}
 
 	if (r->user.len) {
-		ret = snprintf(buf + offset, remain, "  userdata = { ");
+		ret = snprintf(buf + offset, remain, "\n  userdata = { ");
 		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 
 		for (i = 0; i < r->user.len; i++) {
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [libnftnl PATCH] rule: Don't append a newline when printing a rule
  2024-10-01 17:45 [libnftnl PATCH] rule: Don't append a newline when printing a rule Phil Sutter
@ 2024-10-01 18:46 ` Pablo Neira Ayuso
  2024-10-01 19:49 ` Phil Sutter
  1 sibling, 0 replies; 3+ messages in thread
From: Pablo Neira Ayuso @ 2024-10-01 18:46 UTC (permalink / raw)
  To: Phil Sutter; +Cc: netfilter-devel

On Tue, Oct 01, 2024 at 07:45:22PM +0200, Phil Sutter wrote:
> Since commit c759027a526ac, printed rules may or may not end with a
> newline depending on whether userdata was present or not. Deal with this
> inconsistency by avoiding the trailing newline in all cases.

LGTM, thanks Phil.

> Fixes: c759027a526ac ("rule, set_elem: remove trailing \n in userdata snprintf")
> Signed-off-by: Phil Sutter <phil@nwl.cc>
> ---
> This supersedes the previous patch with subject: Partially revert "rule,
> set_elem: remove trailing \n in userdata snprintf" by solving the
> problem in the opposite direction. As correctly assessed by Pablo, this
> way is consistent with other printers.
> 
> I tested the change with nftables and iptables testsuites. It breaks a
> single test in the latter which compares full ruleset debug output
> against a record. To fix that, one could just pass --ignore-blank-lines
> parameter to the diff call.
> ---
>  src/rule.c | 10 ++++------
>  1 file changed, 4 insertions(+), 6 deletions(-)
> 
> diff --git a/src/rule.c b/src/rule.c
> index 811d5a213f835..c22918a8f3527 100644
> --- a/src/rule.c
> +++ b/src/rule.c
> @@ -573,23 +573,21 @@ static int nftnl_rule_snprintf_default(char *buf, size_t remain,
>  		sep = " ";
>  	}
>  
> -	ret = snprintf(buf + offset, remain, "\n");
> -	SNPRINTF_BUFFER_SIZE(ret, remain, offset);
> -
>  	list_for_each_entry(expr, &r->expr_list, head) {
> -		ret = snprintf(buf + offset, remain, "  [ %s ", expr->ops->name);
> +		ret = snprintf(buf + offset, remain,
> +			       "\n  [ %s ", expr->ops->name);
>  		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
>  
>  		ret = nftnl_expr_snprintf(buf + offset, remain, expr,
>  					     type, flags);
>  		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
>  
> -		ret = snprintf(buf + offset, remain, "]\n");
> +		ret = snprintf(buf + offset, remain, "]");
>  		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
>  	}
>  
>  	if (r->user.len) {
> -		ret = snprintf(buf + offset, remain, "  userdata = { ");
> +		ret = snprintf(buf + offset, remain, "\n  userdata = { ");
>  		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
>  
>  		for (i = 0; i < r->user.len; i++) {
> -- 
> 2.43.0
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [libnftnl PATCH] rule: Don't append a newline when printing a rule
  2024-10-01 17:45 [libnftnl PATCH] rule: Don't append a newline when printing a rule Phil Sutter
  2024-10-01 18:46 ` Pablo Neira Ayuso
@ 2024-10-01 19:49 ` Phil Sutter
  1 sibling, 0 replies; 3+ messages in thread
From: Phil Sutter @ 2024-10-01 19:49 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: netfilter-devel

On Tue, Oct 01, 2024 at 07:45:22PM +0200, Phil Sutter wrote:
> Since commit c759027a526ac, printed rules may or may not end with a
> newline depending on whether userdata was present or not. Deal with this
> inconsistency by avoiding the trailing newline in all cases.
> 
> Fixes: c759027a526ac ("rule, set_elem: remove trailing \n in userdata snprintf")
> Signed-off-by: Phil Sutter <phil@nwl.cc>

Patch applied.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-10-01 19:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-01 17:45 [libnftnl PATCH] rule: Don't append a newline when printing a rule Phil Sutter
2024-10-01 18:46 ` Pablo Neira Ayuso
2024-10-01 19:49 ` Phil Sutter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).