Skip to content

Commit d3e80e0

Browse files
Don't adjust start, remaining until the end of the loop.
1 parent 0b4a7fe commit d3e80e0

File tree

1 file changed

+18
-30
lines changed

1 file changed

+18
-30
lines changed

extensions/autolink.c

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -323,19 +323,14 @@ static void postprocess_text(cmark_parser *parser, cmark_node *text) {
323323
if (offset >= remaining)
324324
break;
325325

326-
start += offset;
327-
remaining -= offset;
328-
329-
at = (uint8_t *)memchr(data + start, '@', remaining);
326+
at = (uint8_t *)memchr(data + start + offset, '@', remaining - offset);
330327
if (!at)
331328
break;
332329

333-
max_rewind = at - (data + start);
334-
start += max_rewind;
335-
remaining -= max_rewind;
330+
max_rewind = at - (data + start + offset);
336331

337332
for (rewind = 0; rewind < max_rewind; ++rewind) {
338-
uint8_t c = data[start - rewind - 1];
333+
uint8_t c = data[start + offset + max_rewind - rewind - 1];
339334

340335
if (cmark_isalnum(c))
341336
continue;
@@ -344,12 +339,12 @@ static void postprocess_text(cmark_parser *parser, cmark_node *text) {
344339
continue;
345340

346341
if (strchr(":", c) != NULL) {
347-
if (validate_protocol("mailto:", data + start, rewind, max_rewind)) {
342+
if (validate_protocol("mailto:", data + start + offset + max_rewind, rewind, max_rewind)) {
348343
auto_mailto = false;
349344
continue;
350345
}
351346

352-
if (validate_protocol("xmpp:", data + start, rewind, max_rewind)) {
347+
if (validate_protocol("xmpp:", data + start + offset + max_rewind, rewind, max_rewind)) {
353348
auto_mailto = false;
354349
is_xmpp = true;
355350
continue;
@@ -360,23 +355,21 @@ static void postprocess_text(cmark_parser *parser, cmark_node *text) {
360355
}
361356

362357
if (rewind == 0) {
363-
// Reset start, remaining back to their values at the start of the loop.
364-
start -= offset + max_rewind;
365-
remaining += offset + max_rewind;
366358
offset += max_rewind + 1;
367359
depth++;
368360
continue;
369361
}
370362

371-
for (link_end = 0; link_end < remaining; ++link_end) {
372-
uint8_t c = data[start + link_end];
363+
for (link_end = 0; link_end < remaining - offset - max_rewind; ++link_end) {
364+
uint8_t c = data[start + offset + max_rewind + link_end];
373365

374366
if (cmark_isalnum(c))
375367
continue;
376368

377369
if (c == '@')
378370
nb++;
379-
else if (c == '.' && link_end < remaining - 1 && cmark_isalnum(data[start + link_end + 1]))
371+
else if (c == '.' && link_end < remaining - offset - max_rewind - 1 &&
372+
cmark_isalnum(data[start + offset + max_rewind + link_end + 1]))
380373
np++;
381374
else if (c == '/' && is_xmpp)
382375
continue;
@@ -385,21 +378,16 @@ static void postprocess_text(cmark_parser *parser, cmark_node *text) {
385378
}
386379

387380
if (link_end < 2 || nb != 1 || np == 0 ||
388-
(!cmark_isalpha(data[start + link_end - 1]) && data[start + link_end - 1] != '.')) {
389-
// Reset start, remaining back to their values at the start of the loop.
390-
start -= offset + max_rewind;
391-
remaining += offset + max_rewind;
381+
(!cmark_isalpha(data[start + offset + max_rewind + link_end - 1]) &&
382+
data[start + offset + max_rewind + link_end - 1] != '.')) {
392383
offset += max_rewind + 1;
393384
depth++;
394385
continue;
395386
}
396387

397-
link_end = autolink_delim(data + start, link_end);
388+
link_end = autolink_delim(data + start + offset + max_rewind, link_end);
398389

399390
if (link_end == 0) {
400-
// Reset start, remaining back to their values at the start of the loop.
401-
start -= offset + max_rewind;
402-
remaining += offset + max_rewind;
403391
offset += max_rewind + 1;
404392
depth++;
405393
continue;
@@ -410,7 +398,7 @@ static void postprocess_text(cmark_parser *parser, cmark_node *text) {
410398
cmark_strbuf_init(parser->mem, &buf, 10);
411399
if (auto_mailto)
412400
cmark_strbuf_puts(&buf, "mailto:");
413-
cmark_strbuf_put(&buf, data + start - rewind, (bufsize_t)(link_end + rewind));
401+
cmark_strbuf_put(&buf, data + start + offset + max_rewind - rewind, (bufsize_t)(link_end + rewind));
414402
link_node->as.link.url = cmark_chunk_buf_detach(&buf);
415403

416404
cmark_node *link_text = cmark_node_new_with_mem(CMARK_NODE_TEXT, parser->mem);
@@ -426,17 +414,17 @@ static void postprocess_text(cmark_parser *parser, cmark_node *text) {
426414

427415
cmark_node *post = cmark_node_new_with_mem(CMARK_NODE_TEXT, parser->mem);
428416
post->as.literal = cmark_chunk_dup(&detached_chunk,
429-
(bufsize_t)(start + link_end),
430-
(bufsize_t)(remaining - link_end));
417+
(bufsize_t)(start + offset + max_rewind + link_end),
418+
(bufsize_t)(remaining - offset - max_rewind - link_end));
431419

432420
cmark_node_insert_after(link_node, post);
433421

434-
text->as.literal = cmark_chunk_dup(&detached_chunk, start - offset - max_rewind, offset + max_rewind - rewind);
422+
text->as.literal = cmark_chunk_dup(&detached_chunk, start, offset + max_rewind - rewind);
435423
cmark_chunk_to_cstr(parser->mem, &text->as.literal);
436424

437425
text = post;
438-
start += link_end;
439-
remaining -= link_end;
426+
start += offset + max_rewind + link_end;
427+
remaining -= offset + max_rewind + link_end;
440428
offset = 0;
441429
depth++;
442430
}

0 commit comments

Comments
 (0)