Skip to contentSkip to conversation

parser_bison: Get rid of (most) bison compiler warnings

PATCH Superseded Phil Sutter 3 participants 8 messages Jul 20, 2019 – Jul 21, 2019 netfilter

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/2 parser_bison: Get rid of (most) bison compiler warnings

Shut the complaints about POSIX incompatibility by passing -Wno-yacc to bison. An alternative would be to not pass -y, but that caused seemingly unsolveable problems with automake and expected file names.

Fix two warnings about deprecated '%pure-parser' and '%error-verbose' statements by replacing them with what bison suggests.

A third warning sadly left in place: Replacing '%name-prefix' by what is suggested leads to compilation errors.

---
 src/Makefile.am    | 2 +-
 src/parser_bison.y | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
src/Makefile.am +1 -1
index e2b531390cefb..740c21f2cac85 100644
@@ -22,7 +22,7 @@ AM_CFLAGS = -Wall								\
 	    -Waggregate-return -Wunused -Wwrite-strings ${GCC_FVISIBILITY_HIDDEN}
 
 
-AM_YFLAGS = -d
+AM_YFLAGS = -d -Wno-yacc
 
 BUILT_SOURCES = parser_bison.h
 
src/parser_bison.y +2 -2
index c90de47e88f74..12e499b4dd025 100644
@@ -116,12 +116,12 @@ int nft_lex(void *, void *, void *);
 
 %name-prefix "nft_"
 %debug
-%pure-parser
+%define api.pure
 %parse-param		{ struct nft_ctx *nft }
 %parse-param		{ void *scanner }
 %parse-param		{ struct parser_state *state }
 %lex-param		{ scanner }
