Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is modification of string literals undefined behaviour according to the C89 standard?

Tags:

c

I believe that in C99, modification of string literals is undefined behaviour. I don't have a copy of that standard but I do have a draft of C1X (n1570) which states in 6.4.5 paragraph 7:

It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

I have found a Stack Overflow question that touches on this topic and contains the following comment from Jonathan Leffler:

Originally, the C89 (C90) standard did not outlaw modifying literals because there was too much code written before the standard that would be broken by it.

But I have also seen lots of discussion of the type of string literals and the fact that they are char[N] and not const char[N]. I gather that this decision was taken so that the large body of existing code would not break.

Can anyone give me a definitive answer. Is string literal modification UB in C89?

like image 360
David Heffernan Avatar asked Apr 03 '12 20:04

David Heffernan


People also ask

Can string literals be modified?

The behavior is undefined if a program attempts to modify any portion of a string literal. Modifying a string literal frequently results in an access violation because string literals are typically stored in read-only memory.

What are the two types of string literals?

A string literal with the prefix L is a wide string literal. A string literal without the prefix L is an ordinary or narrow string literal. The type of narrow string literal is array of char .

Which is a valid string literal?

The valid string literals for the linkage specification to call programs are: "OS" OS linkage call. "OS nowiden"

How many types of string literals are there?

There are three sets of literal types available in TypeScript today: strings, numbers, and booleans; by using literal types you can allow an exact value which a string, number, or boolean must have.


1 Answers

Yes, they are non-modifiable in C89.

(C90, 6.1.4) "If the program attempts to modify a string literal of either form, the behavior is undefined"

Even in K&R 2nd edition, there are quotes regarding the immutability of string literals.

(K&R2, 5.5) "the result is undefined if you try to modify the string contents"

(K&R2, Appendix C) "Strings are no longer modifiable, and so may be placed in read-only memory"

In the ANSI C89 Rationale, there is an explanation of why it is non-modifiable:

(ANSI C89 Rationale, 3.1.4) "String literals are specified to be unmodifiable. This specification allows implementations to share copies of strings with identical text, to place string literals in read-only memory, and perform certain optimizations."

like image 193
ouah Avatar answered Sep 28 '22 16:09

ouah