Modifying a char pointer initialized by character string literal should cause an error.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Modifying a char pointer initialized by character string literal should cause an error.

瞿兴

Hi,

In section 6.7.8 of the C99 draft document defines :

If an attempt is made to use pointer variable p to modify the contents of the array, the behavior is undefined. 

When executing the testcase(in the attachment), tinyc does not report an error and modify the value of the variable NISLParameter0 successfully while other compilers such as gcc and clang report a runtime error: “Segmentation fault”. I think it would be better for tinyc to throw an error or at least a warning for the wrong action.

 

The OS is:

Linux version 4.15.0-65-generic

 

Compiler Version:

tcc version 0.9.27 (x86_64 Linux)

 

Output:

111

abcde1ghij

 

Expected output:

Throw an error or at least a warning.

 

The attachment includes the relevant testcase. Looking forward to your reply.


_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

testcase.c (368 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Modifying a char pointer initialized by character string literal should cause an error.

uso ewin
On Fri, Dec 27, 2019 at 4:22 PM 瞿兴 <[hidden email]> wrote:

>
> Hi,
>
> In section 6.7.8 of the C99 draft document defines :
>
> If an attempt is made to use pointer variable p to modify the contents of the array, the behavior is undefined.
>
> When executing the testcase(in the attachment), tinyc does not report an error and modify the value of the variable NISLParameter0 successfully while other compilers such as gcc and clang report a runtime error: “Segmentation fault”. I think it would be better for tinyc to throw an error or at least a warning for the wrong action.
>
>
>
> The OS is:
>
> Linux version 4.15.0-65-generic
>
>
>
> Compiler Version:
>
> tcc version 0.9.27 (x86_64 Linux)
>
>
>
> Output:
>
> 111
>
> abcde1ghij
>
>
>
> Expected output:
>
> Throw an error or at least a warning.
>
>
>
> The attachment includes the relevant testcase. Looking forward to your reply.

Undefined Behavior mean the Behavior is undefined,
so not having an error is as okay as a segmentation fault
Also, even on gcc on linux, if you use mprotec
you can modify a string literal without error.

_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel
Reply | Threaded
Open this post in threaded view
|

Re: Modifying a char pointer initialized by character string literal should cause an error.

Daniel Glöckner-2
In reply to this post by 瞿兴
Hi,

On Fri, Dec 27, 2019 at 10:26:24PM +0800, 瞿兴 wrote:
> In section 6.7.8 of the C99 draft document defines :
>
> If an attempt is made to use pointer variable p to modify the contents of the array, the behavior is undefined.
>
> When executing the testcase(in the attachment), tinyc does not report an error and modify the value of the variable NISLParameter0 successfully

Well, "undefined behavior" allows tinycc to do this.

> The attachment includes the relevant testcase.

IMHO the only thing tinycc should do is to warn that a const char * has
been implicitly cast to char * in the initialization of NISLParameter0.
Putting string literals into read-only sections is a nice to have.

>     char *NISLParameter0 = "abcdefghij";

Best regards,

  Daniel

_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel