diff options
author | David Robillard <d@drobilla.net> | 2023-09-10 11:50:45 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-09-22 23:54:50 -0400 |
commit | 031decbb82f34313a2403e8852ce630b573eb177 (patch) | |
tree | a4b564638bb4462a2b6bec5eaa65274a0ac44f40 | |
parent | af1d31f6ee9d822d85087ad03ad8b2cee5a36744 (diff) | |
download | serd-031decbb82f34313a2403e8852ce630b573eb177.tar.gz serd-031decbb82f34313a2403e8852ce630b573eb177.tar.bz2 serd-031decbb82f34313a2403e8852ce630b573eb177.zip |
Fix potential realloc leaks
-rw-r--r-- | src/env.c | 9 | ||||
-rw-r--r-- | src/writer.c | 9 |
2 files changed, 12 insertions, 6 deletions
@@ -119,10 +119,13 @@ serd_env_add(SerdEnv* const env, serd_node_free(&old_prefix_uri); } } else { - env->prefixes = (SerdPrefix*)realloc( + SerdPrefix* const new_prefixes = (SerdPrefix*)realloc( env->prefixes, (++env->n_prefixes) * sizeof(SerdPrefix)); - env->prefixes[env->n_prefixes - 1].name = serd_node_copy(name); - env->prefixes[env->n_prefixes - 1].uri = serd_node_copy(uri); + if (new_prefixes) { + env->prefixes = new_prefixes; + env->prefixes[env->n_prefixes - 1].name = serd_node_copy(name); + env->prefixes[env->n_prefixes - 1].uri = serd_node_copy(uri); + } } } diff --git a/src/writer.c b/src/writer.c index 07edc7f4..2a6d7c31 100644 --- a/src/writer.c +++ b/src/writer.c @@ -1266,9 +1266,12 @@ size_t serd_chunk_sink(const void* buf, size_t len, void* stream) { SerdChunk* chunk = (SerdChunk*)stream; - chunk->buf = (uint8_t*)realloc((uint8_t*)chunk->buf, chunk->len + len); - memcpy((uint8_t*)chunk->buf + chunk->len, buf, len); - chunk->len += len; + uint8_t* new_buf = (uint8_t*)realloc((uint8_t*)chunk->buf, chunk->len + len); + if (new_buf) { + memcpy(new_buf + chunk->len, buf, len); + chunk->buf = new_buf; + chunk->len += len; + } return len; } |