From 6e5f65caf34e2171af701e25206d42186b7640ff Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Thu, 3 Jul 2014 18:38:13 +0200 Subject: [PATCH] data: Improved memory handling in data_copy(). Ensure that dynamic memory in the destination is freed before copying the new data. That'll avoid that the caller has to use data_free_datum() first and destroy previous data even if the copy fails. --- src/core/data.c | 1 + src/core/store.c | 4 +--- src/include/core/data.h | 3 ++- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/data.c b/src/core/data.c index dc2d0ae..beba5a7 100644 --- a/src/core/data.c +++ b/src/core/data.c @@ -65,6 +65,7 @@ sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src) break; } + sdb_data_free_datum(dst); *dst = tmp; return 0; } /* sdb_data_copy */ diff --git a/src/core/store.c b/src/core/store.c index 3885e50..8b284e7 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -532,10 +532,8 @@ sdb_store_attribute(const char *hostname, if (status >= 0) { assert(updated_attr); - sdb_data_free_datum(&ATTR(updated_attr)->value); - if (sdb_data_copy(&ATTR(updated_attr)->value, value)) { + if (sdb_data_copy(&ATTR(updated_attr)->value, value)) status = -1; - } } pthread_rwlock_unlock(&host_lock); diff --git a/src/include/core/data.h b/src/include/core/data.h index 879af90..d6bf317 100644 --- a/src/include/core/data.h +++ b/src/include/core/data.h @@ -81,7 +81,8 @@ typedef struct { * Copy the datum stored in 'src' to the memory location pointed to by 'dst'. * Any dynamic data (strings, binary data) is copied to newly allocated * memory. Use, for example, sdb_data_free_datum() to free any dynamic memory - * stored in a datum. + * stored in a datum. On error, 'dst' is unchanged. Else, any dynamic memory + * in 'dst' will be freed. * * Returns: * - 0 on success -- 2.30.2