Fix getTokenExclude hang on a trailing backslash
getTokenExclude looped forever when the data ended in a backslash: once the cursor hit the end, the "previous character was a backslash" check never changed. It also treated an escaped backslash (\\) before the terminator as escaping it, so a token could never end in a backslash. Stop at the end of the data, and treat a terminator as escaped only when an odd run of backslashes precedes it. Escapes are still returned as written, so tokens without those two cases are unchanged (testing/zstring_test output is identical). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f5aee23f5e
commit
6719b8c9a3
1 file changed
+15
-7
+15
-7
@@ -328,16 +328,24 @@ namespace coreutils {
|
||||
}
|
||||
|
||||
ZString ZString::getTokenExclude(const char *exclude) {
|
||||
char previous = 0;
|
||||
// Scans up to the first character in exclude that is not escaped.
|
||||
// A terminator is escaped only by an odd run of backslashes before
|
||||
// it, so "a\\" ends at its closing quote while "a\"b" does not.
|
||||
// Escapes are left in the returned token as written. Stops at the
|
||||
// end of the data - previously a token ending in a backslash looped
|
||||
// forever there.
|
||||
char *start = cursor;
|
||||
do {
|
||||
while ((cursor < (data + length)) && !isCharacter(*cursor, exclude)) {
|
||||
previous = *cursor;
|
||||
++cursor;
|
||||
char *end = data + length;
|
||||
while(cursor < end) {
|
||||
if(isCharacter(*cursor, exclude)) {
|
||||
size_t backslashes = 0;
|
||||
for(char *p = cursor; (p > start) && (p[-1] == '\\'); --p)
|
||||
++backslashes;
|
||||
if((backslashes % 2) == 0)
|
||||
break;
|
||||
}
|
||||
++cursor;
|
||||
} while(previous == '\\');
|
||||
--cursor;
|
||||
}
|
||||
return ZString(start, cursor - start);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user