Discussion:
segmentation fault in strtok_r()
(too old to reply)
qazmlp
2003-07-30 01:05:16 UTC
Permalink
There is a segmentation fault when running the executable of the
following code.
Please suggest a fix for this.

I compiled the code with: "CC -mt file.C"
pstack core info:
core 'core' of 26704: a.out
----------------- lwp# 1 / thread# 1 --------------------
ff24db7c strtok_r (10c9d, 10c84, ffbeeb2c, ff2bbc60, 6d, ff00) + 58
00010a4c char*trimLeadEndSpace(char*) (0, 0, 0, 219b8, 0, 0) + 8c
00010b5c main (1, ffbeec04, ffbeec0c, 20c00, 0, 0) + c
00010998 _start (0, 0, 0, 0, 0, 0) + 108
----------------- lwp# 2 / thread# 2 --------------------
ff2999ec _signotifywait (ff30e000, 5c, 0, 0, 0, 0) + 8
ff2f1c88 thr_yield (0, 0, 0, 0, 0, 0) + 8c
----------------- lwp# 3 --------------------------------
ff2975b0 _door_return (ff1e5d78, 0, 6000, ffbee6bc, 0, 0) + 10
ff2f1c88 thr_yield (0, 0, 0, 0, 0, 0) + 8c
-------------------------- thread# 3 --------------------
ff2ed8e8 _reap_wait (ff312a30, 209f4, 0, ff30e000, 0, 0) + 38
ff2ed640 _reaper (ff30ee58, ff314798, ff312a30, ff30ee30, 1,
fe400000) + 38
ff2fbb34 _thread_start (0, 0, 0, 0, 0, 0) + 40


// Code Starts here
#include <string.h>
#include <stdio.h>

char * trimLeadEndSpace( char * pCharArray )
{
printf("pCharArray=%s", pCharArray ) ;
char *AddrWOWhiteSpace = 0 ;
char *pPTR = 0 , *ppPTR = 0 ;
char *pTOK = 0 , *ppTOK = 0 ;
char *dummy = 0 ;

if( pCharArray == NULL ) return NULL ;

int Len = strlen(pCharArray) ;
dummy = new char [ Len + 1 ] ;
strcpy( dummy , pCharArray ) ;


int len = 0 , index = 0;

//
// here we gather all the tokens and add up each length to
// give the total length of the string w/o white-spaces
//
printf("pCharArray=%s", pCharArray ) ;
pTOK = strtok_r( pCharArray , " " , &pPTR );
printf("pCharArray=%s", pCharArray ) ;
while( pTOK != NULL ){
index++;
len = len + strlen(pTOK) ;
pTOK = strtok_r( NULL , " " , &pPTR );
}

// here we prep a mem location enough for the final
// string w/o any white spaces
//
AddrWOWhiteSpace = new char [len + 1];
AddrWOWhiteSpace[0] = '\0' ;


//
// here we concat all the tokens
//
index = 0 ;
ppTOK = strtok_r( dummy , " " , &ppPTR );
while( ppTOK != NULL )
{
strncat( AddrWOWhiteSpace , ppTOK , strlen(ppTOK) ) ;

index++;
ppTOK = strtok_r( NULL , " " , &ppPTR );
}

delete [] dummy ;

return (AddrWOWhiteSpace) ;
}

