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
Open
Give C string constants the type const char[N] instead of char[N] (-Wwrite-strings)#14666MisterDA wants to merge 2 commits intoocaml:trunkfrom
const char[N] instead of char[N] (-Wwrite-strings)#14666MisterDA wants to merge 2 commits intoocaml:trunkfrom
Conversation
> 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
-Wwrite-strings flag if supportedconst char[N] instead of char[N] (-Wwrite-strings)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR enables the use of the
-Wwrite-stringsflag of GCC and Clang for a bit of added type safety. Some background on character string literals and this flag: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-stringsflag.https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wwrite-strings
There are interfaces such as
dlerrorwhich return achar *that is missing theconstqualifier, but whose documentation forbids modifying the string returned. Other functions such asexecveexpect constant parameters, but for historical reasons or other limitations, theconstqualifier cannot be used.