Skip to contentSkip to conversation

Compile code with "-Wwrite-strings"

PATCH In review Thomas Huth 3 participants 4 messages Jun 28, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Compile code with "-Wwrite-strings"

So we make sure that we do not accidentially write to constant strings. Also add some missing "const" qualifiers in the code to avoid that we get compiler warnings now.

---
This patch supersedes my previous patch "Declare the prefix string variable in va_report() as const"

 Makefile     | 2 +-
 lib/report.c | 6 +++---
 x86/msr.c    | 4 ++--
 x86/pmu.c    | 2 +-
 4 files changed, 7 insertions(+), 7 deletions(-)
Makefile +1 -1
index 933b9f0..e79cf93 100644
@@ -51,7 +51,7 @@ cc-option = $(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null \
               > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
 
 CFLAGS += -g
-CFLAGS += $(autodepend-flags) -Wall -Werror
+CFLAGS += $(autodepend-flags) -Wall -Wwrite-strings -Werror
 frame-pointer-flag=-f$(if $(KEEP_FRAME_POINTER),no-,)omit-frame-pointer
 fomit_frame_pointer := $(call cc-option, $(frame-pointer-flag), "")
 fnostack_protector := $(call cc-option, -fno-stack-protector, "")
lib/report.c +3 -3
index b002d21..5da27ab 100644
@@ -81,9 +81,9 @@ void report_prefix_pop(void)
 static void va_report(const char *msg_fmt,
 		bool pass, bool xfail, bool skip, va_list va)
 {
-	char *prefix = skip ? "SKIP"
-	                    : xfail ? (pass ? "XPASS" : "XFAIL")
-	                            : (pass ? "PASS"  : "FAIL");
+	const char *prefix = skip ? "SKIP"
+				  : xfail ? (pass ? "XPASS" : "XFAIL")
+					  : (pass ? "PASS"  : "FAIL");
 
 	spin_lock(&lock);
 
x86/msr.c +2 -2
index 91351a3..ffc24b1 100644
@@ -6,7 +6,7 @@
 
 struct msr_info {
     int index;
-    char *name;
+    const char *name;
     struct tc {
         int valid;
         unsigned long long value;
@@ -78,7 +78,7 @@ static void test_msr_rw(int msr_index, unsigned long long input, unsigned long l
 {
     unsigned long long r = 0;
     int index;
-    char *sptr;
+    const char *sptr;
     if ((index = find_msr_info(msr_index)) != -1) {
         sptr = msr_info[index].name;
     } else {
x86/pmu.c +1 -1
index c689800..a0238dc 100644
@@ -73,7 +73,7 @@ union cpuid10_edx {
 } edx;
 
 struct pmu_event {
-	char *name;
+	const char *name;
 	uint32_t unit_sel;
 	int min;
 	int max;
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 1 line · Thomas Huth

So we make sure that we do not accidentially write to constant

"accidentally" (I wouldn't know if Thunderbird wouldn't tell me ;) )

Quoted 19 lines

strings. Also add some missing "const" qualifiers in the code to avoid that we get compiler warnings now.

---
This patch supersedes my previous patch "Declare the prefix string variable in va_report() as const"

 Makefile     | 2 +-
 lib/report.c | 6 +++---
 x86/msr.c    | 4 ++--
 x86/pmu.c    | 2 +-
 4 files changed, 7 insertions(+), 7 deletions(-)
Makefile +0 -0
index 933b9f0..e79cf93 100644

Makefile cc-option = $(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null \ · line 54

Quoted 5 lines
Makefile +1 -1
@@ -51,7 +51,7 @@ cc-option = $(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null \
               > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
 
 CFLAGS += -g
-CFLAGS += $(autodepend-flags) -Wall -Werror
+CFLAGS += $(autodepend-flags) -Wall -Wwrite-strings -Werror

I assume this option has been around for quite some while, so no reasonable gcc will spit fire.

Signature · 3 lines
Thanks,

David
#2 lore raw
Quoted 4 lines · David Hildenbrand
Quoted 1 line · Thomas Huth

So we make sure that we do not accidentially write to constant

"accidentally" (I wouldn't know if Thunderbird wouldn't tell me ;) )

Oh, ok. Paolo, Radim, could you please fix it when picking up the patch?

Quoted 21 lines
Quoted 21 lines

strings. Also add some missing "const" qualifiers in the code to avoid that we get compiler warnings now.

---
This patch supersedes my previous patch "Declare the prefix string variable in va_report() as const"

 Makefile     | 2 +-
 lib/report.c | 6 +++---
 x86/msr.c    | 4 ++--
 x86/pmu.c    | 2 +-
 4 files changed, 7 insertions(+), 7 deletions(-)
Makefile +0 -0
index 933b9f0..e79cf93 100644
@@ -51,7 +51,7 @@ cc-option = $(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null \
               > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
 
Quoted 7 lines

Makefile cc-option = $(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null \ · line 54

Quoted 3 lines
Makefile +1 -1
 CFLAGS += -g
-CFLAGS += $(autodepend-flags) -Wall -Werror
+CFLAGS += $(autodepend-flags) -Wall -Wwrite-strings -Werror

I assume this option has been around for quite some while, so no reasonable gcc will spit fire.

A very quick search revealed that it is at least 14 years old and GCC 4.4 already contained it. Likely even earlier versions. I think we do not really support older versions anymore, so that should be ok.

Quoted 1 line

Thanks!

Thomas

maintainer
#3 lore raw
Quoted 7 lines · Thomas Huth
Quoted 4 lines · David Hildenbrand
Quoted 1 line · Thomas Huth

So we make sure that we do not accidentially write to constant

"accidentally" (I wouldn't know if Thunderbird wouldn't tell me ;) )

Oh, ok. Paolo, Radim, could you please fix it when picking up the patch?

Sure.

Quoted 6 lines
Quoted 2 lines

I assume this option has been around for quite some while, so no reasonable gcc will spit fire.

A very quick search revealed that it is at least 14 years old and GCC 4.4 already contained it. Likely even earlier versions. I think we do not really support older versions anymore, so that should be ok.

Yes, I think I've used it in GCC 2.x or so. :)

Paolo

Quoted 6 lines
Quoted 1 line

Thanks!

Thomas