<p>neels has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/c/libosmocore/+/16159">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">fix osmo_escape_str_c() and osmo_quote_str_c()<br><br>The osmo_escape_str_c() and osmo_quote_str_c() functions return truncated<br>results when characters need escaping. For example:<br><br>  osmo_quote_str_c(NULL, "foo"); --> "foo"<br>  osmo_quote_str_c(NULL, "foo\n"); --> "foo\n<br>  osmo_quote_str_c(NULL, "foo\tbar\t\n"); --> "foo\tbar\t<br><br>Implement these _c variants using OSMO_NAME_C_IMPL() to always allocate<br>sufficient memory.<br><br>However, current osmo_escape_str_buf2() and osmo_quote_str_buf2() fail to<br>return the required buffer size (even though that information is readily<br>avaiable), so these don't qualify for accurate use of OSMO_NAME_C_IMPL().<br><br>Hence, move the implementations of osmo_escape_str and osmo_quote_str to an<br>internal static function that returns the characters needed, so that all<br>dynamically allocating implementations can return un-truncated results.<br><br>Of course, external callers would also benefit from escape/quote API that<br>accurately returns the amount of characters needed, but I am not changing<br>public API in this patch, on purpose, ... yet.<br><br>Change-Id: I16c08eced41bf1b7acf6e95f658068ace99ca4c8<br>---<br>M src/utils.c<br>1 file changed, 49 insertions(+), 31 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/59/16159/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/src/utils.c b/src/utils.c</span><br><span>index 6fc2ee6..904f6e4 100644</span><br><span>--- a/src/utils.c</span><br><span>+++ b/src/utils.c</span><br><span>@@ -668,13 +668,15 @@</span><br><span> }</span><br><span> </span><br><span> /*! Return the string with all non-printable characters escaped.</span><br><span style="color: hsl(120, 100%, 40%);">+ * This internal function is the implementation for all osmo_escape_str* and osmo_quote_str* API versions.</span><br><span style="color: hsl(120, 100%, 40%);">+ * It provides a return value of characters-needed, to allow producing un-truncated strings in all cases.</span><br><span>  * \param[out] buf  string buffer to write escaped characters to.</span><br><span>  * \param[in] bufsize  sizeof(buf).</span><br><span>  * \param[in] str  A string that may contain any characters.</span><br><span>  * \param[in] in_len  Pass -1 to print until nul char, or >= 0 to force a length (also past nul chars).</span><br><span style="color: hsl(0, 100%, 40%);">- * \return The output buffer (buf).</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return Number of characters that would be written if bufsize were large enough excluding '\0' (like snprintf()).</span><br><span>  */</span><br><span style="color: hsl(0, 100%, 40%);">-char *osmo_escape_str_buf2(char *buf, size_t bufsize, const char *str, int in_len)</span><br><span style="color: hsl(120, 100%, 40%);">+static size_t _osmo_escape_str_buf(char *buf, size_t bufsize, const char *str, int in_len)</span><br><span> {</span><br><span>   struct osmo_strbuf sb = { .buf = buf, .len = bufsize };</span><br><span>      int in_pos = 0;</span><br><span>@@ -729,6 +731,19 @@</span><br><span>       }</span><br><span> </span><br><span> done:</span><br><span style="color: hsl(120, 100%, 40%);">+        return sb.chars_needed;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! Return the string with all non-printable characters escaped.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[out] buf  string buffer to write escaped characters to.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] bufsize  sizeof(buf).</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] str  A string that may contain any characters.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] in_len  Pass -1 to print until nul char, or >= 0 to force a length (also past nul chars).</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return The output buffer (buf).</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+char *osmo_escape_str_buf2(char *buf, size_t bufsize, const char *str, int in_len)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     _osmo_escape_str_buf(buf, bufsize, str, in_len);</span><br><span>     return buf;</span><br><span> }</span><br><span> </span><br><span>@@ -750,10 +765,31 @@</span><br><span>  */</span><br><span> char *osmo_escape_str_c(const void *ctx, const char *str, int in_len)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-       char *buf = talloc_size(ctx, in_len+1);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!buf)</span><br><span style="color: hsl(0, 100%, 40%);">-               return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-    return osmo_escape_str_buf2(buf, in_len+1, str, in_len);</span><br><span style="color: hsl(120, 100%, 40%);">+      /* The string will be at least as long as in_len, but some characters might need escaping.</span><br><span style="color: hsl(120, 100%, 40%);">+     * These extra bytes should catch most usual escaping situations, avoiding a second run in OSMO_NAME_C_IMPL. */</span><br><span style="color: hsl(120, 100%, 40%);">+       OSMO_NAME_C_IMPL(ctx, in_len + 16, "ERROR", _osmo_escape_str_buf, str, in_len);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! Return a quoted and escaped representation of the string.</span><br><span style="color: hsl(120, 100%, 40%);">+ * This internal function is the implementation for all osmo_quote_str* API versions.</span><br><span style="color: hsl(120, 100%, 40%);">+ * It provides a return value of characters-needed, to allow producing un-truncated strings in all cases.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[out] buf  string buffer to write escaped characters to.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] bufsize  sizeof(buf).</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] str  A string that may contain any characters.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] in_len  Pass -1 to print until nul char, or >= 0 to force a length (also past nul chars).</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return Number of characters that would be written if bufsize were large enough excluding '\0' (like snprintf()).</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static size_t _osmo_quote_str_buf(char *buf, size_t bufsize, const char *str, int in_len)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    struct osmo_strbuf sb = { .buf = buf, .len = bufsize };</span><br><span style="color: hsl(120, 100%, 40%);">+       if (!str)</span><br><span style="color: hsl(120, 100%, 40%);">+             OSMO_STRBUF_PRINTF(sb, "NULL");</span><br><span style="color: hsl(120, 100%, 40%);">+     else {</span><br><span style="color: hsl(120, 100%, 40%);">+                OSMO_STRBUF_PRINTF(sb, "\"");</span><br><span style="color: hsl(120, 100%, 40%);">+          OSMO_STRBUF_APPEND(sb, _osmo_escape_str_buf, str, in_len);</span><br><span style="color: hsl(120, 100%, 40%);">+            OSMO_STRBUF_PRINTF(sb, "\"");</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+     return sb.chars_needed;</span><br><span> }</span><br><span> </span><br><span> /*! Like osmo_escape_str_buf2(), but returns double-quotes around a string, or "NULL" for a NULL string.</span><br><span>@@ -767,14 +803,7 @@</span><br><span>  */</span><br><span> char *osmo_quote_str_buf2(char *buf, size_t bufsize, const char *str, int in_len)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-  struct osmo_strbuf sb = { .buf = buf, .len = bufsize };</span><br><span style="color: hsl(0, 100%, 40%);">- if (!str)</span><br><span style="color: hsl(0, 100%, 40%);">-               OSMO_STRBUF_PRINTF(sb, "NULL");</span><br><span style="color: hsl(0, 100%, 40%);">-       else {</span><br><span style="color: hsl(0, 100%, 40%);">-          OSMO_STRBUF_PRINTF(sb, "\"");</span><br><span style="color: hsl(0, 100%, 40%);">-            OSMO_STRBUF_APPEND_NOLEN(sb, osmo_escape_str_buf2, str, in_len);</span><br><span style="color: hsl(0, 100%, 40%);">-                OSMO_STRBUF_PRINTF(sb, "\"");</span><br><span style="color: hsl(0, 100%, 40%);">-    }</span><br><span style="color: hsl(120, 100%, 40%);">+     _osmo_quote_str_buf(buf, bufsize, str, in_len);</span><br><span>      return buf;</span><br><span> }</span><br><span> </span><br><span>@@ -792,7 +821,7 @@</span><br><span>           return "NULL";</span><br><span>     if (!buf || !bufsize)</span><br><span>                return "(error)";</span><br><span style="color: hsl(0, 100%, 40%);">-     osmo_quote_str_buf2(buf, bufsize, str, in_len);</span><br><span style="color: hsl(120, 100%, 40%);">+       _osmo_quote_str_buf(buf, bufsize, str, in_len);</span><br><span>      return buf;</span><br><span> }</span><br><span> </span><br><span>@@ -804,7 +833,8 @@</span><br><span>  */</span><br><span> const char *osmo_quote_str(const char *str, int in_len)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-       return osmo_quote_str_buf(str, in_len, namebuf, sizeof(namebuf));</span><br><span style="color: hsl(120, 100%, 40%);">+     _osmo_quote_str_buf(namebuf, sizeof(namebuf), str, in_len);</span><br><span style="color: hsl(120, 100%, 40%);">+   return namebuf;</span><br><span> }</span><br><span> </span><br><span> /*! Like osmo_quote_str_buf() but returns the result in a dynamically-allocated buffer.</span><br><span>@@ -814,21 +844,9 @@</span><br><span>  */</span><br><span> char *osmo_quote_str_c(const void *ctx, const char *str, int in_len)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-  size_t len = in_len == -1 ? strlen(str) : in_len;</span><br><span style="color: hsl(0, 100%, 40%);">-       char *buf;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-      /* account for two quote characters + terminating NUL */</span><br><span style="color: hsl(0, 100%, 40%);">-        len += 3;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-       /* some minimum length for things like "NULL" or "(error)" */</span><br><span style="color: hsl(0, 100%, 40%);">-       if (len < 32)</span><br><span style="color: hsl(0, 100%, 40%);">-                len = 32;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-       buf = talloc_size(ctx, len);</span><br><span style="color: hsl(0, 100%, 40%);">-    if (!buf)</span><br><span style="color: hsl(0, 100%, 40%);">-               return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-    return osmo_quote_str_buf2(buf, len, str, in_len);</span><br><span style="color: hsl(120, 100%, 40%);">+    /* The string will be at least as long as in_len, but some characters might need escaping.</span><br><span style="color: hsl(120, 100%, 40%);">+     * These extra bytes should catch most usual escaping situations, avoiding a second run in OSMO_NAME_C_IMPL. */</span><br><span style="color: hsl(120, 100%, 40%);">+       OSMO_NAME_C_IMPL(ctx, in_len + 16, "ERROR", _osmo_quote_str_buf, str, in_len);</span><br><span> }</span><br><span> </span><br><span> /*! perform an integer square root operation on unsigned 32bit integer.</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/libosmocore/+/16159">change 16159</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/libosmocore/+/16159"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: libosmocore </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I16c08eced41bf1b7acf6e95f658068ace99ca4c8 </div>
<div style="display:none"> Gerrit-Change-Number: 16159 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: neels <nhofmeyr@sysmocom.de> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>