From dd4c6d45f20e0e5ec771263a3227f1f9bc5d3784 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 5 Aug 2006 22:06:12 +0000 Subject: [PATCH] buffer2array: fix for trailing sub-quoted text inside a quoted context Also added a unit test to check for errors/bugs to make sure we don't have regressions. Bug found by Qball. git-svn-id: https://svn.musicpd.org/mpd/trunk@4569 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- src/buffer2array.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/buffer2array.c b/src/buffer2array.c index 39fc29bc9..ecd01701b 100644 --- a/src/buffer2array.c +++ b/src/buffer2array.c @@ -34,10 +34,12 @@ int cstrtok(char *buffer, char *array[], const int max) array[i++] = ++c; while (*c != '\0') { if (*c == '\"') { - if (escape) + if (escape) { memmove(c - 1, c, strlen(c) + 1); - else { + if (*c == '"') + break; + } else { *(c++) = '\0'; break; } @@ -62,3 +64,35 @@ int cstrtok(char *buffer, char *array[], const int max) } return i; } + +#ifdef UNIT_TEST + +#include +#include +#include + +int main() +{ + char *a[4] = { NULL }; + char *b; + int i, max; + + b = strdup("lsinfo \"/some/dir/name \\\"test\\\"\""); + max = cstrtok(b, a, 4); + assert( !strcmp("lsinfo", a[0]) ); + assert( !strcmp("/some/dir/name \"test\"", a[1]) ); + + b = strdup("lsinfo \"/some/dir/name \\\"test\\\" something else\""); + max = cstrtok(b, a, 4); + assert( !strcmp("lsinfo", a[0]) ); + assert( !strcmp("/some/dir/name \"test\" something else", a[1]) ); + + b = strdup("lsinfo \"/some/dir\\\\name\""); + max = cstrtok(b, a, 4); + assert( !strcmp("lsinfo", a[0]) ); + assert( !strcmp("/some/dir\\name", a[1]) ); + + return 0; +} + +#endif