MULE
0.0.1
Multi-Use Library Essentials
Toggle main menu visibility
Loading...
Searching...
No Matches
10_dangling.c
A bug on purpose. A view kept across an append that moves the buffer reads freed memory.
/* Bug on purpose: a view kept across a call that reallocates. */
#include "mem/mem.c"
#include "str/str.c"
#include <stdio.h>
#include <stdlib.h>
int
main(
void
) {
Str
s = { 0 };
if
(!
str_append_cstr
(&s,
"Sonya"
))
return
EXIT_FAILURE;
StrView
name =
str_view
(&s);
/* 15 bytes fit; this append needs more, so the buffer moves. */
if
(!
str_append_cstr
(&s,
" Semyonovna Marmeladova"
))
return
EXIT_FAILURE;
printf(
"%.*s\n"
,
STR_FMT
(name));
/* reads freed memory */
str_free
(&s);
return
EXIT_SUCCESS;
}
str_view
StrView str_view(const Str *s)
Borrow the whole of s.
Definition
str.c:212
STR_FMT
#define STR_FMT(s)
printf helper: printf("%.*s", STR_FMT(s)) prints exactly len bytes.
Definition
str.h:85
str_free
void str_free(Str *s)
Free the buffer and reset s to the zero state.
Definition
str.c:50
str_append_cstr
bool str_append_cstr(Str *s, const char *cstr)
Append cstr.
Definition
str.c:119
Str
Owned, growable byte string.
Definition
str.h:64
StrView
Non-owning view of bytes; handy for passing slices and literals around.
Definition
str.h:72
Generated by
1.18.0