Main Content

CERT C: Rec. STR05-C

R2026b

Use pointers to const when referring to string literals

Since R2026b

Description

Use pointers to const when referring to string literals1

Polyspace Implementation

Polyspace® checks for the issue Non-const pointer to string literal.

Examples

expand all

Issue

The issue occurs when a pointer is initialized with a string literal but the pointed-to type is not const-qualified. The checker reports violations on declarations and initializations involving:

  • Narrow (char*), wide (wchar_t*), and Unicode (char16_t*, char32_t*) string literal pointers

  • Structure member initializations where a non-const member receives a string literal

  • Typedef-based pointer types that resolve to a non-const character pointer

  • Function parameter declarations that accept string literals through a non-const pointer

A char* const declaration (const pointer to non-const data) is reported as a violation because this pattern qualifies the pointer as constinstead of the pointed-to type. Assignment of a string literal to an already declared pointer is not reported as a violation.

Risk

The C standard defines string literals as constant arrays of type char or wchar_t, and modifying them results in undefined behavior. If you don't use a const qualifier on a pointer initialized with a string literal, the compiler cannot prevent writes through the pointer, and any such write can crash the program or corrupt memory.

Fix

Add const to the pointed-to type so the declaration reflects the immutability of the string literal. The const qualifier makes the immutability explicit and allows the compiler to catch accidental modifications at compile time.

When you need the string content to be mutable, use array initialization instead, which copies the literal into a modifiable local array.

Example — Pointer to Narrow String Literal

In this example, Polyspace reports a violation when the non-const pointer message is initialized using a string literal.


#include <stdio.h>

void print_greeting(void) {
    char *message = "Hello, world!";  // Noncompliant
    printf("%s\n", message);
}
Correction — Add const Qualifier

Add const to the pointer declaration to indicate that the pointed-to data is read-only.


#include <stdio.h>

void print_greeting(void) {
    const char *message = "Hello, world!";  // Compliant
    printf("%s\n", message);
}

Check Information

Group: Rec. 07. Characters and Strings (STR)
PQL Name: std.cert.STR05_C

Version History

Introduced in R2026b


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.