From 37db7d3ea6d9f62940b26dc5e9f9457a8c35eced Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Tue, 7 May 2019 16:53:04 +0200 Subject: Fix osmo_quote_str_c() for strings larger than 32 bytes As Neels pointed out, we shouldn't pass a constant value of 32 to osmo_quote_str_buf2(). Change-Id: Id9bde14166d6674ce4dda36fa9f4ae9217ce5cc2 --- src/utils.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/utils.c b/src/utils.c index b66721e6..59dc8163 100644 --- a/src/utils.c +++ b/src/utils.c @@ -813,10 +813,21 @@ const char *osmo_quote_str(const char *str, int in_len) */ char *osmo_quote_str_c(const void *ctx, const char *str, int in_len) { - char *buf = talloc_size(ctx, OSMO_MAX(in_len+2, 32)); + size_t len = in_len == -1 ? strlen(str) : in_len; + char *buf; + + /* account for two quote characters + terminating NUL */ + len += 3; + + /* some minimum length for things like "NULL" or "(error)" */ + if (len < 32) + len = 32; + + buf = talloc_size(ctx, len); if (!buf) return NULL; - return osmo_quote_str_buf2(buf, 32, str, in_len); + + return osmo_quote_str_buf2(buf, len, str, in_len); } /*! perform an integer square root operation on unsigned 32bit integer. -- cgit v1.2.3