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
This commit is contained in:
parent
8e8d4fc6fd
commit
dd4c6d45f2
@ -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 <stdio.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user