Re: strcpy giving sigsegv error
- From: gordonb.uk3v5@xxxxxxxxxxx (Gordon Burditt)
- Date: Sun, 21 May 2006 01:24:34 -0000
I'm writing a program to traverse up a homemade file system and print
the path that is taken to get to root. It should be very similar to
pwd. Where I've run into to trouble is that the creation of the path
string using strcat and strcpy has drawn an error. I tried making the
char* path variable initialized with a space character to prevent null
pointers but I still get a sigsegv error.
If you write on the memory of a quoted string literal, you have invoked
the wrath of undefined behavior, often expressed as a smegmentation fault.
If you copy more than one character to path with strcpy(), you've run
off the end of allocated memory for what path points at, which also
invokes the wrath of undefined behavior. How long a memory buffer
do you need for path? If you're going to go with a fixed-length
buffer (not a real good idea), I'd use at least 10k.
Gordon L. Burditt
The code I'm working with is
at the bottom. I'm able to extract the names of the files in the order
dir3, dir2, dir1, root if the path they would normally generate would
be root/dir1/dir2/dir3. When I debug the code I see that in the strcpy
call in the do-while loop that both path and dirName both are valid
pointers pointing to null terminated strings. I get the error the
first time through. If anyone wants to see my debugging scripts I'd be
willing to post those as well.
Brian
void doPWD(Arg * a)
{
uint currIn = cfv->root.nInode;
uint parInd, curTrvIn;
char *path = " ", *dirName;
do
{
parInd = cfv->root.findName("..");
curTrvIn = cfv->root.findName(".");
if ( curTrvIn == 1 ) break;
cfv->root.destroy();
cfv->root.reCreate(cfv, parInd);
dirName = cfv->root.findNameWNum(curTrvIn);
printf("%s\n",dirName);
strcat(dirName, "/");
strcat(dirName, path);
strcpy(path, dirName); // ***ERROR*** here and path is the
culprit
} while (1);
dirName = "/";
strcat(dirName, path);
strcpy(path, dirName);
printf("%s\n",path);
cfv->root.destroy();
cfv->root.reCreate(cfv, currIn);
}
.
- References:
- strcpy giving sigsegv error
- From: mcraven . 2
- strcpy giving sigsegv error
- Prev by Date: Re: strcpy giving sigsegv error
- Next by Date: Re: unix "getch()" question ,why my not print right char?
- Previous by thread: Re: strcpy giving sigsegv error
- Index(es):
Relevant Pages
|