Skip to contentSkip to conversation

tools: Fix str_error_r() Werror=restrict build

PATCH In review Sergey Senozhatsky 4 participants 5 messages Mar 19, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: Fix str_error_r() Werror=restrict build

Commit c8b5f2c96d1bf6c ("tools: Introduce str_error_r()") added an str_error_r() wrapper which makes gcc8 unhappy due to restrict-qualified parameter aliasing violation:

../lib/str_error_r.c: In function ‘str_error_r’:
../lib/str_error_r.c:25:3: error: passing argument 1 to restrict-qualified parameter aliases with argument 5 [-Werror=restrict]
snprintf(buf, buflen, "INTERNAL ERROR: strerror_r(%d, %p, %zd)=%d", errnum, buf, buflen, err);
^~~~~~~~
cc1: all warnings being treated as errors

Workaround that aliasing error by creating an additional stack variable which holds the buf pointer value we passed to strerror_r().

---
 tools/lib/str_error_r.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/lib/str_error_r.c +7 -2
index d6d65537b0d9..11c3425f272b 100644
@@ -21,7 +21,12 @@
 char *str_error_r(int errnum, char *buf, size_t buflen)
 {
 	int err = strerror_r(errnum, buf, buflen);
-	if (err)
-		snprintf(buf, buflen, "INTERNAL ERROR: strerror_r(%d, %p, %zd)=%d", errnum, buf, buflen, err);
+	if (err) {
+		char *err_buf = buf;
+
+		snprintf(err_buf, buflen,
+			 "INTERNAL ERROR: strerror_r(%d, %p, %zd)=%d",
+			 errnum, buf, buflen, err);
+	}
 	return buf;
 }
Signature · 1 line
2.16.2
#1 lore raw
Quoted 10 lines · Sergey Senozhatsky

Commit c8b5f2c96d1bf6c ("tools: Introduce str_error_r()") added an str_error_r() wrapper which makes gcc8 unhappy due to restrict-qualified parameter aliasing violation:

../lib/str_error_r.c: In function ‘str_error_r’:
../lib/str_error_r.c:25:3: error: passing argument 1 to restrict-qualified parameter aliases with argument 5 [-Werror=restrict]
snprintf(buf, buflen, "INTERNAL ERROR: strerror_r(%d, %p, %zd)=%d", errnum, buf, buflen, err);
^~~~~~~~
cc1: all warnings being treated as errors

Quoted 4 lines

Workaround that aliasing error by creating an additional stack variable which holds the buf pointer value we passed to strerror_r().

Josh posted another way to fix it:
https://marc.info/?l=linux-kernel&m=152116992412107&w=2

your patch keeps the same output, I dont mind either way ;-)

jirka

Quoted 26 lines
---
 tools/lib/str_error_r.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/lib/str_error_r.c +7 -2
index d6d65537b0d9..11c3425f272b 100644
@@ -21,7 +21,12 @@
 char *str_error_r(int errnum, char *buf, size_t buflen)
 {
 	int err = strerror_r(errnum, buf, buflen);
-	if (err)
-		snprintf(buf, buflen, "INTERNAL ERROR: strerror_r(%d, %p, %zd)=%d", errnum, buf, buflen, err);
+	if (err) {
+		char *err_buf = buf;
+
+		snprintf(err_buf, buflen,
+			 "INTERNAL ERROR: strerror_r(%d, %p, %zd)=%d",
+			 errnum, buf, buflen, err);
+	}
 	return buf;
 }
Signature · 1 line
2.16.2
#2 lore raw
Quoted 11 lines · Jiri Olsa
Quoted 10 lines · Sergey Senozhatsky

Commit c8b5f2c96d1bf6c ("tools: Introduce str_error_r()") added an str_error_r() wrapper which makes gcc8 unhappy due to restrict-qualified parameter aliasing violation:

../lib/str_error_r.c: In function ‘str_error_r’:
../lib/str_error_r.c:25:3: error: passing argument 1 to restrict-qualified parameter aliases with argument 5 [-Werror=restrict]
snprintf(buf, buflen, "INTERNAL ERROR: strerror_r(%d, %p, %zd)=%d", errnum, buf, buflen, err);
^~~~~~~~
cc1: all warnings being treated as errors

Quoted 7 lines
Quoted 4 lines

Workaround that aliasing error by creating an additional stack variable which holds the buf pointer value we passed to strerror_r().

Josh posted another way to fix it:
https://marc.info/?l=linux-kernel&m=152116992412107&w=2

Oh, thanks for the pointers.

Quoted 1 line

your patch keeps the same output, I dont mind either way ;-)

Thanks. No strong opinion, either way is OK :) Can't tell for sure if snprintf(... "%p", buf) is significantly better than snprintf(... "[buf]"), but Arnaldo wanted to have the first version.

	-ss
#3 lore raw
In reply to Sergey Senozhatsky

Em Mon, Mar 19, 2018 at 02:55:04PM +0900, Sergey Senozhatsky escreveu:

Quoted 9 lines

Commit c8b5f2c96d1bf6c ("tools: Introduce str_error_r()") added an str_error_r() wrapper which makes gcc8 unhappy due to restrict-qualified parameter aliasing violation:

../lib/str_error_r.c: In function ‘str_error_r’:
../lib/str_error_r.c:25:3: error: passing argument 1 to restrict-qualified parameter aliases with argument 5 [-Werror=restrict]
snprintf(buf, buflen, "INTERNAL ERROR: strerror_r(%d, %p, %zd)=%d", errnum, buf, buflen, err);
^~~~~~~~
cc1: all warnings being treated as errors

I applied Josh's patch, just printing '[buf]', that is good enough, I think.

- Arnaldo

Quoted 29 lines

Workaround that aliasing error by creating an additional stack variable which holds the buf pointer value we passed to strerror_r().

---
 tools/lib/str_error_r.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/lib/str_error_r.c +7 -2
index d6d65537b0d9..11c3425f272b 100644
@@ -21,7 +21,12 @@
 char *str_error_r(int errnum, char *buf, size_t buflen)
 {
 	int err = strerror_r(errnum, buf, buflen);
-	if (err)
-		snprintf(buf, buflen, "INTERNAL ERROR: strerror_r(%d, %p, %zd)=%d", errnum, buf, buflen, err);
+	if (err) {
+		char *err_buf = buf;
+
+		snprintf(err_buf, buflen,
+			 "INTERNAL ERROR: strerror_r(%d, %p, %zd)=%d",
+			 errnum, buf, buflen, err);
+	}
 	return buf;
 }
Signature · 1 line
2.16.2
#4 lore raw
Quoted 5 lines · Arnaldo Carvalho de Melo

Em Mon, Mar 19, 2018 at 02:55:04PM +0900, Sergey Senozhatsky escreveu:

Quoted 4 lines

Commit c8b5f2c96d1bf6c ("tools: Introduce str_error_r()") added an str_error_r() wrapper which makes gcc8 unhappy due to restrict-qualified parameter aliasing violation:

Quoted 8 lines
Quoted 5 lines

../lib/str_error_r.c: In function ‘str_error_r’:
../lib/str_error_r.c:25:3: error: passing argument 1 to restrict-qualified parameter aliases with argument 5 [-Werror=restrict]
snprintf(buf, buflen, "INTERNAL ERROR: strerror_r(%d, %p, %zd)=%d", errnum, buf, buflen, err);
^~~~~~~~
cc1: all warnings being treated as errors

I applied Josh's patch, just printing '[buf]', that is good enough, I think.

Sure. Do you need the whole "strerror_r(%d, %p, %zd)" in this case? Seems that printing out just `errnum' should be good enough.

	-ss