Skip to content

Commit

Permalink
json: Fix parsing of strings that end with a backslash.
Browse files Browse the repository at this point in the history
json_string_unescape() flagged a backslash at the end of a string as an
error, but of course "\\" is a valid string.  This fixes the problem.

VMware-BZ: #1275208
Reported-by: Michael Hu <[email protected]>
Signed-off-by: Ben Pfaff <[email protected]>
Acked-by: Alex Wang <[email protected]>
  • Loading branch information
blp committed Jun 25, 2014
1 parent 5dbebe6 commit 7b7c2a4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/json.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
* Copyright (c) 2009, 2010, 2011, 2012, 2014 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -812,10 +812,6 @@ json_string_unescape(const char *in, size_t in_len, char **outp)

ds_init(&out);
ds_reserve(&out, in_len);
if (in_len > 0 && in[in_len - 1] == '\\') {
ds_put_cstr(&out, "quoted string may not end with backslash");
goto exit;
}
while (in < end) {
if (*in == '"') {
ds_clear(&out);
Expand All @@ -828,6 +824,14 @@ json_string_unescape(const char *in, size_t in_len, char **outp)
}

in++;
if (in >= end) {
/* The JSON parser will never trigger this message, because its
* lexer will never pass in a string that ends in a single
* backslash, but json_string_unescape() has other callers that
* are not as careful.*/
ds_put_cstr(&out, "quoted string may not end with backslash");
goto exit;
}
switch (*in++) {
case '"': case '\\': case '/':
ds_put_char(&out, in[-1]);
Expand Down
7 changes: 7 additions & 0 deletions tests/json.at
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ JSON_CHECK_NEGATIVE([surrogatess must paired properly],
JSON_CHECK_NEGATIVE([null bytes not allowed],
[[["\u0000"]]],
[error: null bytes not supported in quoted strings])
dnl Check for regression against a prior bug.
JSON_CHECK_POSITIVE([properly quoted backslash at end of string],
[[["\\"]]],
[[["\\"]]])
JSON_CHECK_NEGATIVE([stray backslash at end of string],
[[["abcd\"]]],
[error: unexpected end of input in quoted string])

AT_SETUP([end of input in quoted string - C])
AT_KEYWORDS([json negative])
Expand Down

0 comments on commit 7b7c2a4

Please sign in to comment.