Change in libosmocore[master]: osmo_escape_str_buf: Always copy, don't return input string pointer

Harald Welte gerrit-no-reply at lists.osmocom.org
Fri Mar 29 16:42:25 UTC 2019


Harald Welte has submitted this change and it was merged. ( https://gerrit.osmocom.org/13447 )

Change subject: osmo_escape_str_buf: Always copy, don't return input string pointer
......................................................................

osmo_escape_str_buf: Always copy, don't return input string pointer

osmo_escape_str_buf() used to have the somewhat odd semantics that
if no escaping was needed, it would return the original pointer without
making any copy to the output buffer.  While this seems like an elegant
optimization, it is a very strange behavior and it works differently
than all of our other *_buf() functions.  Let's unify the API and
turn osmo_escape_str_buf() into a strlcpy() if no escaping is needed.

Change-Id: I3a02bdb27008a73101c2db41ac04248960ed4064
---
M include/osmocom/core/utils.h
M src/utils.c
M tests/utils/utils_test.c
M tests/utils/utils_test.ok
4 files changed, 10 insertions(+), 31 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h
index 16159d3..e3728cd 100644
--- a/include/osmocom/core/utils.h
+++ b/include/osmocom/core/utils.h
@@ -137,7 +137,7 @@
 bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars);
 
 const char *osmo_escape_str(const char *str, int len);
-const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
+char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
 const char *osmo_quote_str(const char *str, int in_len);
 const char *osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
 
diff --git a/src/utils.c b/src/utils.c
index 2d5bcb0..019e733 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -538,14 +538,14 @@
 	return osmo_separated_identifiers_valid(str, NULL);
 }
 
-/*! Return the string with all non-printable characters escaped.
+/*! Return the string with all non-printable characters escapeda, in user-supplied buffer.
  * \param[in] str  A string that may contain any characters.
  * \param[in] len  Pass -1 to print until nul char, or >= 0 to force a length.
  * \param[inout] buf  string buffer to write escaped characters to.
  * \param[in] bufsize  size of \a buf.
- * \returns buf containing an escaped representation, possibly truncated, or str itself.
+ * \returns buf containing an escaped representation, possibly truncated.
  */
-const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize)
+char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize)
 {
 	int in_pos = 0;
 	int next_unprintable = 0;
@@ -567,9 +567,10 @@
 		     && str[next_unprintable] != '\\';
 		     next_unprintable++);
 
-		if (next_unprintable == in_len
-		    && in_pos == 0)
-			return str;
+		if (next_unprintable == in_len && in_pos == 0) {
+			osmo_strlcpy(buf, str, bufsize);
+			return buf;
+		}
 
 		while (in_pos < next_unprintable && out_pos < out_len)
 			out[out_pos++] = str[in_pos++];
@@ -633,25 +634,13 @@
  */
 const char *osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize)
 {
-	const char *res;
 	int l;
 	if (!str)
 		return "NULL";
 	if (bufsize < 3)
 		return "<buf-too-small>";
 	buf[0] = '"';
-	res = osmo_escape_str_buf(str, in_len, buf + 1, bufsize - 2);
-	/* if osmo_escape_str_buf() returned the str itself, we need to copy it to buf to be able to
-	 * quote it. */
-	if (res == str) {
-		/* max_len = bufsize - two quotes - nul term */
-		int max_len = bufsize - 2 - 1;
-		if (in_len >= 0)
-			max_len = OSMO_MIN(in_len, max_len);
-		/* It is not allowed to pass unterminated strings into osmo_strlcpy() :/ */
-		strncpy(buf + 1, str, max_len);
-		buf[1 + max_len] = '\0';
-	}
+	osmo_escape_str_buf(str, in_len, buf + 1, bufsize - 2);
 	l = strlen(buf);
 	buf[l] = '"';
 	buf[l+1] = '\0'; /* both osmo_escape_str_buf() and max_len above ensure room for '\0' */
diff --git a/tests/utils/utils_test.c b/tests/utils/utils_test.c
index d592fe0..711d6e1 100644
--- a/tests/utils/utils_test.c
+++ b/tests/utils/utils_test.c
@@ -545,7 +545,7 @@
 
 	printf("- passthru:\n");
 	res = osmo_escape_str(printable, -1);
-	if (res != printable)
+	if (strcmp(res, printable))
 		printf("NOT passed through! \"%s\"\n", res);
 	else
 		printf("passed through unchanged \"%s\"\n", res);
@@ -560,14 +560,6 @@
 	memset(out_buf, 0x7f, sizeof(out_buf));
 	printf("\"%s\"\n", osmo_escape_str_buf((const char *)in_buf, sizeof(in_buf), out_buf, 10));
 	OSMO_ASSERT(out_buf[10] == 0x7f);
-
-	printf("- passthrough without truncation when no escaping needed:\n");
-	memset(in_buf, 'x', sizeof(in_buf));
-	in_buf[19] = 'E';
-	in_buf[20] = '\0';
-	memset(out_buf, 0x7f, sizeof(out_buf));
-	printf("\"%s\"\n", osmo_escape_str_buf((const char *)in_buf, -1, out_buf, 10));
-	OSMO_ASSERT(out_buf[0] == 0x7f);
 }
 
 static void str_quote_test(void)
diff --git a/tests/utils/utils_test.ok b/tests/utils/utils_test.ok
index 1215ddd..8fce9f1 100644
--- a/tests/utils/utils_test.ok
+++ b/tests/utils/utils_test.ok
@@ -232,8 +232,6 @@
 ""
 - truncation when too long:
 "\axxxxxxE"
-- passthrough without truncation when no escaping needed:
-"xxxxxxxxxxxxxxxxxxxE"
 
 Testing string quoting
 - all chars from 0 to 255 in batches of 16:

-- 
To view, visit https://gerrit.osmocom.org/13447
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I3a02bdb27008a73101c2db41ac04248960ed4064
Gerrit-Change-Number: 13447
Gerrit-PatchSet: 3
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder (1000002)
Gerrit-Reviewer: Pau Espin Pedrol <pespin at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190329/cfce370b/attachment.html>


More information about the gerrit-log mailing list