sync xml.c changes: parse numeric entities more strictly - webdump - HTML to plain-text converter for webpages
(HTM) git clone git://git.codemadness.org/webdump
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit bc810c876a5d5de1e71796e5579b0c966ca092fd
(DIR) parent 31fac2476f06b72f3d8bc7ac654cfde4e8452525
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Thu, 11 Dec 2025 20:52:22 +0100
sync xml.c changes: parse numeric entities more strictly
Diffstat:
M xml.c | 35 ++++++++++++++++++++++++-------
M xml.h | 2 +-
2 files changed, 28 insertions(+), 9 deletions(-)
---
(DIR) diff --git a/xml.c b/xml.c
@@ -9,7 +9,9 @@
#define HTML_MODE
#define ISALPHA(c) ((((unsigned)c) | 32) - 'a' < 26)
+#define ISDIGIT(c) (((unsigned)c) - '0' < 10)
#define ISSPACE(c) ((c) == ' ' || ((((unsigned)c) - '\t') < 5))
+#define ISXDIGIT(c) ((((unsigned)c) - '0' < 10) || (((unsigned)c) | 32) - 'a' < 6)
static void
xml_parseattrs(XMLParser *x)
@@ -288,22 +290,39 @@ static int
numericentitytostr(const char *e, char *buf, size_t bufsiz)
{
long l;
- int len;
+ int base, len;
+ const char *s;
char *end;
/* buffer is too small */
if (bufsiz < 5)
return -1;
+ /* hex (base 16) or decimal (base 10) */
+ if (*e == 'x') {
+ e++;
+ for (s = e; *s && *s != ';'; s++) {
+ if (!ISXDIGIT((unsigned char)*s))
+ return -1; /* invalid: no hex */
+ }
+ base = 16;
+
+ } else {
+ for (s = e; *s && *s != ';'; s++) {
+ if (!ISDIGIT((unsigned char)*s))
+ return -1; /* invalid: no digits */
+ }
+ base = 10;
+ }
+ if (*s != ';' || *(s + 1) != '\0')
+ return -1; /* must end with ';' NUL */
+
errno = 0;
- /* hex (16) or decimal (10) */
- if (*e == 'x')
- l = strtol(++e, &end, 16);
- else
- l = strtol(e, &end, 10);
+ l = strtol(e, &end, base);
+
/* invalid value or not a well-formed entity or invalid code point */
if (errno || e == end || *end != ';' || l < 0 || l > 0x10ffff ||
- (l >= 0xd800 && l <= 0xdfff))
+ (l >= 0xd800 && l <= 0xdfff)) /* surrogate range */
return -1;
len = codepointtoutf8(l, buf);
buf[len] = '\0';
@@ -422,7 +441,7 @@ read_data:
if (x->xmldatastart)
x->xmldatastart(x);
while ((c = GETNEXT()) != EOF) {
- if (c == '&') {
+ if (c == '&') { /* entities */
if (datalen) {
x->data[datalen] = '\0';
if (x->xmldata)
(DIR) diff --git a/xml.h b/xml.h
@@ -36,7 +36,7 @@ typedef struct xmlparser {
/* current tag */
char tag[1024];
size_t taglen;
- /* current tag is in short tag ? <tag /> */
+ /* current tag is a short tag ? <tag /> */
int isshorttag;
/* current attribute name */
char name[1024];