|
1 | 1 | #include <stdbool.h> |
| 2 | +#include <stdio.h> |
2 | 3 | #include <stdlib.h> |
3 | 4 | #include <string.h> |
4 | 5 |
|
@@ -264,24 +265,34 @@ cmark_node *cmark_node_last_child(cmark_node *node) { |
264 | 265 | } |
265 | 266 | } |
266 | 267 |
|
267 | | -static bufsize_t cmark_set_cstr(cmark_mem *mem, unsigned char **dst, |
268 | | - const char *src) { |
| 268 | +static int cmark_set_cstr(cmark_mem *mem, unsigned char **dst, |
| 269 | + bufsize_t *len, const char *src) { |
269 | 270 | unsigned char *old = *dst; |
270 | | - bufsize_t len; |
| 271 | + unsigned char *new_str = NULL; |
| 272 | + bufsize_t new_len = 0; |
271 | 273 |
|
272 | 274 | if (src && src[0]) { |
273 | | - len = (bufsize_t)strlen(src); |
274 | | - *dst = (unsigned char *)mem->realloc(NULL, len + 1); |
275 | | - memcpy(*dst, src, len + 1); |
| 275 | + size_t srclen = strlen(src); |
| 276 | + if (srclen > (size_t)BUFSIZE_MAX - 1) { |
| 277 | + return 0; |
| 278 | + } |
| 279 | + new_len = (bufsize_t)srclen; |
| 280 | + new_str = (unsigned char *)mem->realloc(NULL, (size_t)new_len + 1); |
| 281 | + memcpy(new_str, src, (size_t)new_len + 1); |
276 | 282 | } else { |
277 | | - len = 0; |
278 | | - *dst = NULL; |
| 283 | + new_len = 0; |
| 284 | + new_str = NULL; |
| 285 | + } |
| 286 | + |
| 287 | + *dst = new_str; |
| 288 | + if (len) { |
| 289 | + *len = new_len; |
279 | 290 | } |
280 | 291 | if (old) { |
281 | 292 | mem->free(old); |
282 | 293 | } |
283 | 294 |
|
284 | | - return len; |
| 295 | + return 1; |
285 | 296 | } |
286 | 297 |
|
287 | 298 | void *cmark_node_get_user_data(cmark_node *node) { |
@@ -331,8 +342,7 @@ int cmark_node_set_literal(cmark_node *node, const char *content) { |
331 | 342 | case CMARK_NODE_HTML_INLINE: |
332 | 343 | case CMARK_NODE_CODE: |
333 | 344 | case CMARK_NODE_CODE_BLOCK: |
334 | | - node->len = cmark_set_cstr(node->mem, &node->data, content); |
335 | | - return 1; |
| 345 | + return cmark_set_cstr(node->mem, &node->data, &node->len, content); |
336 | 346 |
|
337 | 347 | default: |
338 | 348 | break; |
@@ -500,8 +510,7 @@ int cmark_node_set_fence_info(cmark_node *node, const char *info) { |
500 | 510 | } |
501 | 511 |
|
502 | 512 | if (node->type == CMARK_NODE_CODE_BLOCK) { |
503 | | - cmark_set_cstr(node->mem, &node->as.code.info, info); |
504 | | - return 1; |
| 513 | + return cmark_set_cstr(node->mem, &node->as.code.info, NULL, info); |
505 | 514 | } else { |
506 | 515 | return 0; |
507 | 516 | } |
@@ -531,8 +540,7 @@ int cmark_node_set_url(cmark_node *node, const char *url) { |
531 | 540 | switch (node->type) { |
532 | 541 | case CMARK_NODE_LINK: |
533 | 542 | case CMARK_NODE_IMAGE: |
534 | | - cmark_set_cstr(node->mem, &node->as.link.url, url); |
535 | | - return 1; |
| 543 | + return cmark_set_cstr(node->mem, &node->as.link.url, NULL, url); |
536 | 544 | default: |
537 | 545 | break; |
538 | 546 | } |
@@ -564,8 +572,7 @@ int cmark_node_set_title(cmark_node *node, const char *title) { |
564 | 572 | switch (node->type) { |
565 | 573 | case CMARK_NODE_LINK: |
566 | 574 | case CMARK_NODE_IMAGE: |
567 | | - cmark_set_cstr(node->mem, &node->as.link.title, title); |
568 | | - return 1; |
| 575 | + return cmark_set_cstr(node->mem, &node->as.link.title, NULL, title); |
569 | 576 | default: |
570 | 577 | break; |
571 | 578 | } |
@@ -597,8 +604,7 @@ int cmark_node_set_on_enter(cmark_node *node, const char *on_enter) { |
597 | 604 | switch (node->type) { |
598 | 605 | case CMARK_NODE_CUSTOM_INLINE: |
599 | 606 | case CMARK_NODE_CUSTOM_BLOCK: |
600 | | - cmark_set_cstr(node->mem, &node->as.custom.on_enter, on_enter); |
601 | | - return 1; |
| 607 | + return cmark_set_cstr(node->mem, &node->as.custom.on_enter, NULL, on_enter); |
602 | 608 | default: |
603 | 609 | break; |
604 | 610 | } |
@@ -630,8 +636,7 @@ int cmark_node_set_on_exit(cmark_node *node, const char *on_exit) { |
630 | 636 | switch (node->type) { |
631 | 637 | case CMARK_NODE_CUSTOM_INLINE: |
632 | 638 | case CMARK_NODE_CUSTOM_BLOCK: |
633 | | - cmark_set_cstr(node->mem, &node->as.custom.on_exit, on_exit); |
634 | | - return 1; |
| 639 | + return cmark_set_cstr(node->mem, &node->as.custom.on_exit, NULL, on_exit); |
635 | 640 | default: |
636 | 641 | break; |
637 | 642 | } |
|
0 commit comments