Solved

gcc bug !?

Posted on 1998-10-31
11
316 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
ID: 1638422
modification of a string literal is undefined behavior
writable string literals is a common extension
0
 

Author Comment

by:bod_1
ID: 1638423
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
ID: 1638424
then you weren't taught in strict compliance to ANSI Standard X3.159-1989
try
static char sDev[] = "/dev/hd_\n";
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Author Comment

by:bod_1
ID: 1638425
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
ID: 1638426
Yes,
That was the problem, I tried the same thing declaring an array instead and there were no problems.
0
 

Author Comment

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

Accepted Solution

by:
ozo earned 20 total points
ID: 1638428
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
ID: 1638429
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
ID: 1638430
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
ID: 1638431
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
ID: 1638432
A string literal is a character sequence enclosed in double-quotes
(const char *) may be unmodifiable
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 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