Skip to content

Commit

Permalink
Cleaner approach to max digits for numeric entities.
Browse files Browse the repository at this point in the history
This modifies unescaping in houdini_html_u.c rather than
the entity handling in inlines.c.  Unlike the other,
this approach works also in e.g. link titles.
  • Loading branch information
jgm committed Nov 11, 2019
1 parent cb1cd88 commit 7b35d4b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/houdini_html_u.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ bufsize_t houdini_unescape_ent(cmark_strbuf *ob, const uint8_t *src,
if (size >= 3 && src[0] == '#') {
int codepoint = 0;
int num_digits = 0;
int max_digits = 7;

if (_isdigit(src[1])) {
for (i = 1; i < size && _isdigit(src[i]); ++i) {
Expand All @@ -55,6 +56,7 @@ bufsize_t houdini_unescape_ent(cmark_strbuf *ob, const uint8_t *src,
}

num_digits = i - 1;
max_digits = 7;
}

else if (src[1] == 'x' || src[1] == 'X') {
Expand All @@ -69,9 +71,11 @@ bufsize_t houdini_unescape_ent(cmark_strbuf *ob, const uint8_t *src,
}

num_digits = i - 2;
max_digits = 6;
}

if (num_digits >= 1 && num_digits <= 8 && i < size && src[i] == ';') {
if (num_digits >= 1 && num_digits <= max_digits &&
i < size && src[i] == ';') {
if (codepoint == 0 || (codepoint >= 0xD800 && codepoint < 0xE000) ||
codepoint >= 0x110000) {
codepoint = 0xFFFD;
Expand Down
7 changes: 1 addition & 6 deletions src/inlines.c
Original file line number Diff line number Diff line change
Expand Up @@ -784,18 +784,13 @@ static cmark_node *handle_backslash(subject *subj) {
static cmark_node *handle_entity(subject *subj) {
cmark_strbuf ent = CMARK_BUF_INIT(subj->mem);
bufsize_t len;
int length_limit = 256;

advance(subj);

len = houdini_unescape_ent(&ent, subj->input.data + subj->pos,
subj->input.len - subj->pos);

if (peek_char(subj) == '#') {
length_limit = 9; // includes #, optional x for hex, and ;
}

if (len <= 0 || len > length_limit)
if (len <= 0)
return make_str(subj, subj->pos - 1, subj->pos - 1, cmark_chunk_literal("&"));

subj->pos += len;
Expand Down

0 comments on commit 7b35d4b

Please sign in to comment.