diff options
author | David Robillard <d@drobilla.net> | 2023-03-28 12:12:22 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-04-05 09:45:15 -0400 |
commit | 25659b663cec302a1ef1c7d27dbbee200a838026 (patch) | |
tree | 9e32584617ca6a4603a829b857fefe6235cb8a63 /test | |
parent | de3fea9563a94c350972e8625e13aecd27b49b34 (diff) | |
download | serd-25659b663cec302a1ef1c7d27dbbee200a838026.tar.gz serd-25659b663cec302a1ef1c7d27dbbee200a838026.tar.bz2 serd-25659b663cec302a1ef1c7d27dbbee200a838026.zip |
Fix potential memory leaks when a write is aborted
Also clean up and simplify writer context management in general.
Diffstat (limited to 'test')
-rw-r--r-- | test/test_writer.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/test/test_writer.c b/test/test_writer.c index c1a5f468..356dbfcb 100644 --- a/test/test_writer.c +++ b/test/test_writer.c @@ -7,6 +7,7 @@ #include <assert.h> #include <stdint.h> +#include <stdio.h> #include <string.h> #define USTR(s) ((const uint8_t*)(s)) @@ -41,10 +42,58 @@ test_write_long_literal(void) serd_free(out); } +static size_t +null_sink(const void* const buf, const size_t len, void* const stream) +{ + (void)buf; + (void)stream; + + return len; +} + +static void +test_writer_cleanup(void) +{ + SerdStatus st = SERD_SUCCESS; + SerdEnv* env = serd_env_new(NULL); + SerdWriter* writer = + serd_writer_new(SERD_TURTLE, (SerdStyle)0U, env, NULL, null_sink, NULL); + + SerdNode s = serd_node_from_string(SERD_URI, USTR("http://example.org/s")); + SerdNode p = serd_node_from_string(SERD_URI, USTR("http://example.org/p")); + SerdNode o = serd_node_from_string(SERD_BLANK, USTR("http://example.org/o")); + + st = serd_writer_write_statement( + writer, SERD_ANON_O_BEGIN, NULL, &s, &p, &o, NULL, NULL); + + assert(!st); + + // Write the start of several nested anonymous objects + for (unsigned i = 0U; !st && i < 8U; ++i) { + char buf[12] = {0}; + snprintf(buf, sizeof(buf), "b%u", i); + + SerdNode next_o = serd_node_from_string(SERD_BLANK, USTR(buf)); + + st = serd_writer_write_statement( + writer, SERD_ANON_O_BEGIN, NULL, &o, &p, &next_o, NULL, NULL); + + o = next_o; + } + + // Finish writing without terminating nodes + assert(!(st = serd_writer_finish(writer))); + + // Free (which could leak if the writer doesn't clean up the stack properly) + serd_writer_free(writer); + serd_env_free(env); +} + int main(void) { test_write_long_literal(); + test_writer_cleanup(); return 0; } |