int main()
{
printf("%s", trimLeadEndSpace( ":***@hotmail.com ;
project=code% A F" ) ) ;
}
Artie Gold
2003-07-30 01:38:28 UTC
Permalink
Post by qazmlp
There is a segmentation fault when running the executable of the
following code.
Please suggest a fix for this.
I compiled the code with: "CC -mt file.C"
core 'core' of 26704: a.out
----------------- lwp# 1 / thread# 1 --------------------
ff24db7c strtok_r (10c9d, 10c84, ffbeeb2c, ff2bbc60, 6d, ff00) + 58
00010a4c char*trimLeadEndSpace(char*) (0, 0, 0, 219b8, 0, 0) + 8c
00010b5c main (1, ffbeec04, ffbeec0c, 20c00, 0, 0) + c
00010998 _start (0, 0, 0, 0, 0, 0) + 108
----------------- lwp# 2 / thread# 2 --------------------
ff2999ec _signotifywait (ff30e000, 5c, 0, 0, 0, 0) + 8
ff2f1c88 thr_yield (0, 0, 0, 0, 0, 0) + 8c
----------------- lwp# 3 --------------------------------
ff2975b0 _door_return (ff1e5d78, 0, 6000, ffbee6bc, 0, 0) + 10
ff2f1c88 thr_yield (0, 0, 0, 0, 0, 0) + 8c
-------------------------- thread# 3 --------------------
ff2ed8e8 _reap_wait (ff312a30, 209f4, 0, ff30e000, 0, 0) + 38
ff2ed640 _reaper (ff30ee58, ff314798, ff312a30, ff30ee30, 1,
fe400000) + 38
ff2fbb34 _thread_start (0, 0, 0, 0, 0, 0) + 40
// Code Starts here
#include <string.h>
#include <stdio.h>
char * trimLeadEndSpace( char * pCharArray )
{
printf("pCharArray=%s", pCharArray ) ;
char *AddrWOWhiteSpace = 0 ;
char *pPTR = 0 , *ppPTR = 0 ;
char *pTOK = 0 , *ppTOK = 0 ;
char *dummy = 0 ;
if( pCharArray == NULL ) return NULL ;
int Len = strlen(pCharArray) ;
dummy = new char [ Len + 1 ] ;
strcpy( dummy , pCharArray ) ;
int len = 0 , index = 0;
//
// here we gather all the tokens and add up each length to
// give the total length of the string w/o white-spaces
//
printf("pCharArray=%s", pCharArray ) ;
pTOK = strtok_r( pCharArray , " " , &pPTR );
printf("pCharArray=%s", pCharArray ) ;
while( pTOK != NULL ){
index++;
len = len + strlen(pTOK) ;
pTOK = strtok_r( NULL , " " , &pPTR );
strtok_r requires a pointer to space that _exists_.
Please see the man page.
Post by qazmlp
}
// here we prep a mem location enough for the final
// string w/o any white spaces
//
AddrWOWhiteSpace = new char [len + 1];
AddrWOWhiteSpace[0] = '\0' ;
//
// here we concat all the tokens
//
index = 0 ;
ppTOK = strtok_r( dummy , " " , &ppPTR );
Similarly.
Post by qazmlp
while( ppTOK != NULL )
{
strncat( AddrWOWhiteSpace , ppTOK , strlen(ppTOK) ) ;
index++;
ppTOK = strtok_r( NULL , " " , &ppPTR );
}
delete [] dummy ;
return (AddrWOWhiteSpace) ;
}
int main()
{
project=code% A F" ) ) ;
}
HTH,
--ag
--
Artie Gold -- Austin, Texas
Paul Pluzhnikov
2003-07-30 03:05:23 UTC
Permalink
Post by Artie Gold
Post by qazmlp
pTOK = strtok_r( pCharArray , " " , &pPTR );
printf("pCharArray=%s", pCharArray ) ;
while( pTOK != NULL ){
index++;
len = len + strlen(pTOK) ;
pTOK = strtok_r( NULL , " " , &pPTR );
strtok_r requires a pointer to space that _exists_.
Please see the man page.
Huh? I think *you* need to revisit the man page ;-)

His use of strtok_r() is correct, but
Post by Artie Gold
Post by qazmlp
int main()
{
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
this is a problem: quoted string literals are not writable (and
strtok_r() *does* write to its argument).

In fact, CC warned him about it:

"junk.cc", line 65: Warning: String literal converted to char*
in formal argument pCharArray in call to trimLeadEndSpace(char*).

Cheers,
--
In order to understand recursion you must first understand recursion.
Paul Pluzhnikov
2003-07-30 04:49:14 UTC
Permalink
The strtok_r() function works the same as the strtok()
function, but instead of using a static buffer it uses a
pointer to a user allocated char* pointer. This pointer,
the ptrptr parameter, must be the same while parsing the
same string.
Have I gone brain-dead? :-(
Not completely: you can still type :-)

He did allocate the 'char*' pointer (on the stack) right here:

char * trimLeadEndSpace( char * pCharArray )
{ ...
char *pPTR = 0 , *ppPTR = 0 ;
^^^^^^^^^^^^^^^^^

The ptrptr (from the man page) == &pPTR, and it is 'the same'
while parsing the same string, so he's done nothing wrong WRT
strtok_r(), which uses (and changes) the value of pPTR to keep
track of where in the string to continue tokenizing ...

Cheers,
--
In order to understand recursion you must first understand recursion.
Artie Gold
2003-07-30 19:27:45 UTC
Permalink
Post by Paul Pluzhnikov
The strtok_r() function works the same as the strtok()
function, but instead of using a static buffer it uses a
pointer to a user allocated char* pointer. This pointer,
the ptrptr parameter, must be the same while parsing the
same string.
Have I gone brain-dead? :-(
Not completely: you can still type :-)
Ah, merely severe cerebral flatulence.
Post by Paul Pluzhnikov
char * trimLeadEndSpace( char * pCharArray )
{ ...
char *pPTR = 0 , *ppPTR = 0 ;
^^^^^^^^^^^^^^^^^
The ptrptr (from the man page) == &pPTR, and it is 'the same'
while parsing the same string, so he's done nothing wrong WRT
strtok_r(), which uses (and changes) the value of pPTR to keep
track of where in the string to continue tokenizing ...
Of course. ;-/

--ag
--
Artie Gold -- Austin, Texas
Casper H.S. Dik
2003-08-01 13:35:47 UTC
Permalink
Post by qazmlp
There is a segmentation fault when running the executable of the
following code.
Please suggest a fix for this.
int main()
{
project=code% A F" ) ) ;
}
You call strok() on a a string constant. Those may live in
read-only memory; strtok_r writes NUL bytes in the string.

Casper

Loading...