Skip to content

Commit

Permalink
Update rule.adoc
Browse files Browse the repository at this point in the history
  • Loading branch information
frederic-tingaud-sonarsource authored Oct 16, 2024
1 parent fb567b6 commit e26f9f6
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions rules/S7129/cfamily/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
== Why is this an issue?

String literals, represented by a text surrounded by `"` like `"Hello world"` are stored in read-only memory and cannot be mutated.
String literals, represented by a text surrounded by double quotes like `"Hello world"` are stored in read-only memory and cannot be mutated.

Historically, string literals were introduced in C before the keyword `const` so it was possible to create a mutable pointer to a string literal:

Expand All @@ -9,13 +9,13 @@ Historically, string literals were introduced in C before the keyword `const` so
char * ptr = "Hello world!"; // Noncompliant
----

To maintain retrocompatibility with an enormous amount of code that had been written without const, pointing to a string literal with a mutable char pointer is still allowed in C and although it has been officially removed from {cpp} since {cpp}11, most compilers still allow it with a warning.
To maintain retro-compatibility with the enormous amount of code that had been written without const, pointing to a string literal with a mutable char pointer remained allowed in C, and although it has been officially removed from {cpp} since {cpp}11, most compilers still allow it with a warning.

Because it is a pointer to read-only memory, trying to modify `ptr` will lead to undefined behavior. And because it is not declared as `const`, the typesystem will not be able to prevent or warn of such modification attempt.
Because it is a pointer to read-only memory, trying to modify `ptr` will lead to undefined behavior. And because it is not declared as `const`, the type system will not be able to prevent or warn of such modification attempts.

=== Exceptions

This rule doesn't raise an issue for explicit conversion to `char *`.
This rule doesn't raise an issue for explicit conversions to `char *`.

[source,c]
----
Expand All @@ -32,7 +32,7 @@ That remains a very dangerous pattern that should be avoided as much as possible

==== Immutable case

In {cpp}, since {cpp}17, if the text does not need to be mutated, the optimal way to fix it is to use `std::string_view`. It doesn't create an unnecessary copy and it points to the literal in a non-mutable way.
Since {cpp}17, if the text does not need to be mutated, the optimal way to fix this issue is to use `std::string_view`. It doesn't create an unnecessary copy and it points to the literal in a non-mutable way.

[source,cpp,diff-id=1,diff-type=noncompliant]
----
Expand Down Expand Up @@ -62,7 +62,7 @@ std::string s = "Hello world!"; // Compliant, mutable

==== Immutable case

In C, if the text doesn't need to mutate, the pointer should be declared const
In C, if the text doesn't need to be mutated, the pointer should be declared const

[source,cpp,diff-id=3,diff-type=noncompliant]
----
Expand All @@ -76,23 +76,23 @@ const char * s = "Hello world!"; // Compliant, non mutable

==== Mutable case

In C, if the text needs to be mutated, the data needs to be copied in a mutable array.
In C, if the text needs to be mutated, the data needs to be fully copied.

[source,cpp,diff-id=4,diff-type=noncompliant]
----
char * s = "Hello world!"; // Noncompliant
mutate(s);
----

The copy can be on the stack
The copy can be made on the stack by declaring a new array initialized with the content of the literal:

[source,cpp,diff-id=4,diff-type=compliant]
----
char s[] = "Hello world!"; // Compliant, full copy on the stack
mutate(s);
----

Or on the heap, in which case it will need to be ``free``d aftwerward
Or on the heap with `strdup`, in which case it will need to be ``free``d afterward:

[source,cpp,diff-id=4,diff-type=compliant]
----
Expand Down

0 comments on commit e26f9f6

Please sign in to comment.