Safer directory listing HTML generation

Also reallocate memory while generating the directory list.
This commit is contained in:
Thomas Kolb 2013-01-17 22:23:00 +01:00
parent 6cd65091ff
commit 2bb32b1cf9

View file

@ -60,7 +60,7 @@ char* gen_html(const char *url, struct Entry *entries, size_t numentries) {
char fullpath[PATH_MAX]; char fullpath[PATH_MAX];
// allocate a buffer for the result string // 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)); LOG(LVL_DEBUG, "initially allocating result string - size: %lu bytes", allocated * sizeof(char));
result = malloc(allocated * sizeof(char)); result = malloc(allocated * sizeof(char));
result[0] = '\0'; 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)) if(!(entries[i].flags & FLG_ISDIR))
continue; 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) { if(isrootdir) {
sprintf(buf, "<li><a href=\"/%s/\">%s/</a></li>\n", sprintf(buf, "<li><a href=\"/%s/\">%s/</a></li>\n",
entries[i].name, entries[i].name); entries[i].name, entries[i].name);
@ -137,16 +152,16 @@ char* gen_html(const char *url, struct Entry *entries, size_t numentries) {
// here the listing entries are generated // here the listing entries are generated
for(i = 0; i < numentries; i++) { for(i = 0; i < numentries; i++) {
size_t entrylen = 32 + 2 * strlen(entries[i].name);
size_t resultlen = strlen(result);
// skip all directories // skip all directories
if(entries[i].flags & FLG_ISDIR) if(entries[i].flags & FLG_ISDIR)
continue; continue;
size_t entrylen = 64 + 2 * strlen(entries[i].name);
size_t resultlen = strlen(result);
if(allocated < resultlen + entrylen) { if(allocated < resultlen + entrylen) {
allocated *= 2; allocated *= 2;
LOG(LVL_DEBUG, "reallocating result string - new size: %lu bytes", allocated * sizeof(char)); LOG(LVL_DEBUG, "reallocating result string (files) - new size: %lu bytes", allocated * sizeof(char));
char *tmpresult = realloc(result, allocated * sizeof(char)); char *tmpresult = realloc(result, allocated * sizeof(char));
if(!tmpresult) { if(!tmpresult) {
LOG(LVL_ERR, "realloc failed for result string: %s", strerror(errno)); LOG(LVL_ERR, "realloc failed for result string: %s", strerror(errno));