NUL terminate each dir entry name.
* operations, we collect the entire list in one atomic operation,
* rather than reading the directory lazily.
*/
* operations, we collect the entire list in one atomic operation,
* rather than reading the directory lazily.
*/
StgAddr
getDirectoryContents(path)
StgByteArray path;
StgAddr
getDirectoryContents(path)
StgByteArray path;
DIR *dir;
struct dirent *d;
char **entries;
DIR *dir;
struct dirent *d;
char **entries;
/* Check for an actual directory */
while (stat(path, &sb) != 0) {
/* Check for an actual directory */
while (stat(path, &sb) != 0) {
- if ((entries[count] = malloc(strlen(d->d_name))) == NULL) {
+ len = strlen(d->d_name);
+ if ((entries[count] = malloc(len+1)) == NULL) {
ghc_errtype = ERR_RESOURCEEXHAUSTED;
ghc_errstr = "not enough virtual memory";
freeEntries(entries, count);
ghc_errtype = ERR_RESOURCEEXHAUSTED;
ghc_errstr = "not enough virtual memory";
freeEntries(entries, count);
return NULL;
}
strcpy(entries[count], d->d_name);
return NULL;
}
strcpy(entries[count], d->d_name);
+ /* Terminate the sucker */
+ *(entries[count] + len) = 0;
if (++count == alloc) {
alloc += LINK_MAX;
if ((entries = (char **) realloc(entries, alloc * sizeof(char *))) == NULL) {
if (++count == alloc) {
alloc += LINK_MAX;
if ((entries = (char **) realloc(entries, alloc * sizeof(char *))) == NULL) {