All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] util: Fix skipped characters in strsplit functions
@ 2016-01-08  0:26 Mat Martineau
  2016-01-08  0:26 ` [PATCH 2/2] unit: Additional strsplit cases with more delimiter variety Mat Martineau
  0 siblings, 1 reply; 3+ messages in thread
From: Mat Martineau @ 2016-01-08  0:26 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 1271 bytes --]

l_strsplit and l_strsplit_sep were skipping an extra character after
each delimiter because len was incremented after being set to 0 but
before the next iteration of the loop began. This caused problems with
trailing and consecutive delimiters.
---
 ell/util.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/ell/util.c b/ell/util.c
index 5bd7b3b..f920895 100644
--- a/ell/util.c
+++ b/ell/util.c
@@ -293,9 +293,15 @@ LIB_EXPORT char **l_strsplit(const char *str, const char sep)
 
 	ret = l_new(char *, len + 1);
 
-	for (i = 0, p = str, len = 0; p[len]; len++) {
-		if (p[len] != sep)
+	i = 0;
+	p = str;
+	len = 0;
+
+	while (p[len]) {
+		if (p[len] != sep) {
+			len += 1;
 			continue;
+		}
 
 		ret[i++] = l_strndup(p, len);
 		p += len + 1;
@@ -347,9 +353,15 @@ LIB_EXPORT char **l_strsplit_set(const char *str, const char *separators)
 
 	ret = l_new(char *, len + 1);
 
-	for (i = 0, p = str, len = 0; p[len]; len++) {
-		if (sep_table[(unsigned char) p[len]] != true)
+	i = 0;
+	p = str;
+	len = 0;
+
+	while (p[len]) {
+		if (sep_table[(unsigned char) p[len]] != true) {
+			len += 1;
 			continue;
+		}
 
 		ret[i++] = l_strndup(p, len);
 		p += len + 1;
-- 
2.7.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-01-08  0:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-01-08  0:26 [PATCH 1/2] util: Fix skipped characters in strsplit functions Mat Martineau
2016-01-08  0:26 ` [PATCH 2/2] unit: Additional strsplit cases with more delimiter variety Mat Martineau
2016-01-08  0:38   ` Denis Kenzior

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.