timprove XML entity conversion - webdump - [FORK] git://git.codemadness.org/webdump
 (HTM) git clone git://git.z3bra.org/webdump.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit bc1669d7927bb347612b5fbd74754207266654d2
 (DIR) parent 1c95e7d86a0dc62670a87f755b3507ceab912ec1
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Wed, 11 Mar 2020 15:46:26 +0100
       
       improve XML entity conversion
       
       - return -1 for invalid XML entities.
       - separate between NUL (&#0;) and invalid entities: although both are
         unwanted in sfeed.
       - validate the number range more strictly and don't wrap to unsigned.
         entities lik: "&#-1;" are handled as invalid now. "&#;" is also invalid
         instead of the same as "&#0;".
       
       Diffstat:
         M xml.c                               |      16 ++++++++--------
       
       1 file changed, 8 insertions(+), 8 deletions(-)
       ---
 (DIR) diff --git a/xml.c b/xml.c
       t@@ -278,7 +278,7 @@ namedentitytostr(const char *e, char *buf, size_t bufsiz)
                        buf[i] = '\0';
                        return i;
                }
       -        return 0;
       +        return -1;
        }
        
        static int
       t@@ -295,12 +295,12 @@ numericentitytostr(const char *e, char *buf, size_t bufsiz)
                errno = 0;
                /* hex (16) or decimal (10) */
                if (*e == 'x')
       -                l = strtoul(e + 1, &end, 16);
       +                l = strtol(++e, &end, 16);
                else
       -                l = strtoul(e, &end, 10);
       -        /* invalid value or not a well-formed entity or too high codepoint */
       -        if (errno || *end != ';' || l > 0x10FFFF)
       -                return 0;
       +                l = strtol(e, &end, 10);
       +        /* invalid value or not a well-formed entity or invalid codepoint */
       +        if (errno || e == end || *end != ';' || l < 0 || l > 0x10ffff)
       +                return -1;
                len = codepointtoutf8(l, buf);
                buf[len] = '\0';
        
       t@@ -308,13 +308,13 @@ numericentitytostr(const char *e, char *buf, size_t bufsiz)
        }
        
        /* convert named- or numeric entity string to buffer string
       - * returns byte-length of string. */
       + * returns byte-length of string or -1 on failure. */
        int
        xml_entitytostr(const char *e, char *buf, size_t bufsiz)
        {
                /* doesn't start with & */
                if (e[0] != '&')
       -                return 0;
       +                return -1;
                /* numeric entity */
                if (e[1] == '#')
                        return numericentitytostr(e + 2, buf, bufsiz);