aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCraig Barnes <craigbarnes@protonmail.com>2021-11-20 15:23:24 +0000
committerCraig Barnes <craigbarnes@protonmail.com>2021-11-20 15:23:24 +0000
commite89e735995382de9b5dd8df5d1f45ed9f766f06a (patch)
treec5380dfa668fc6072c461c83fd0006172d6a31eb
parentconfig: fix memory leak in value_to_enum() (diff)
config: use stack allocation for temporary buffer in value_to_enum()
The maximum size required for the string buffer is relatively small, known at compile-time and not likely to ever grow beyond 512 bytes. Therefore we may as well just use the stack.
-rw-r--r--config.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/config.c b/config.c
index 50306b9d..24ed1267 100644
--- a/config.c
+++ b/config.c
@@ -27,6 +27,7 @@
#include "util.h"
#include "wayland.h"
#include "xmalloc.h"
+#include "xsnprintf.h"
static const uint32_t default_foreground = 0xdcdccc;
static const uint32_t default_background = 0x111111;
@@ -604,17 +605,17 @@ value_to_enum(struct context *ctx, const char **value_map, int *res)
}
const size_t size = str_len + count * 4 + 1;
- char *valid_values = xmalloc(size);
- int idx = 0;
+ char valid_values[512];
+ size_t idx = 0;
+ xassert(size < sizeof(valid_values));
for (size_t i = 0; i < count; i++)
- idx += snprintf(&valid_values[idx], size - idx, "'%s', ", value_map[i]);
+ idx += xsnprintf(&valid_values[idx], size - idx, "'%s', ", value_map[i]);
if (count > 0)
valid_values[idx - 2] = '\0';
LOG_CONTEXTUAL_ERR("not one of %s", valid_values);
- free(valid_values);
*res = -1;
return false;
}