]> git.ozlabs.org Git - ccan/commitdiff
membuf: avoid buffer overrun in example code, memleak.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 9 Jan 2023 02:17:22 +0000 (12:47 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 16 Mar 2023 23:22:47 +0000 (09:52 +1030)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/membuf/_info

index bdcbce2b2f205724d39a6783e212eee2da3a7e83..a859318c62ee3486f5110ad9dbe7ee74e9e08438 100644 (file)
  *
  *     membuf_init(&charbuf, malloc(10), 10, membuf_realloc);
  *
- *     for (int i = 1; i < argc; i++)
- *             strcpy(membuf_add(&charbuf, strlen(argv[i])), argv[i]);
+ *     for (int i = 1; i < argc; i++) {
+ *             size_t len = strlen(argv[i]);
+ *             memcpy(membuf_add(&charbuf, len), argv[i], len);
+ *     }
  *
  *     // This is dumb, we could do all at once, but shows technique.
  *     while (membuf_num_elems(&charbuf) > 0)
  *             printf("%c", *(char *)membuf_consume(&charbuf, 1));
  *     printf("\n");
+ *     free(membuf_cleanup(&charbuf));
  *     return 0;
  * }
  */