Skip to content

Give C string constants the type const char[N] instead of char[N] (-Wwrite-strings)#14666

Open
MisterDA wants to merge 2 commits intoocaml:trunkfrom
MisterDA:Wwrite-strings
Open

Give C string constants the type const char[N] instead of char[N] (-Wwrite-strings)#14666
MisterDA wants to merge 2 commits intoocaml:trunkfrom
MisterDA:Wwrite-strings

Conversation

@MisterDA
Copy link
Copy Markdown
Contributor

This PR enables the use of the -Wwrite-strings flag of GCC and Clang for a bit of added type safety. Some background on character string literals and this flag:

The type of the [character string] literal is char[N], where N is the size of the string in code units of the execution narrow encoding, including the null terminator.

String literals are not modifiable (and in fact may be placed in read-only memory such as .rodata). If a program attempts to modify the static array formed by a string literal, the behavior is undefined.

char* p = "Hello";
p[1] = 'M'; // Undefined behavior
char a[] = "Hello";
a[1] = 'M'; // OK: a is not a string literal

https://en.cppreference.com/w/c/language/string_literal.html

In C++ ordinary string literals have type const char[N] which prevents the undefined behavior. GCC and Clang allow changing the type of character literals for just a bit more of type safety with the -Wwrite-strings flag.

When compiling C, give string constants the type const char[length] so that copying the address of one into a non-const char * pointer produces a warning. These warnings help you find at compile time code that can try to write into a string constant, but only if you have been very careful about using const in declarations and prototypes.

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wwrite-strings

There are interfaces such as dlerror which return a char * that is missing the const qualifier, but whose documentation forbids modifying the string returned. Other functions such as execve expect constant parameters, but for historical reasons or other limitations, the const qualifier cannot be used.

> The type of the [character string] literal is `char[N]`, where `N`
> is the size of the string in code units of the execution narrow
> encoding, including the null terminator.

> String literals are not modifiable (and in fact may be placed in
> read-only memory such as `.rodata`). If a program attempts to modify
> the static array formed by a string literal, the behavior is
> undefined.
>
> ```c
> char* p = "Hello";
> p[1] = 'M'; // Undefined behavior
> char a[] = "Hello";
> a[1] = 'M'; // OK: a is not a string literal
> ```

https://en.cppreference.com/w/c/language/string_literal.html

In C++ ordinary string literals have type `const char[N]` which
prevents the undefined behavior. GCC and Clang allow changing the type
of character literals for just a bit more of type safety with the
`-Wwrite-strings` flag.

> When compiling C, give string constants the type
> `const char[length]` so that copying the address of one into a
> non-`const char *` pointer produces a warning. These warnings help
> you find at compile time code that can try to write into a string
> constant, but only if you have been very careful about using `const`
> in declarations and prototypes.

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wwrite-strings

There are interfaces such as [`dlerror`][1] which return a `char *`
that is missing the `const` qualifier, but whose documentation forbids
modifying the string returned. Other functions such as [`execve`][2]
expect constant parameters, but for historical reasons or other
limitations, the `const` qualifier cannot be used.

[1]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/dlerror.html
[2]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/execve.html#tag_17_129_08
@MisterDA MisterDA changed the title Enable the -Wwrite-strings flag if supported Give C string constants the type const char[N] instead of char[N] (-Wwrite-strings) Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants