Segmentation Fault when changing a passed string parm

I have code that works under SCO Unix, but gives a segmentation fault under Linux. The line of code that causes the segmentation fault is:
pszStr[i] = 0x00;
See below for the code in context. If I remove the offending lines of code, everything is OK, until I hit the next piece of code that changes a passed string.

I have multiple source files, linked together into one executable. The following code is in one source file. It takes a string, splits it (at line ends, \n) into multiple writes it to a file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* ------------------------------------------------------------------ */
void JcTrace(char * pszStr)
/* ------------------------------------------------------------------ */

{
auto   FILE *        pFile;
auto   int           cStrLen;
auto   int           iStart;
auto   int           i;
auto   int           fWritten;
auto   UCHAR         save;

if ((pFile = jcOpenTraceFile()) == NULL)
    return;

cStrLen  = strlen(pszStr);
fWritten = FALSE;
iStart = 0;

for (i=0 ; i<cStrLen ; i++)
    {
    if (pszStr[i] == '\n')
        {
        i++;
        save      = pszStr[i];
        pszStr[i] = 0x00;            //THIS CAUSES SEGMENTATION FAULT
        jcWriteOneLine(pFile,&pszStr[iStart]);
        fWritten = TRUE;
        pszStr[i] = save;            //THIS ALSO CAUSES SEGMENTATION FAULT
        iStart    = i;
        }
    }

if ((!fWritten)
&&  (cStrLen))
    jcWriteOneLine(pFile,pszStr);

fclose(pFile);
}


Here is the code in another source file which calls the above code:
1
2
3
4
5
6
7
8
/* ------------------------------------------------------------------ */
static int jcGetCsa(void)
/* ------------------------------------------------------------------ */

{
JcTrace("*jcGetCsa\n");
return(TRUE);
}
You are passing in a literal string which is essentially a const char* (pointer to a string in read only memory) and you are attempting to write to it.

Thanks. Can I change the literal some how so that is not defined as const? After getting by this piece of code, I found another, where there is a table of data defined as static in one program, then it calls another to change any lower case data to upper case. Since this was allowed in SCO Unix, who knows how many more conditions of this I have. It is a very large program.
Can I change the literal some how so that is not defined as const?
No. String literals are allocated in a memory section that's read-only (.text, IIRC). There's no way to change this behavior, and anything written there, regardless of what it is, will result in a segmentation fault.
The only way around this is by copying the parameter to a temporary string.

By the way, if the original coder had properly used const and made JcTrace() take a 'const char *', then the compiler wouldn't have let you do something like that in the first place.
Thanks for the replies. This helps. Hopefully I won't find too many of these.
Topic archived. No new replies allowed.