Skip to contentSkip to conversation

xen/common: Use const char * for string literals

PATCH In review Andrew Cooper 2 participants 2 messages Jul 14, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/common: Use const char * for string literals

For pre-ANSI-C compatibility reasons, string literals have a mutable type, but it is undefined behaviour to mutate them.

Swap char *'s to const char *'s for variables which hold string literals.

This fixes several violations of MISRA Rule 7.4:

A string literal shall not be assigned to an object unless the object's type is "pointer to const-qualified char".

No functional change.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>

---
 xen/common/gunzip.c                | 2 +-
 xen/common/ioreq.c                 | 3 ++-
 xen/common/libelf/libelf-dominfo.c | 2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)
xen/common/gunzip.c +1 -1
index b810499be2b6..2c6eae167d54 100644
@@ -52,7 +52,7 @@ typedef unsigned long   ulg;
 static long __initdata bytes_out;
 static void flush_window(void);
 
-static __init void error(char *x)
+static __init void error(const char *x)
 {
     panic("%s\n", x);
 }
xen/common/ioreq.c +2 -1
index ecb8f545e1c4..7cb717f7a2a4 100644
@@ -501,7 +501,8 @@ static int ioreq_server_alloc_rangesets(struct ioreq_server *s,
 
     for ( i = 0; i < NR_IO_RANGE_TYPES; i++ )
     {
-        char *name, *type;
+        const char *type;
+        char *name;
 
         switch ( i )
         {
xen/common/libelf/libelf-dominfo.c +1 -1
index fcaee2f7bcff..a13a5e4db603 100644
@@ -100,7 +100,7 @@ elf_errorstatus elf_xen_parse_note(struct elf_binary *elf,
 {
 /* *INDENT-OFF* */
     static const struct {
-        char *name;
+        const char *name;
         bool str;
     } note_desc[] = {
         [XEN_ELFNOTE_ENTRY] = { "ENTRY", 0},
Signature · 1 line
2.30.2
Acked-by on v1 1/1
#1 lore raw

Hi Andrew,

Quoted 5 lines · Andrew Cooper

For pre-ANSI-C compatibility reasons, string literals have a mutable type, but it is undefined behaviour to mutate them.

Swap char *'s to const char *'s for variables which hold string literals.

Quoted 8 lines

This fixes several violations of MISRA Rule 7.4:

A string literal shall not be assigned to an object unless the object's type is "pointer to const-qualified char".

No functional change.

Cheers,

Signature · 1 line
Julien Grall