From c3b3743d222e9e72dac1eb041a26a8c0cabf82d7 Mon Sep 17 00:00:00 2001 From: sof Date: Wed, 22 Apr 1998 00:08:32 +0000 Subject: [PATCH] [project @ 1998-04-22 00:08:32 by sof] NUL terminate each dir entry name. --- ghc/lib/std/cbits/getDirectoryContents.lc | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ghc/lib/std/cbits/getDirectoryContents.lc b/ghc/lib/std/cbits/getDirectoryContents.lc index 025aae9..6f66b45 100644 --- a/ghc/lib/std/cbits/getDirectoryContents.lc +++ b/ghc/lib/std/cbits/getDirectoryContents.lc @@ -42,7 +42,6 @@ freeEntries(char **entries, int count) * operations, we collect the entire list in one atomic operation, * rather than reading the directory lazily. */ - StgAddr getDirectoryContents(path) StgByteArray path; @@ -51,7 +50,7 @@ StgByteArray path; DIR *dir; struct dirent *d; char **entries; - int alloc, count; + int alloc, count, len; /* Check for an actual directory */ while (stat(path, &sb) != 0) { @@ -100,7 +99,8 @@ StgByteArray path; } errno = 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); @@ -108,6 +108,8 @@ StgByteArray path; 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) { -- 1.7.10.4