[PATCH 2/2] vty: Support multi-char separators and end strings

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/OpenBSC@lists.osmocom.org/.

Jacob Erlbeck jerlbeck at sysmocom.de
Tue Aug 6 12:29:15 UTC 2013


In vty_cmd_string_from_valstr() include the real string lengths of
the sep and end arguments into the buffer size calculation.
---
 src/vty/utils.c       |    9 +++++----
 tests/vty/vty_test.ok |    2 +-
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/src/vty/utils.c b/src/vty/utils.c
index 88932fa..fae5c08 100644
--- a/src/vty/utils.c
+++ b/src/vty/utils.c
@@ -69,12 +69,13 @@ char *vty_cmd_string_from_valstr(void *ctx, const struct value_string *vals,
 				 const char *end, int do_lower)
 {
 	int len = 0, offset = 0, ret, rem;
-	int size = strlen(prefix);
+	int size = strlen(prefix) + strlen(end);
+	int sep_len = strlen(sep); 
 	const struct value_string *vs;
 	char *str;
 
 	for (vs = vals; vs->value || vs->str; vs++)
-		size += strlen(vs->str) + 1;
+		size += strlen(vs->str) + sep_len;
 
 	rem = size;
 	str = talloc_zero_size(ctx, size);
@@ -102,8 +103,8 @@ char *vty_cmd_string_from_valstr(void *ctx, const struct value_string *vals,
 			OSMO_SNPRINTF_RET(ret, rem, offset, len);
 		}
 	}
-	offset--;	/* to remove the trailing | */
-	rem++;
+	offset -= sep_len;	/* to remove the trailing sep */
+	rem += sep_len;
 
 	ret = snprintf(str + offset, rem, "%s", end);
 	if (ret < 0)
diff --git a/tests/vty/vty_test.ok b/tests/vty/vty_test.ok
index 9ff68c8..baec249 100644
--- a/tests/vty/vty_test.ok
+++ b/tests/vty/vty_test.ok
@@ -1,3 +1,3 @@
 Going to test vty_cmd_string_from_valstr()
-Tested with %s-strings, resulting cmd = '[prefix%s%s%s%s%s][foo%s%s%s%s%s][sep%s%s%s%s%s]['
+Tested with %s-strings, resulting cmd = '[prefix%s%s%s%s%s][foo%s%s%s%s%s][sep%s%s%s%s%s][bar%s%s%s%s%s][end%s%s%s%s%s]'
 All tests passed
-- 
1.7.9.5





More information about the OpenBSC mailing list