-%error-verbose
+%define parse.error verbose
 %locations
 
 %initial-action {
Signature · 1 line
2.22.0
author reviewer
#1 lore raw

PATCH 2/2 nfnl_osf: Silence string truncation gcc warnings

Albeit a bit too enthusiastic, gcc is right in that these strings may be truncated since the destination buffer is smaller than the source one. Get rid of the warnings (and the potential problem) by specifying a string "precision" of one character less than the destination. This ensures a terminating nul-character may be written as well.

Fixes af00174af3ef4 ("src: osf: import nfnl_osf.c to load osf fingerprints")
---
 src/nfnl_osf.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
src/nfnl_osf.c +9 -7
index be3fd8100b665..bed9ba64b65c6 100644
@@ -289,32 +289,34 @@ static int osf_load_line(char *buffer, int len, int del,
 	pend = nf_osf_strchr(pbeg, OSFPDEL);
 	if (pend) {
 		*pend = '\0';
-		cnt = snprintf(obuf, sizeof(obuf), "%s,", pbeg);
+		i = sizeof(obuf);
+		cnt = snprintf(obuf, i, "%.*s,", i - 2, pbeg);
 		pbeg = pend + 1;
 	}
 
 	pend = nf_osf_strchr(pbeg, OSFPDEL);
 	if (pend) {
 		*pend = '\0';
+		i = sizeof(f.genre);
 		if (pbeg[0] == '@' || pbeg[0] == '*')
-			cnt = snprintf(f.genre, sizeof(f.genre), "%s", pbeg + 1);
-		else
-			cnt = snprintf(f.genre, sizeof(f.genre), "%s", pbeg);
+			pbeg++;
+		cnt = snprintf(f.genre, i, "%.*s", i - 1, pbeg + 1);
 		pbeg = pend + 1;
 	}
 
 	pend = nf_osf_strchr(pbeg, OSFPDEL);
 	if (pend) {
 		*pend = '\0';
-		cnt = snprintf(f.version, sizeof(f.version), "%s", pbeg);
+		i = sizeof(f.version);
+		cnt = snprintf(f.version, i, "%.*s", i - 1, pbeg);
 		pbeg = pend + 1;
 	}
 
 	pend = nf_osf_strchr(pbeg, OSFPDEL);
 	if (pend) {
 		*pend = '\0';
-		cnt =
-		    snprintf(f.subtype, sizeof(f.subtype), "%s", pbeg);
+		i = sizeof(f.subtype);
+		cnt = snprintf(f.subtype, i, "%.*s", i - 1, pbeg);
 		pbeg = pend + 1;
 	}
 
Signature · 1 line
2.22.0
maintainer
#2 lore raw

Re: [nft PATCH 2/2] nfnl_osf: Silence string truncation gcc warnings

Quoted 5 lines · Phil Sutter

Albeit a bit too enthusiastic, gcc is right in that these strings may be truncated since the destination buffer is smaller than the source one. Get rid of the warnings (and the potential problem) by specifying a string "precision" of one character less than the destination. This ensures a terminating nul-character may be written as well.

Fernando sent a patch for this already, with the notable difference of altering the size of the destination buffer by one.

author reviewer
#3 lore raw

Re: [nft PATCH 2/2] nfnl_osf: Silence string truncation gcc warnings

Quoted 9 lines · Florian Westphal
Quoted 5 lines · Phil Sutter

Albeit a bit too enthusiastic, gcc is right in that these strings may be truncated since the destination buffer is smaller than the source one. Get rid of the warnings (and the potential problem) by specifying a string "precision" of one character less than the destination. This ensures a terminating nul-character may be written as well.

Fernando sent a patch for this already, with the notable difference of altering the size of the destination buffer by one.

Ah, thanks! I missed it. Replied to it pointing at my patch, also spotted a typo in his patch. :)

Cheers, Phil

#4 lore raw

Re: [nft PATCH 2/2] nfnl_osf: Silence string truncation gcc warnings

In reply to Phil Sutter

Hi Phil,

I am porting your changes to my patch and I have few comments, please see below.

Quoted 28 lines · Phil Sutter

Albeit a bit too enthusiastic, gcc is right in that these strings may be truncated since the destination buffer is smaller than the source one. Get rid of the warnings (and the potential problem) by specifying a string "precision" of one character less than the destination. This ensures a terminating nul-character may be written as well.

Fixes af00174af3ef4 ("src: osf: import nfnl_osf.c to load osf fingerprints")
---
 src/nfnl_osf.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
src/nfnl_osf.c +2 -1
index be3fd8100b665..bed9ba64b65c6 100644
@@ -289,32 +289,34 @@ static int osf_load_line(char *buffer, int len, int del,
 	pend = nf_osf_strchr(pbeg, OSFPDEL);
 	if (pend) {
 		*pend = '\0';
-		cnt = snprintf(obuf, sizeof(obuf), "%s,", pbeg);
+		i = sizeof(obuf);
+		cnt = snprintf(obuf, i, "%.*s,", i - 2, pbeg);
 		pbeg = pend + 1;
 	}
 
 	pend = nf_osf_strchr(pbeg, OSFPDEL);
 	if (pend) {

src/nfnl_osf.c static int osf_load_line(char *buffer, int len, int del, · line 305

Quoted 10 lines
src/nfnl_osf.c +3 -3
 		*pend = '\0';
+		i = sizeof(f.genre);
 		if (pbeg[0] == '@' || pbeg[0] == '*')
-			cnt = snprintf(f.genre, sizeof(f.genre), "%s", pbeg + 1);
-		else
-			cnt = snprintf(f.genre, sizeof(f.genre), "%s", pbeg);
+			pbeg++;
+		cnt = snprintf(f.genre, i, "%.*s", i - 1, pbeg + 1);
 		pbeg = pend + 1;
 	}

I am not including this because the pbeg pointer is being modified if the condition is true which is not what we want. Note that pbeg is being used below. Also, we cannot do pbeg++ and at the same time shift the pointer passed to snprintf with pbeg + 1.

I propose to let the if statement as it is and modify only the snprintf().

What do you think? Am I missing something here? Thanks Phil!

Quoted 21 lines
src/nfnl_osf.c +4 -3
 	pend = nf_osf_strchr(pbeg, OSFPDEL);
 	if (pend) {
 		*pend = '\0';
-		cnt = snprintf(f.version, sizeof(f.version), "%s", pbeg);
+		i = sizeof(f.version);
+		cnt = snprintf(f.version, i, "%.*s", i - 1, pbeg);
 		pbeg = pend + 1;
 	}
 
 	pend = nf_osf_strchr(pbeg, OSFPDEL);
 	if (pend) {
 		*pend = '\0';
-		cnt =
-		    snprintf(f.subtype, sizeof(f.subtype), "%s", pbeg);
+		i = sizeof(f.subtype);
+		cnt = snprintf(f.subtype, i, "%.*s", i - 1, pbeg);
 		pbeg = pend + 1;
 	}
 
 
author reviewer
#5 lore raw

Re: [nft PATCH 2/2] nfnl_osf: Silence string truncation gcc warnings

Hi,

On Sun, Jul 21, 2019 at 01:15:58PM +0200, Fernando Fernandez Mancera wrote:
[...]

Quoted 9 lines
Quoted 9 lines
src/nfnl_osf.c +2 -3
 	pend = nf_osf_strchr(pbeg, OSFPDEL);
 	if (pend) {
 		*pend = '\0';
+		i = sizeof(f.genre);
 		if (pbeg[0] == '@' || pbeg[0] == '*')
-			cnt = snprintf(f.genre, sizeof(f.genre), "%s", pbeg + 1);
-		else
-			cnt = snprintf(f.genre, sizeof(f.genre), "%s", pbeg);
+			pbeg++;
Quoted 8 lines

src/nfnl_osf.c static int osf_load_line(char *buffer, int len, int del, · line 305

Quoted 3 lines
src/nfnl_osf.c +1 -0
+		cnt = snprintf(f.genre, i, "%.*s", i - 1, pbeg + 1);
 		pbeg = pend + 1;
 	}

I am not including this because the pbeg pointer is being modified if the condition is true which is not what we want. Note that pbeg is being used below. Also, we cannot do pbeg++ and at the same time shift the pointer passed to snprintf with pbeg + 1.

Oh, sorry that 'pbeg + 1' in my added code is a bug. I guess incrementing pbeg if it starts with @ or * is fine because after the call to snprintf() it is reset ('pbeg = pend + 1') without reusing its old value.

Cheers, Phil

maintainer
#6 lore raw
In reply to Phil Sutter
Quoted 9 lines · Phil Sutter

Shut the complaints about POSIX incompatibility by passing -Wno-yacc to bison. An alternative would be to not pass -y, but that caused seemingly unsolveable problems with automake and expected file names.

Fix two warnings about deprecated '%pure-parser' and '%error-verbose' statements by replacing them with what bison suggests.

A third warning sadly left in place: Replacing '%name-prefix' by what is suggested leads to compilation errors.

Can you add those warnings to the changelog before pushing?

I don't see them, even without this patch.

author reviewer
#7 lore raw
Quoted 5 lines · Florian Westphal
Quoted 4 lines · Phil Sutter

Shut the complaints about POSIX incompatibility by passing -Wno-yacc to bison. An alternative would be to not pass -y, but that caused seemingly unsolveable problems with automake and expected file names.

Quoted 7 lines
Quoted 5 lines

Fix two warnings about deprecated '%pure-parser' and '%error-verbose' statements by replacing them with what bison suggests.

A third warning sadly left in place: Replacing '%name-prefix' by what is suggested leads to compilation errors.

Can you add those warnings to the changelog before pushing?

Sure!

Quoted 1 line

I don't see them, even without this patch.

I started seeing them just recently, maybe after the last bison update (to 3.4.1).

Cheers, Phil