Skip to contentSkip to conversation

Partially revert "rule, set_elem: remove trailing \n in userdata snprintf"

PATCH In review Phil Sutter 2 participants 5 messages Oct 1, 2024 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/1 Partially revert "rule, set_elem: remove trailing \n in userdata snprintf"

This reverts the rule-facing part of commit
c759027a526ac09ce413dc88c308a4ed98b33416.

It can't be right: Rules without userdata are printed with a trailing newline, so this commit made behaviour inconsistent.

Fixes c759027a526ac ("rule, set_elem: remove trailing \n in userdata snprintf")
---
 src/rule.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/rule.c +1 -1
index 811d5a213f835..51d778d095317 100644
@@ -601,7 +601,7 @@ static int nftnl_rule_snprintf_default(char *buf, size_t remain,
 			SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 		}
 
-		ret = snprintf(buf + offset, remain, " }");
+		ret = snprintf(buf + offset, remain, " }\n");
 		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 
 	}
Signature · 1 line
2.43.0
#1 lore raw
Quoted 5 lines · Phil Sutter

This reverts the rule-facing part of commit
c759027a526ac09ce413dc88c308a4ed98b33416.

It can't be right: Rules without userdata are printed with a trailing newline, so this commit made behaviour inconsistent.

Did you run tests/py with this? It is the primary user for this.

Quoted 22 lines
Fixes c759027a526ac ("rule, set_elem: remove trailing \n in userdata snprintf")
---
 src/rule.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/rule.c +1 -1
index 811d5a213f835..51d778d095317 100644
@@ -601,7 +601,7 @@ static int nftnl_rule_snprintf_default(char *buf, size_t remain,
 			SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 		}
 
-		ret = snprintf(buf + offset, remain, " }");
+		ret = snprintf(buf + offset, remain, " }\n");
 		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 
 	}
Signature · 1 line
2.43.0
author reviewer
#2 lore raw

Hi Pablo,

Quoted 8 lines · Pablo Neira Ayuso
Quoted 5 lines · Phil Sutter

This reverts the rule-facing part of commit
c759027a526ac09ce413dc88c308a4ed98b33416.

It can't be right: Rules without userdata are printed with a trailing newline, so this commit made behaviour inconsistent.

Did you run tests/py with this? It is the primary user for this.

It doesn't cover this because there's no test containing a rule with a comment. I just added a respective test, but only to notice it does not matter because nft-test.py compares rules' payload individually per-rule and thus does not care whether output has a trailing newline or not.

I noticed it when testing the iptables compat ext stuff. You can easily reproduce it like so:

| # nft --check --debug=netlink 'table t { chain c { accept comment mycomment; accept; accept;};}'
| ip (null) (null) use 0
| ip t c
|   [ immediate reg 0 accept ]
|   userdata = { \x00\x0amycomment\x00 }
| ip t c
|   [ immediate reg 0 accept ]
| 
| ip t c
|   [ immediate reg 0 accept ]

Note the missing empty line after the first rule.

Cheers, Phil

#3 lore raw
Quoted 31 lines · Phil Sutter

Hi Pablo,

Quoted 8 lines · Pablo Neira Ayuso
Quoted 5 lines · Phil Sutter

This reverts the rule-facing part of commit
c759027a526ac09ce413dc88c308a4ed98b33416.

It can't be right: Rules without userdata are printed with a trailing newline, so this commit made behaviour inconsistent.

Did you run tests/py with this? It is the primary user for this.

It doesn't cover this because there's no test containing a rule with a comment. I just added a respective test, but only to notice it does not matter because nft-test.py compares rules' payload individually per-rule and thus does not care whether output has a trailing newline or not.

I noticed it when testing the iptables compat ext stuff. You can easily reproduce it like so:

| # nft --check --debug=netlink 'table t { chain c { accept comment mycomment; accept; accept;};}'
| ip (null) (null) use 0
| ip t c
|   [ immediate reg 0 accept ]
|   userdata = { \x00\x0amycomment\x00 }
| ip t c
|   [ immediate reg 0 accept ]
| 
| ip t c
|   [ immediate reg 0 accept ]
Quoted 1 line

Note the missing empty line after the first rule.

None of the existing libnftnl _snprintf functions terminate string with line break, right?. I mean, for consistency with other existing _snprintf functions. Maybe fix nft instead?

author reviewer
#4 lore raw
Quoted 32 lines · Pablo Neira Ayuso
Quoted 31 lines · Phil Sutter

Hi Pablo,

Quoted 8 lines · Pablo Neira Ayuso
Quoted 5 lines · Phil Sutter

This reverts the rule-facing part of commit
c759027a526ac09ce413dc88c308a4ed98b33416.

It can't be right: Rules without userdata are printed with a trailing newline, so this commit made behaviour inconsistent.

Did you run tests/py with this? It is the primary user for this.

It doesn't cover this because there's no test containing a rule with a comment. I just added a respective test, but only to notice it does not matter because nft-test.py compares rules' payload individually per-rule and thus does not care whether output has a trailing newline or not.

I noticed it when testing the iptables compat ext stuff. You can easily reproduce it like so:

| # nft --check --debug=netlink 'table t { chain c { accept comment mycomment; accept; accept;};}'
| ip (null) (null) use 0
| ip t c
|   [ immediate reg 0 accept ]
|   userdata = { \x00\x0amycomment\x00 }
| ip t c
|   [ immediate reg 0 accept ]
| 
| ip t c
|   [ immediate reg 0 accept ]
Quoted 5 lines
Quoted 1 line

Note the missing empty line after the first rule.

None of the existing libnftnl _snprintf functions terminate string with line break, right?. I mean, for consistency with other existing _snprintf functions. Maybe fix nft instead?

Correct, though others don't emit newlines at all. I'll change rule debug output to never append a newline and see what breaks.

Thanks, Phil