tgmd: an "&" with no ";" behind it is text, not the head of an entity

sp_tgmd_next() could return 1 with a piece of zero bytes and the cursor
where it was. The sender runs `while (sp_tgmd_next(...) > 0)`, so that
piece was an endless loop: telegram never sent the answer, never returned
and the turn hung.

The cause is safe_cut(): it walked back from the cut, saw a '&' and moved
the cut in front of it, assuming an entity. An "&" that no ";" follows (a
lone ampersand at a piece boundary) therefore cut at offset 0 every time.

entity_at() now only treats "&" as the head of an entity when a ";" closes
it within the 10 bytes an entity can take, and a cut that would come out
empty sends the entity whole instead of nothing at all.

Test: tests/test_tgmd.c check_no_stall() feeds 13 lone-ampersand shapes at
three limits and asserts each piece either grows the cursor or carries
bytes. Red before the fix (the guard runs out, cur.pos != cur.len), green
after. Fuzz: 20000 random inputs through render+chunk, 0 stalls; ASan/UBSan
clean; valgrind clean on test_tgmd.
This commit is contained in:
lotherk committed 2026-11-23 10:23:11 +00:00
1 parent 1b5b4fab27
commit 4b4d822188
2 files changed
+93 -5

No files matched your search

+50 -5
View File
@@ -733,9 +733,42 @@ open_cost(const struct sp_tgmd_cursor *c)
return n;
}
/*
* Is the '&' at 'p' the start of an entity - is there a ';' close behind it?
* An "&" that no ";" follows is a lone ampersand, and treating it as the head
* of an entity is what made the chunker hand back empty pieces.
*/
static int
entity_at(const char *s, size_t len, size_t p)
{
size_t i;
for (i = p + 1; i < len && i <= p + 9; i++) {
if (s[i] == ';')
return 1;
if (s[i] == '&' || s[i] == '<' || s[i] == ' ' || s[i] == '\n')
return 0;
}
return 0;
}
/* The whole entity at 'p', or 0 when there is none. */
static size_t
entity_len(const char *s, size_t len, size_t p)
{
size_t i;
if (!entity_at(s, len, p))
return 0;
for (i = p + 1; i < len; i++)
if (s[i] == ';')
return i + 1 - p;
return 0;
}
/* Never cut a UTF-8 character or an entity in half: both become mojibake. */
static size_t
safe_cut(const char *s, size_t start, size_t cut)
safe_cut(const char *s, size_t len, size_t start, size_t cut)
{
size_t j;
@@ -743,7 +776,8 @@ safe_cut(const char *s, size_t start, size_t cut)
cut--;
for (j = cut; j > start && cut - j < 10; j--) {
if (s[j - 1] == '&') {
cut = j - 1;
if (entity_at(s, len, j - 1))
cut = j - 1;
break;
}
if (s[j - 1] == ';' || s[j - 1] == ' ' || s[j - 1] == '\n')
@@ -814,11 +848,22 @@ sp_tgmd_next(struct sp_tgmd_cursor *c, size_t limit, struct sp_buf *out)
{
size_t avail = limit - out->len;
size_t take = run < avail ? run : avail;
size_t stop = safe_cut(c->html, c->pos, c->pos + take);
size_t stop = safe_cut(c->html, c->len, c->pos,
c->pos + take);
take = stop - c->pos;
if (take > 0 && sp_buf_add(out, c->html + c->pos,
take) != 0)
if (take == 0) {
/*
* An entity that starts exactly at the cut and
* runs past it: send it whole. A piece a few
* bytes over budget is a piece; nothing at all
* is the loop in the sender never ending.
*/
take = entity_len(c->html, c->len, c->pos);
if (take == 0)
take = 1;
}
if (sp_buf_add(out, c->html + c->pos, take) != 0)
return -1;
c->pos += take;
if (take < run)
+43
View File
@@ -186,6 +186,48 @@ check_huge_link(void)
sp_buf_free(&piece);
}
/*
* The contract: a piece is either text or the end. Handing back 1 with
* nothing in it and the cursor where it was makes the sender's
* `while (sp_tgmd_next(...) > 0)` spin for ever - an entity that starts at a
* piece boundary ("&" with no ";" behind it) did exactly that.
*/
static void
check_no_stall(void)
{
static const char *const in[] = {
"&", "&a", "&aaaa", "&<b>", "&amp", "x&", "<b>&</b>",
"&;", "&x", "&#", "&#x", "text &", "& text"
};
size_t i;
for (i = 0; i < sizeof(in) / sizeof(in[0]); i++) {
struct sp_tgmd_cursor cur;
struct sp_buf piece;
size_t limit;
int guard;
for (limit = 256; limit <= 300; limit += 44) {
CHECK(sp_buf_init(&piece) == 0);
CHECK(sp_tgmd_cursor_init(&cur, in[i]) == 0);
for (guard = 0; guard < 200; guard++) {
size_t before = cur.pos;
int r;
sp_buf_clear(&piece);
r = sp_tgmd_next(&cur, limit, &piece);
if (r != 1)
break;
CHECK(cur.pos > before || piece.len > 0);
}
CHECK(guard < 200);
CHECK(cur.pos == cur.len);
sp_tgmd_cursor_free(&cur);
sp_buf_free(&piece);
}
}
}
int
main(void)
{
@@ -235,6 +277,7 @@ main(void)
check_chunks();
check_huge_link();
check_no_stall();
if (failures == 0)
printf("test_tgmd: ok\n");