Skip to content

Commit 6fb16b8

Browse files
committed
Update file_basename implementation to handle really long filenames (Issue #532)
1 parent 683bec5 commit 6fb16b8

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Changes in HTMLDOC v1.9.19
22

3+
- Security: Fixed an issue with the `file_basename` implementation (Issue #532)
34
- Updated HTML and header/footer code to use a string pool to simplify memory
45
management and fix potential double-free bugs.
56
- Updated configure script to look for zlib with pkg-config (Issue #519)

htmldoc/file.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Filename routines for HTMLDOC, a HTML document processing program.
33
*
4-
* Copyright © 2011-2023 by Michael R Sweet.
4+
* Copyright © 2011-2024 by Michael R Sweet.
55
* Copyright © 1997-2010 by Easy Software Products. All rights reserved.
66
*
77
* This program is free software. Distribution and use rights are outlined in
@@ -89,23 +89,23 @@ file_basename(const char *s) /* I - Filename or URL */
8989
if (s == NULL)
9090
return (NULL);
9191

92-
if ((basename = strrchr(s, '/')) != NULL)
93-
basename ++;
94-
else if ((basename = strrchr(s, '\\')) != NULL)
95-
basename ++;
96-
else
97-
basename = (char *)s;
98-
99-
if (basename[0] == '#')
100-
return (NULL);
92+
if (strchr(s, '#') != NULL)
93+
{
94+
char *bufptr; // Pointer into buffer
10195

102-
if (strchr(basename, '#') == NULL)
103-
return (basename);
96+
strlcpy(buf, s, sizeof(buf));
97+
s = buf;
10498

105-
strlcpy(buf, basename, sizeof(buf));
106-
*(char *)strchr(buf, '#') = '\0';
99+
if ((bufptr = strchr(buf, '#')) != NULL)
100+
*bufptr = '\0';
101+
}
107102

108-
return (buf);
103+
if ((basename = strrchr(s, '/')) != NULL)
104+
return (basename + 1);
105+
else if ((basename = strrchr(s, '\\')) != NULL)
106+
return (basename + 1);
107+
else
108+
return (s);
109109
}
110110

111111

0 commit comments

Comments
 (0)