ISO/IEC TS 17961 [strmod]
Modifying string literals
Description
Rule Definition
Modifying string literals.1
Polyspace Implementation
This checker checks for Writing to const qualified object.
Examples
Writing to const qualified object
Writing to const
qualified
object occurs when you do one of the following:
Use a
const
-qualified object as the destination of an assignment.Pass a
const
-qualified object to a function that modifies the argument.
For instance, the defect can occur in the following situations:
You pass a
const
-qualified object as first argument of one of the following functions:mkstemp
mkostemp
mkostemps
mkdtemp
You pass a
const
-qualified object as the destination argument of one of the following functions:strcpy
strncpy
strcat
memset
You perform a write operation on a
const
-qualified object.
The risk depends upon the modifications made to the
const
-qualified object.
Situation | Risk |
---|---|
Passing to mkstemp ,
mkostemp , mkostemps ,
mkdtemp , and so on. | These functions replace the last six characters of their first
argument with a string. Therefore, they expect a modifiable
char array as their first argument. |
Passing to strcpy , strncpy ,
strcat , memset and so
on. | These functions modify their destination argument. Therefore,
they expect a modifiable char array as their
destination argument. |
Writing to the object | The const qualifier implies an agreement that
the value of the object will not be modified. By writing to a
const -qualified object, you break the
agreement. The result of the operation is undefined. |
The fix depends on the modification made to the const
-qualified
object.
Situation | Fix |
---|---|
Passing to mkstemp ,
mkostemp , mkostemps ,
mkdtemp , and so on. | Pass a non-const object as first argument
of the function. |
Passing to strcpy ,
strncpy , strcat ,
memset and so on. | Pass a non-const object as destination
argument of the function. |
Writing to the object | Perform the write operation on a non-const
object. |
See examples of fixes below.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
const
-Qualified Object#include <string.h>
const char* buffer = "abcdeXXXXXXX";
void func(char* string) {
char *ptr = (char*)strchr(buffer,'X');
if(ptr)
strcpy(ptr,string);
}
In this example, because buffer
is const
-qualified, strchr(buffer,'X')
returns
a const
-qualified char*
pointer.
When this char*
pointer is used as the destination
argument of strcpy
, a Writing to const
qualified object error appears.
const
-Qualified
Object to Non-const
ObjectOne possible correction is to assign the constant string to
a non-const
object and use the non-const
object
as destination argument of strchr
.
#include <string.h> char buffer[] = "abcdeXXXXXXX"; void func(char* string) { char *ptr = (char*)strchr(buffer,'X'); if(ptr) strcpy(ptr,string); }
Check Information
Decidability: Undecidable |
Version History
Introduced in R2019a
1 Extracts from the standard "ISO/IEC TS 17961 Technical Specification - 2013-11-15" are reproduced with the agreement of AFNOR. Only the original and complete text of the standard, as published by AFNOR Editions - accessible via the website www.boutique.afnor.org - has normative value.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)