diff --git a/dirlisting.c b/dirlisting.c index 7c16a65..32c3ec0 100644 --- a/dirlisting.c +++ b/dirlisting.c @@ -60,7 +60,7 @@ char* gen_html(const char *url, struct Entry *entries, size_t numentries) { char fullpath[PATH_MAX]; // allocate a buffer for the result string - allocated = 64 * numentries + strlen(HEADER1 HEADER2 FOOTER) + 4096; + allocated = strlen(HEADER1 HEADER2 FOOTER) + 64 * numentries + 4096; LOG(LVL_DEBUG, "initially allocating result string - size: %lu bytes", allocated * sizeof(char)); result = malloc(allocated * sizeof(char)); result[0] = '\0'; @@ -119,6 +119,21 @@ char* gen_html(const char *url, struct Entry *entries, size_t numentries) { if(!(entries[i].flags & FLG_ISDIR)) continue; + size_t entrylen = 32 + 2 * strlen(entries[i].name); + size_t resultlen = strlen(result); + + if(allocated < resultlen + entrylen) { + allocated *= 2; + LOG(LVL_DEBUG, "reallocating result string (subdirs) - new size: %lu bytes", allocated * sizeof(char)); + char *tmpresult = realloc(result, allocated * sizeof(char)); + if(!tmpresult) { + LOG(LVL_ERR, "realloc failed for result string: %s", strerror(errno)); + break; + } + + result = tmpresult; + } + if(isrootdir) { sprintf(buf, "