T
Thomas Barth
Hi,
I would like to create a file index like updatedb on Linux does as a
part of my program, but I dont know how long the filenames could be.
Therefore I want to use malloc to keep the size of the filenames
flexible. I would expect an segmentation fault with the following
sourcecode, when invoking strcat or sprintf to assign the first value to
szFile because of missing space.
function with recursion...
char *szFile = NULL,
....
while((ptrDirentry = readdir(ptrDir)) != NULL) {
if (strcmp((*ptrDirentry).d_name, ".") != 0 &&
strcmp((*ptrDirentry).d_name, "..") != 0) {
setAbsoluteFilename(szFile, szDir, (*ptrDirentry).d_name);
....
void setAbsoluteFilename(char *szFile, char *szDir, char *szFilename) {
int iLenF = strlen(szFilename),
iLenD = strlen(szDir);
szFile = (char *)malloc(1); //???
//szFile = (char *)malloc((iLenD + iLenF + 1));
strcat(szFile, szDir);
strcat(szFile, "/");
strcat(szFile, szFilename);
//sprintf(szFile, "%s/%s", szDir, szFilename);
printf("Filename: %s, Size: %d\n", szFile, strlen(szFile));
}
Why is it possible to assign strings to szFile bigger than space is
allocated?
Regards,
T h o m a s B
I would like to create a file index like updatedb on Linux does as a
part of my program, but I dont know how long the filenames could be.
Therefore I want to use malloc to keep the size of the filenames
flexible. I would expect an segmentation fault with the following
sourcecode, when invoking strcat or sprintf to assign the first value to
szFile because of missing space.
function with recursion...
char *szFile = NULL,
....
while((ptrDirentry = readdir(ptrDir)) != NULL) {
if (strcmp((*ptrDirentry).d_name, ".") != 0 &&
strcmp((*ptrDirentry).d_name, "..") != 0) {
setAbsoluteFilename(szFile, szDir, (*ptrDirentry).d_name);
....
void setAbsoluteFilename(char *szFile, char *szDir, char *szFilename) {
int iLenF = strlen(szFilename),
iLenD = strlen(szDir);
szFile = (char *)malloc(1); //???
//szFile = (char *)malloc((iLenD + iLenF + 1));
strcat(szFile, szDir);
strcat(szFile, "/");
strcat(szFile, szFilename);
//sprintf(szFile, "%s/%s", szDir, szFilename);
printf("Filename: %s, Size: %d\n", szFile, strlen(szFile));
}
Why is it possible to assign strings to szFile bigger than space is
allocated?
Regards,
T h o m a s B