Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Blog | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Segmentation fault



This could be a malloc quirk. I don;t know what algorithms are used by 
malloc in glibc. Some mallocs try to give you exactly what you ask for, 
but malloc is also required to give you a pointer which is always on a 
"natual", boundary. There should be nothing special about 12. 
You call rest_of_string(temp, i), in a for loop where i varies from 0 to (len - 
1). In the final case:
	temp = rest_of_string(temp, i);
	usedchar then == i == len - 1;
	so string[usedchar] = '\0'; is ok,
	BUT:strcat( string, (string + usedchar + 1) );
	is really strcat(string, string + len);
	Now, you allocate temp for len bytes, to string[len - ] is still 1 byte 
	beyond the end of your allocation. And string + len is 2 bytes 
     beyond, and you have not initialized the string beyond that so you 
     really could have garbage:
	Here is an example:
	string = "abcd";
	len = strlen(string);
	temp = malloc(len); 
	/* you allocated 4 bytes */
	strcpy(temp, string); /* copy 5 bytes */
	then:
	strcat(temp, temp + len); 
	/* this itself should be ok because temp[4] is a null, but still wrong,
	* which is strcat(temp, "\0"); But I think there was a case where that 
     * 1 bytes was actually a second one. I don't have your original code 
	* here. Still, you should try to figure out what the error actually is. 
	*/
	Since you are exceeding the bounds of the array, even by one 
	character, you may be stepping on something. You also need not
	multiply len by sizeof(char) in this case since it is understood that
	that the len is in sizeof(char) units in C. 

On 7 Feb 00, at 12:45, Derek Martin wrote:

> Though I still don't quite understand why this works fine if the string's
> length is less than 12, and not otherwise... 
> 
> > > char *
> > > rest_of_string( char *string, int usedchar )
> > > {
> > >   
> > >   string[usedchar] = '\0';
> > >   strcat( string, (string + usedchar + 1) );
> > >   return string;
> > >   
> > > }

--
Jerry Feldman
Contractor, eInfrastructure Partner Engineering   
508-467-4315 http://www.testdrive.compaq.com/linux/

Compaq Computer Corp.
200 Forest Street MRO1-3/F1
Marlboro, Ma. 01752
-
Subcription/unsubscription/info requests: send e-mail with
"subscribe", "unsubscribe", or "info" on the first line of the
message body to discuss-request at blu.org (Subject line is ignored).




BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org