Module Name: src Committed By: thorpej Date: Fri Nov 17 21:29:33 UTC 2023
Modified Files: src/common/lib/libprop: prop_string.c Log Message: In _prop_string_instantiate(), when we de-dup a non-MUTABLE string, make sure we free the provided string buffer if NOCOPY is not set. Fixes a memory leak reported by M. Boerschig. While we're at it, also change _prop_string_instantiate() to free the provided string buffer in the not-NOCOPY case when string object allocation fails (this was previously handled by _prop_string_instantiate()'s callers). PR lib/57699 To generate a diff of this commit: cvs rdiff -u -r1.17 -r1.18 src/common/lib/libprop/prop_string.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/common/lib/libprop/prop_string.c diff -u src/common/lib/libprop/prop_string.c:1.17 src/common/lib/libprop/prop_string.c:1.18 --- src/common/lib/libprop/prop_string.c:1.17 Wed Aug 3 21:13:46 2022 +++ src/common/lib/libprop/prop_string.c Fri Nov 17 21:29:33 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: prop_string.c,v 1.17 2022/08/03 21:13:46 riastradh Exp $ */ +/* $NetBSD: prop_string.c,v 1.18 2023/11/17 21:29:33 thorpej Exp $ */ /*- * Copyright (c) 2006, 2020 The NetBSD Foundation, Inc. @@ -247,12 +247,18 @@ _prop_string_instantiate(int const flags */ prop_object_retain(ops); _PROP_MUTEX_UNLOCK(_prop_string_tree_mutex); + if ((flags & PS_F_NOCOPY) == 0) { + _PROP_FREE(ps->ps_mutable, + M_PROP_STRING); + } _PROP_POOL_PUT(_prop_string_pool, ps); ps = ops; } else { _PROP_MUTEX_UNLOCK(_prop_string_tree_mutex); } } + } else if ((flags & PS_F_NOCOPY) == 0) { + _PROP_FREE(__UNCONST(str), M_PROP_STRING); } return (ps); @@ -311,7 +317,6 @@ prop_string_create_cstring_nocopy(const prop_string_t __printflike(1, 2) prop_string_create_format(const char *fmt, ...) { - prop_string_t ps; char *str = NULL; int len; size_t nlen; @@ -335,11 +340,7 @@ prop_string_create_format(const char *fm vsnprintf(str, nlen, fmt, ap); va_end(ap); - ps = _prop_string_instantiate(0, str, (size_t)len); - if (ps == NULL) - _PROP_FREE(str, M_PROP_STRING); - - return (ps); + return _prop_string_instantiate(0, str, (size_t)len); } /* @@ -374,7 +375,6 @@ prop_string_create_nocopy(const char *st prop_string_t prop_string_copy(prop_string_t ops) { - prop_string_t ps; char *cp; if (! prop_object_is_string(ops)) @@ -391,11 +391,7 @@ prop_string_copy(prop_string_t ops) strcpy(cp, prop_string_contents(ops)); - ps = _prop_string_instantiate(PS_F_MUTABLE, cp, ops->ps_size); - if (ps == NULL) - _PROP_FREE(cp, M_PROP_STRING); - - return (ps); + return _prop_string_instantiate(PS_F_MUTABLE, cp, ops->ps_size); } _PROP_DEPRECATED(prop_string_copy_mutable, @@ -404,7 +400,6 @@ _PROP_DEPRECATED(prop_string_copy_mutabl prop_string_t prop_string_copy_mutable(prop_string_t ops) { - prop_string_t ps; char *cp; if (! prop_object_is_string(ops)) @@ -416,11 +411,7 @@ prop_string_copy_mutable(prop_string_t o strcpy(cp, prop_string_contents(ops)); - ps = _prop_string_instantiate(PS_F_MUTABLE, cp, ops->ps_size); - if (ps == NULL) - _PROP_FREE(cp, M_PROP_STRING); - - return (ps); + return _prop_string_instantiate(PS_F_MUTABLE, cp, ops->ps_size); } /* @@ -655,7 +646,6 @@ bool _prop_string_internalize(prop_stack_t stack, prop_object_t *obj, struct _prop_object_internalize_context *ctx) { - prop_string_t string; char *str; size_t len, alen; @@ -691,10 +681,6 @@ _prop_string_internalize(prop_stack_t st return (true); } - string = _prop_string_instantiate(0, str, len); - if (string == NULL) - _PROP_FREE(str, M_PROP_STRING); - - *obj = string; + *obj = _prop_string_instantiate(0, str, len); return (true); }