Solved

gcc bug !?

Posted on 1998-10-31
11
312 Views
Last Modified: 2010-04-22
Hi,
I think that I found some kind of bug in gcc.
This simple code to cycle through devices changes the last letter (device name, hda, hdb, etc)
This code compiles and runs fine on msDev, Borland, AIX V5.
When I compiled it on my Linux machine it results in a core dump (segmentation fault) as soon as you try to change the device letter.


#include <stdio.h>


int main()
{
    char* sDev = "/dev/hd_\n", /* the '_' is char # 8 */
        *p = NULL;

    p = &sDev[8];

    printf("%s\n", sDev);

    *p = ((char)0x61); /* ERROR on Linux */

    printf("%s\n", sDev);

    return 0;
}

?
0
Comment
Question by:bod_1
  • 6
  • 5
11 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
modification of a string literal is undefined behavior
writable string literals is a common extension
0
 

Author Comment

by:bod_1
Comment Utility
I was taught that in C you'r supposed to be able to alter strings and arrays through an item's index or a pointer to that index.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
then you weren't taught in strict compliance to ANSI Standard X3.159-1989
try
static char sDev[] = "/dev/hd_\n";
0
 

Author Comment

by:bod_1
Comment Utility
ahh,
I've been using msDev and Borland soo often that using strict compiling really messed me up.  I still need to get used to it.
That answers my question.
Thanks Ozo.
0
 

Author Comment

by:bod_1
Comment Utility
Yes,
That was the problem, I tried the same thing declaring an array instead and there were no problems.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:bod_1
Comment Utility
Ozo,
Do you want these points?
You straightened out my question for me
0
 
LVL 84

Accepted Solution

by:
ozo earned 20 total points
Comment Utility
although it is a common extension to allow writeable string literals, and to guarantee that identical string literalls be distinct,
the ANSI C standard actually specifies them to be unmodifiable,
and if a program attemps to modify a literal rhe behavior is undefined.
a strictly conforming program should instead use:

     static char sDev[] = "/dev/hd_\n";

0
 

Author Comment

by:bod_1
Comment Utility
When you told me that I lost some respect for C.  That rule puts some serious restrictions on what can be done regarding strings.
Altering unknown length strings is sometimes a necessity.

eg.  For a recent assignment at school I implemented a function with the ...'s.  If I had it my way, I would have malloc'd a string to hold the arguments, and realloc'd the string as needed if there were more args.  Because of that rule, I just set a limit on the number of args that could be passed.  I would have preferred the limits being set by the amount of memory available.
If I sound ignorant (ie. undoubtably there are some good reasons behind that rule) please excuse me;  I am still very new to this.

Anyways, thanks for the help Ozo.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
malloc and realloc are in the C standard, so it sounds like your way should work.

The ANSI C Rationale (which is not actually part of the X3.159-1989 standard itself) says about section 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.
...
Those members of the Committee who insisted that string literals should be modifiable were content to have this practice designated a common extension (see F.5.5).
Existing code which modifies string literals can be made strictly conforming by replacing the string literal with an initialized static character array.
...
0
 

Author Comment

by:bod_1
Comment Utility
Ah,  I never tried modifying the string after using malloc...assumed that the rule applied to all char*'s.
That's not so bad then.
Thanks again Ozo
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
A string literal is a character sequence enclosed in double-quotes
(const char *) may be unmodifiable
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

In this tutorial I will explain how to make squid prevent malwares in five easy steps: Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-…
This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now