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.
286 lines
7.6 KiB
C
286 lines
7.6 KiB
C
/*
|
|
* splinter -- model markdown as Telegram HTML.
|
|
*
|
|
* The chat used to show "**fett**", "|---|" and "<b>" as characters. These
|
|
* cases pin what has to become a tag, what has to stay text (snake_case, a bare
|
|
* '<'), and how a long answer is split so that every piece parses on its own.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "splinter.h"
|
|
#include "tgmd.h"
|
|
|
|
static int failures;
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
|
failures++; \
|
|
} \
|
|
} while (0)
|
|
|
|
static void
|
|
check_render(const char *md, const char *want)
|
|
{
|
|
struct sp_buf b;
|
|
|
|
if (sp_buf_init(&b) != 0) {
|
|
fprintf(stderr, "FAIL init\n");
|
|
failures++;
|
|
return;
|
|
}
|
|
if (sp_tgmd_render(md, &b) != 0) {
|
|
fprintf(stderr, "FAIL render of:\n%s\n", md);
|
|
failures++;
|
|
} else if (b.p == NULL || strcmp(b.p, want) != 0) {
|
|
fprintf(stderr, "FAIL %s:%d:\n--- in ---\n%s--- want ---\n%s"
|
|
"--- got ---\n%s\n", __FILE__, __LINE__, md, want,
|
|
b.p != NULL ? b.p : "(null)");
|
|
failures++;
|
|
}
|
|
sp_buf_free(&b);
|
|
}
|
|
|
|
/* tag names opened and closed in 's', in order: 1 when every tag balances */
|
|
static int
|
|
balanced(const char *s)
|
|
{
|
|
char stack[32][16];
|
|
size_t depth = 0;
|
|
size_t i;
|
|
|
|
for (i = 0; s[i] != '\0'; i++) {
|
|
char name[16];
|
|
size_t n = 0, j;
|
|
|
|
if (s[i] != '<')
|
|
continue;
|
|
j = i + 1;
|
|
if (s[j] == '/') {
|
|
j++;
|
|
while (s[j] != '\0' && s[j] != '>')
|
|
name[n++] = s[j++];
|
|
name[n] = '\0';
|
|
if (depth == 0 || strcmp(stack[depth - 1], name) != 0)
|
|
return 0;
|
|
depth--;
|
|
i = j;
|
|
continue;
|
|
}
|
|
while (s[j] != '\0' && s[j] != '>' && s[j] != ' ')
|
|
name[n++] = s[j++];
|
|
name[n] = '\0';
|
|
if (n == 0)
|
|
continue;
|
|
if (depth == sizeof(stack) / sizeof(stack[0]))
|
|
return 0;
|
|
snprintf(stack[depth++], sizeof(stack[0]), "%s", name);
|
|
i = j;
|
|
}
|
|
return depth == 0;
|
|
}
|
|
|
|
/* the text between tags, with the entities the renderer writes undone */
|
|
static int
|
|
plain_into(struct sp_buf *out, const char *s)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; s[i] != '\0'; i++) {
|
|
if (s[i] == '<') {
|
|
const char *gt = strchr(s + i, '>');
|
|
|
|
if (gt == NULL)
|
|
return -1;
|
|
i = (size_t)(gt - s);
|
|
continue;
|
|
}
|
|
if (strncmp(s + i, "&", 5) == 0) {
|
|
if (sp_buf_puts(out, "&") != 0)
|
|
return -1;
|
|
i += 4;
|
|
} else if (strncmp(s + i, "<", 4) == 0) {
|
|
if (sp_buf_puts(out, "<") != 0)
|
|
return -1;
|
|
i += 3;
|
|
} else if (strncmp(s + i, ">", 4) == 0) {
|
|
if (sp_buf_puts(out, ">") != 0)
|
|
return -1;
|
|
i += 3;
|
|
} else if (sp_buf_add(out, s + i, 1) != 0) {
|
|
return -1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
check_chunks(void)
|
|
{
|
|
struct sp_buf md, html, piece, all, back;
|
|
struct sp_tgmd_cursor cur;
|
|
size_t i;
|
|
int n, pieces = 0;
|
|
|
|
CHECK(sp_buf_init(&md) == 0);
|
|
CHECK(sp_buf_init(&html) == 0);
|
|
CHECK(sp_buf_init(&piece) == 0);
|
|
CHECK(sp_buf_init(&all) == 0);
|
|
CHECK(sp_buf_init(&back) == 0);
|
|
for (i = 0; i < 120; i++)
|
|
CHECK(sp_buf_printf(&md,
|
|
"**Zeile %zu** mit `code` und einem <tag> & mehr Text, "
|
|
"damit die Antwort lang genug fuer mehrere Nachrichten ist.\n",
|
|
i) == 0);
|
|
CHECK(sp_tgmd_render(md.p, &html) == 0);
|
|
CHECK(sp_tgmd_cursor_init(&cur, html.p) == 0);
|
|
while ((n = sp_tgmd_next(&cur, 1000, &piece)) > 0) {
|
|
pieces++;
|
|
CHECK(piece.len > 0);
|
|
/* a piece is what Telegram gets: it has to fit and to parse */
|
|
CHECK(piece.len < 4096);
|
|
CHECK(balanced(piece.p));
|
|
CHECK(plain_into(&all, piece.p) == 0);
|
|
}
|
|
CHECK(n == 0);
|
|
CHECK(pieces > 1);
|
|
CHECK(plain_into(&back, html.p) == 0);
|
|
CHECK(all.p != NULL && back.p != NULL && strcmp(all.p, back.p) == 0);
|
|
sp_tgmd_cursor_free(&cur);
|
|
sp_buf_free(&md);
|
|
sp_buf_free(&html);
|
|
sp_buf_free(&piece);
|
|
sp_buf_free(&all);
|
|
sp_buf_free(&back);
|
|
}
|
|
|
|
/* A link whose tag alone is longer than a piece once looped forever. */
|
|
static void
|
|
check_huge_link(void)
|
|
{
|
|
struct sp_buf md, html, piece;
|
|
struct sp_tgmd_cursor cur;
|
|
size_t i;
|
|
int n, pieces = 0;
|
|
|
|
CHECK(sp_buf_init(&md) == 0);
|
|
CHECK(sp_buf_init(&html) == 0);
|
|
CHECK(sp_buf_init(&piece) == 0);
|
|
CHECK(sp_buf_puts(&md, "vorher **[x](https://example.org/") == 0);
|
|
for (i = 0; i < 4000; i++)
|
|
CHECK(sp_buf_puts(&md, "a") == 0);
|
|
CHECK(sp_buf_puts(&md, ")** nachher") == 0);
|
|
CHECK(sp_tgmd_render(md.p, &html) == 0);
|
|
CHECK(sp_tgmd_cursor_init(&cur, html.p) == 0);
|
|
while ((n = sp_tgmd_next(&cur, 1000, &piece)) > 0 && pieces < 100) {
|
|
pieces++;
|
|
CHECK(piece.len < 4096);
|
|
CHECK(balanced(piece.p));
|
|
}
|
|
CHECK(n == 0);
|
|
CHECK(pieces < 100);
|
|
sp_tgmd_cursor_free(&cur);
|
|
sp_buf_free(&md);
|
|
sp_buf_free(&html);
|
|
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>", "&", "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)
|
|
{
|
|
/* inline: the shapes a model actually writes */
|
|
check_render("**fett**", "<b>fett</b>");
|
|
check_render("__fett__", "<b>fett</b>");
|
|
check_render("*kursiv*", "<i>kursiv</i>");
|
|
check_render("_kursiv_", "<i>kursiv</i>");
|
|
check_render("~~weg~~", "<s>weg</s>");
|
|
check_render("ein `code` hier", "ein <code>code</code> hier");
|
|
check_render("**fett mit *kursiv***",
|
|
"<b>fett mit <i>kursiv</i></b>");
|
|
check_render("[Titel](https://example.org/a?x=1&y=2)",
|
|
"<a href=\"https://example.org/a?x=1&y=2\">Titel</a>");
|
|
check_render("<https://example.org/x>",
|
|
"<a href=\"https://example.org/x\">https://example.org/x</a>");
|
|
check_render("",
|
|
"<a href=\"https://example.org/b.png\">Bild</a>");
|
|
|
|
/* what must stay text */
|
|
check_render("snake_case_name", "snake_case_name");
|
|
check_render("2 * 3 * 4 = 24", "2 * 3 * 4 = 24");
|
|
check_render("a < b & c > d", "a < b & c > d");
|
|
check_render("<script>x</script>", "<script>x</script>");
|
|
check_render("[kein link](javascript:alert(1))",
|
|
"[kein link](javascript:alert(1))");
|
|
check_render("* nicht fett", "\xe2\x80\xa2 nicht fett");
|
|
|
|
/* blocks */
|
|
check_render("## Titel\nText\n", "<b>Titel</b>\nText\n");
|
|
check_render("> zitat\n> mehr\n",
|
|
"<blockquote>zitat\nmehr\n</blockquote>\n");
|
|
check_render("---\n", "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\n");
|
|
check_render("- eins\n- zwei\n",
|
|
"\xe2\x80\xa2 eins\n\xe2\x80\xa2 zwei\n");
|
|
check_render("```c\nif (a < b)\n\treturn **x**;\n```\n",
|
|
"<pre><code class=\"language-c\">if (a < b)\n\treturn **x**;"
|
|
"</code></pre>\n");
|
|
check_render("```\nplain & simple\n```\n",
|
|
"<pre><code>plain & simple</code></pre>\n");
|
|
|
|
/* a table: columns in <pre>, not a row of pipes in a proportional font */
|
|
check_render("| a | b |\n|---|---|\n| 1 | 22 |\n",
|
|
"<pre>| a | b |\n|---|----|\n| 1 | 22 |\n</pre>\n");
|
|
|
|
check_chunks();
|
|
check_huge_link();
|
|
check_no_stall();
|
|
|
|
if (failures == 0)
|
|
printf("test_tgmd: ok\n");
|
|
return failures == 0 ? 0 : 1;
|
|
}
|