From 19d479d6844690eae2d40e786ed5c6a88ea180f3 Mon Sep 17 00:00:00 2001
From: Simon Tatham <anakin@pobox.com>
Date: Thu, 21 Nov 2024 12:45:44 +0000
Subject: [PATCH] Fix memory leaks in conf_try_set_*.

Spotted by Coverity: we should check the value_type of the Conf
setting and return failure _before_ allocating the new conf_entry.
---
 utils/conf.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/utils/conf.c b/utils/conf.c
index 074d6e5e..dd51f79f 100644
--- a/utils/conf.c
+++ b/utils/conf.c
@@ -469,13 +469,13 @@ void conf_set_int_int(Conf *conf, int primary,
 
 bool conf_try_set_str(Conf *conf, int primary, const char *value)
 {
-    struct conf_entry *entry = snew(struct conf_entry);
-
     assert(conf_key_info[primary].subkey_type == CONF_TYPE_NONE);
     if (conf_key_info[primary].value_type == CONF_TYPE_UTF8)
         return false;
     assert(conf_key_info[primary].value_type == CONF_TYPE_STR ||
            conf_key_info[primary].value_type == CONF_TYPE_STR_AMBI);
+
+    struct conf_entry *entry = snew(struct conf_entry);
     entry->key.primary = primary;
     entry->value.u.stringval.str = dupstr(value);
     entry->value.u.stringval.utf8 = false;
@@ -491,13 +491,13 @@ void conf_set_str(Conf *conf, int primary, const char *value)
 
 bool conf_try_set_utf8(Conf *conf, int primary, const char *value)
 {
-    struct conf_entry *entry = snew(struct conf_entry);
-
     assert(conf_key_info[primary].subkey_type == CONF_TYPE_NONE);
     if (conf_key_info[primary].value_type == CONF_TYPE_STR)
         return false;
     assert(conf_key_info[primary].value_type == CONF_TYPE_UTF8 ||
            conf_key_info[primary].value_type == CONF_TYPE_STR_AMBI);
+
+    struct conf_entry *entry = snew(struct conf_entry);
     entry->key.primary = primary;
     entry->value.u.stringval.str = dupstr(value);
     entry->value.u.stringval.utf8 = true;