This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
neels gerrit-no-reply at lists.osmocom.orgneels has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/16029 ) Change subject: fix osmo_quote_str_c() to alloc sufficient size ...................................................................... fix osmo_quote_str_c() to alloc sufficient size osmo_quote_str_c() allocates too small buffers when the quoted string also contains unprintable characters that need escaping (which of course are longer than strlen() + 3 that it currently naively assumes). Add osmo_quote_str_buf3() to actually return the required buffer size (sb.chars_needed), osmo_quote_str_buf2() unfortunately returns the char buffer itself. Hence implement osmo_quote_str_c() using OSMO_NAME_C_IMPL(), which now always allocates sufficient size. Change-Id: I04d97e8eec93ffb74006503c356a68cceaf429ac --- M include/osmocom/core/utils.h M src/utils.c 2 files changed, 18 insertions(+), 17 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/29/16029/1 diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h index ef58eeb..cb69b64 100644 --- a/include/osmocom/core/utils.h +++ b/include/osmocom/core/utils.h @@ -151,6 +151,7 @@ char *osmo_escape_str_c(const void *ctx, const char *str, int in_len); const char *osmo_quote_str(const char *str, int in_len); char *osmo_quote_str_buf2(char *buf, size_t bufsize, const char *str, int in_len); +size_t osmo_quote_str_buf3(char *buf, size_t bufsize, const char *str, int in_len); const char *osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize); char *osmo_quote_str_c(const void *ctx, const char *str, int in_len); diff --git a/src/utils.c b/src/utils.c index ea1de0f..95245f5 100644 --- a/src/utils.c +++ b/src/utils.c @@ -756,6 +756,20 @@ return osmo_escape_str_buf2(buf, in_len+1, str, in_len); } +/*! Like osmo_quote_str_buf3(), but returning the buf instead of the required number of characters. + * The function signature is suitable for OSMO_STRBUF_APPEND_NOLEN(). + * \param[out] buf string buffer to write escaped characters to. + * \param[in] bufsize sizeof(buf). + * \param[in] str A string that may contain any characters. + * \param[in] in_len Pass -1 to print until nul char, or >= 0 to force a length. + * \return buf. + */ +char *osmo_quote_str_buf2(char *buf, size_t bufsize, const char *str, int in_len) +{ + osmo_quote_str_buf3(buf, bufsize, str, in_len); + return buf; +} + /*! Like osmo_escape_str_buf2(), but returns double-quotes around a string, or "NULL" for a NULL string. * This allows passing any char* value and get its C representation as string. * The function signature is suitable for OSMO_STRBUF_APPEND_NOLEN(). @@ -765,7 +779,7 @@ * \param[in] in_len Pass -1 to print until nul char, or >= 0 to force a length. * \return Number of characters that would be written if bufsize were large enough excluding '\0' (like snprintf()). */ -char *osmo_quote_str_buf2(char *buf, size_t bufsize, const char *str, int in_len) +size_t osmo_quote_str_buf3(char *buf, size_t bufsize, const char *str, int in_len) { struct osmo_strbuf sb = { .buf = buf, .len = bufsize }; if (!str) @@ -775,7 +789,7 @@ OSMO_STRBUF_APPEND_NOLEN(sb, osmo_escape_str_buf2, str, in_len); OSMO_STRBUF_PRINTF(sb, "\""); } - return buf; + return sb.chars_needed; } /*! Like osmo_quote_str_buf2, but with unusual ordering of arguments, and may sometimes return string constants instead @@ -815,21 +829,7 @@ */ char *osmo_quote_str_c(const void *ctx, const char *str, int in_len) { - 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, len, str, in_len); + OSMO_NAME_C_IMPL(ctx, 32, "(error)", osmo_quote_str_buf3, str, in_len) } /*! perform an integer square root operation on unsigned 32bit integer. -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/16029 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I04d97e8eec93ffb74006503c356a68cceaf429ac Gerrit-Change-Number: 16029 Gerrit-PatchSet: 1 Gerrit-Owner: neels <nhofmeyr at sysmocom.de> Gerrit-MessageType: newchange -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20191111/6e68160b/attachment.htm>