: QMP/Monitor queue
Hi Anthony,
The following QMP/Monitor patches have been sent to the list and look good
to me. I have also tested most of them.
The changes (since 0e2029a063405091ee34170ef71aa321715e4357) are available in
the following repository:
git://repo.or.cz/qemu/qmp-unstable.git for-anthony
Jan Kiszka (1):
hxtool: Fix line number reporting on SQMP/EQMP errors
Luiz Capitulino (6):
json-lexer: Initialize 'x' and 'y'
json-lexer: Handle missing escapes
qjson: Handle "\f"
check-qjson: Add more escape tests
json-lexer: Drop 'buf'
json-streamer: Don't use qdict_put_obj()
Paolo Bonzini (3):
add some tests for invalid JSON
implement optional lookahead in json lexer
remove unnecessary lookaheads
check-qjson.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++- hxtool | 2 + json-lexer.c | 114 ++++++++++++++++++++++++++---------------------------- json-parser.c | 4 ++ json-streamer.c | 8 ++-- qjson.c | 3 + 6 files changed, 177 insertions(+), 66 deletions(-)
PATCH 1/10 json-lexer: Initialize 'x' and 'y'
The 'lexer' variable is passed by the caller, it can contain anything (eg. garbage).
--- json-lexer.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
json-lexer.c +1 -0
@@ -275,6 +275,7 @@ void json_lexer_init(JSONLexer *lexer, JSONLexerEmitter func) lexer->emit = func; lexer->state =IN_START ; lexer->token =qstring_new (); + lexer->x = lexer->y =0 ; }static int json_lexer_feed_char (JSONLexer *lexer,char ch)
Signature · 1 line
1.7.1.251.g92a7
PATCH 2/10 json-lexer: Handle missing escapes
In reply to Luiz CapitulinoThe JSON escape sequence "\/" and "\\" are valid and should be handled.
--- json-lexer.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
json-lexer.c +4 -0
@@ -97,6 +97,8 @@ static const uint8_t json_lexer[][256] = { ['n'] = IN_DQ_STRING, ['r'] = IN_DQ_STRING, ['t'] = IN_DQ_STRING, + ['/'] = IN_DQ_STRING, + ['\\'] = IN_DQ_STRING, ['\''] = IN_DQ_STRING, ['\"'] = IN_DQ_STRING, ['u'] = IN_DQ_UCODE0,
@@ -134,6 +136,8 @@ static const uint8_t json_lexer[][256] = { ['n'] = IN_SQ_STRING, ['r'] = IN_SQ_STRING, ['t'] = IN_SQ_STRING, + ['/'] = IN_DQ_STRING, + ['\\'] = IN_DQ_STRING, ['\''] = IN_SQ_STRING, ['\"'] = IN_SQ_STRING, ['u'] = IN_SQ_UCODE0,
Signature · 1 line
1.7.1.251.g92a7
PATCH 3/10 qjson: Handle "\f"
In reply to Luiz CapitulinoIt's valid JSON and should be handled.
--- json-parser.c | 4 ++++ qjson.c | 3 +++ 2 files changed, 7 insertions(+), 0 deletions(-)
json-parser.c +4 -0
@@ -206,6 +206,10 @@ static QString *qstring_from_escaped_str(JSONParserContext *ctxt, QObject *tokenqstring_append (str,"\b" ); ptr++;break ; +case 'f' : +qstring_append (str,"\f" ); + ptr++; +break ;case 'n' :qstring_append (str,"\n" ); ptr++;
qjson.c +3 -0
@@ -158,6 +158,9 @@ static void to_json(const QObject *obj, QString *str)case '\b' :qstring_append (str,"\\b" );break ; +case '\f' : +qstring_append (str,"\\f" ); +break ;case '\n' :qstring_append (str,"\\n" );break ;
Signature · 1 line
1.7.1.251.g92a7
PATCH 4/10 check-qjson: Add more escape tests
In reply to Luiz CapitulinoWhile there make the fail_unless() calls print error messages.
IMPORTANT: The test for "\/" is failing, don't know why.
--- check-qjson.c | 14 ++++++++++++-- 1 files changed, 12 insertions(+), 2 deletions(-)
check-qjson.c +12 -2
@@ -29,6 +29,13 @@ START_TEST(escaped_string)const char *decoded;int skip; } test_cases[] = { + { "\"\\b\"", "\b" }, + { "\"\\f\"", "\f" }, + { "\"\\n\"", "\n" }, + { "\"\\r\"", "\r" }, + { "\"\\t\"", "\t" }, + { "\"\\/\"", "\\/" }, + { "\"\\\\\"", "\\" }, { "\"\\\"\"", "\"" }, { "\"hello world \\\"embedded string\\\"\"", "hello world \"embedded string\"" },
@@ -49,11 +56,14 @@ START_TEST(escaped_string)fail_unless (qobject_type (obj) ==QTYPE_QSTRING ); str =qobject_to_qstring (obj); -fail_unless (strcmp (qstring_get_str (str), test_cases[i].decoded) ==0 ); +fail_unless (strcmp (qstring_get_str (str), test_cases[i].decoded) ==0 , +"%s != %s\n" ,qstring_get_str (str), test_cases[i].decoded);if (test_cases[i].skip ==0 ) { str =qobject_to_json (obj); -fail_unless (strcmp (qstring_get_str (str), test_cases[i].encoded) ==0 ); +fail_unless (strcmp (qstring_get_str (str),test_cases[i].encoded) ==0 , +"%s != %s\n" ,qstring_get_str (str), + test_cases[i].encoded);qobject_decref (obj); }
Signature · 1 line
1.7.1.251.g92a7
PATCH 5/10 json-lexer: Drop 'buf'
In reply to Luiz CapitulinoQString supports adding a single char, 'buf' is unneeded.
--- json-lexer.c | 7 +------ 1 files changed, 1 insertions(+), 6 deletions(-)
json-lexer.c +1 -6
@@ -284,8 +284,6 @@ void json_lexer_init(JSONLexer *lexer, JSONLexerEmitter func)static int json_lexer_feed_char (JSONLexer *lexer,char ch) { -char buf[2 ]; - lexer->x++;if (ch =='\n' ) { lexer->x =0 ;
@@ -313,10 +311,7 @@ static int json_lexer_feed_char(JSONLexer *lexer, char ch)break ; } - buf[0 ] = ch; - buf[1 ] =0 ; - -qstring_append (lexer->token, buf); +qstring_append_chr (lexer->token, ch);return 0 ; }
Signature · 1 line
1.7.1.251.g92a7
PATCH 6/10 json-streamer: Don't use qdict_put_obj()
In reply to Luiz CapitulinoIt's not needed, use qobject_put() instead and get a cleaner code.
--- json-streamer.c | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)
json-streamer.c +4 -4
@@ -43,11 +43,11 @@ static void json_message_process_token(JSONLexer *lexer, QString *token, JSONTok } dict =qdict_new (); -qdict_put_obj (dict,"type" ,QOBJECT (qint_from_int (type))); +qdict_put (dict,"type" ,qint_from_int (type));QINCREF (token); -qdict_put_obj (dict,"token" ,QOBJECT (token)); -qdict_put_obj (dict,"x" ,QOBJECT (qint_from_int (x))); -qdict_put_obj (dict,"y" ,QOBJECT (qint_from_int (y))); +qdict_put (dict,"token" , token); +qdict_put (dict,"x" ,qint_from_int (x)); +qdict_put (dict,"y" ,qint_from_int (y));qlist_append (parser->tokens, dict);
Signature · 1 line
1.7.1.251.g92a7
PATCH 7/10 add some tests for invalid JSON
In reply to Luiz CapitulinoFrom: Paolo Bonzini <[email protected]>
--- check-qjson.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 97 insertions(+), 1 deletions(-)
check-qjson.c +97 -1
@@ -638,11 +638,90 @@ START_TEST(simple_varargs) }END_TEST +START_TEST (empty_input ) +{ +QObject *obj =qobject_from_json ("" ); +fail_unless (obj ==NULL ); +} +END_TEST + +START_TEST (unterminated_string ) +{ +QObject *obj =qobject_from_json ("\"abc" ); +fail_unless (obj ==NULL ); +} +END_TEST + +START_TEST (unterminated_sq_string ) +{ +QObject *obj =qobject_from_json ("'abc" ); +fail_unless (obj ==NULL ); +} +END_TEST + +START_TEST (unterminated_escape ) +{ +QObject *obj =qobject_from_json ("\"abc\\\"" ); +fail_unless (obj ==NULL ); +} +END_TEST + +START_TEST (unterminated_array ) +{ +QObject *obj =qobject_from_json ("[32" ); +fail_unless (obj ==NULL ); +} +END_TEST + +START_TEST (unterminated_array_comma ) +{ +QObject *obj =qobject_from_json ("[32," ); +fail_unless (obj ==NULL ); +} +END_TEST + +START_TEST (invalid_array_comma ) +{ +QObject *obj =qobject_from_json ("[32,}" ); +fail_unless (obj ==NULL ); +} +END_TEST + +START_TEST (unterminated_dict ) +{ +QObject *obj =qobject_from_json ("{'abc':32" ); +fail_unless (obj ==NULL ); +} +END_TEST + +START_TEST (unterminated_dict_comma ) +{ +QObject *obj =qobject_from_json ("{'abc':32," ); +fail_unless (obj ==NULL ); +} +END_TEST + +#if 0 +START_TEST (invalid_dict_comma) +{ +QObject *obj =qobject_from_json ("{'abc':32,}" ); +fail_unless (obj ==NULL ); +} +END_TEST + +START_TEST (unterminated_literal ) +{ +QObject *obj =qobject_from_json ("nul" ); +fail_unless (obj ==NULL ); +} +END_TEST +#endif +static Suite *qjson_suite (void ) {Suite *suite;TCase *string_literals, *number_literals, *keyword_literals; -TCase *dicts, *lists, *whitespace, *varargs; +TCase *dicts, *lists, *whitespace, *varargs, *errors; string_literals =tcase_create ("String Literals" );tcase_add_test (string_literals, simple_string);
@@ -668,6 +747,22 @@ static Suite *qjson_suite(void) varargs =tcase_create ("Varargs" );tcase_add_test (varargs, simple_varargs); + errors =tcase_create ("Invalid JSON" ); +tcase_add_test (errors, empty_input); +tcase_add_test (errors, unterminated_string); +tcase_add_test (errors, unterminated_escape); +tcase_add_test (errors, unterminated_sq_string); +tcase_add_test (errors, unterminated_array); +tcase_add_test (errors, unterminated_array_comma); +tcase_add_test (errors, invalid_array_comma); +tcase_add_test (errors, unterminated_dict); +tcase_add_test (errors, unterminated_dict_comma); +#if 0 +/* FIXME: this print parse error messages on stderr. */ +tcase_add_test (errors, invalid_dict_comma); +tcase_add_test (errors, unterminated_literal); +#endif + suite =suite_create ("QJSON test-suite" );suite_add_tcase (suite, string_literals);suite_add_tcase (suite, number_literals);
@@ -676,6 +771,7 @@ static Suite *qjson_suite(void)suite_add_tcase (suite, lists);suite_add_tcase (suite, whitespace);suite_add_tcase (suite, varargs); +suite_add_tcase (suite, errors);return suite; }
Signature · 1 line
1.7.1.251.g92a7
PATCH 8/10 implement optional lookahead in json lexer
In reply to Luiz CapitulinoFrom: Paolo Bonzini <[email protected]>
Not requiring one extra character when lookahead is not necessary ensures that clients behave properly even if they, for example, send QMP requests without a trailing newline.
--- json-lexer.c | 58 +++++++++++++++++++++++++++++++++++----------------------- 1 files changed, 35 insertions(+), 23 deletions(-)
json-lexer.c +35 -23
@@ -65,6 +65,12 @@ enum json_lexer_state {#define TERMINAL (state) [0 ... 0x7F] = (state) +/* Return whether TERMINAL is a terminal state and the transition to it +from OLD_STATE required lookahead. This happens whenever the table +below uses the TERMINAL macro. */ +#define TERMINAL_NEEDED_LOOKAHEAD (old_state, terminal) \ + (json_lexer[(old_state)][0] == (terminal)) +static const uint8_t json_lexer[][256 ] = { [IN_DONE_STRING ] = {TERMINAL (JSON_STRING ),
@@ -284,35 +290,41 @@ void json_lexer_init(JSONLexer *lexer, JSONLexerEmitter func)static int json_lexer_feed_char (JSONLexer *lexer,char ch) { +int char_consumed, new_state; + lexer->x++;if (ch =='\n' ) { lexer->x =0 ; lexer->y++; } - lexer->state = json_lexer[lexer->state][(uint8_t )ch]; - -switch (lexer->state) { -case JSON_OPERATOR : -case JSON_ESCAPE : -case JSON_INTEGER : -case JSON_FLOAT : -case JSON_KEYWORD : -case JSON_STRING : - lexer->emit (lexer, lexer->token, lexer->state, lexer->x, lexer->y); -case JSON_SKIP : - lexer->state = json_lexer[IN_START ][(uint8_t )ch]; -QDECREF (lexer->token); - lexer->token =qstring_new (); -break ; -case ERROR : -return -EINVAL ; -default : -break ; - } - -qstring_append_chr (lexer->token, ch); +do { + new_state = json_lexer[lexer->state][(uint8_t )ch]; + char_consumed = !TERMINAL_NEEDED_LOOKAHEAD (lexer->state, new_state); +if (char_consumed) { +qstring_append_chr (lexer->token, ch); + } +switch (new_state) { +case JSON_OPERATOR : +case JSON_ESCAPE : +case JSON_INTEGER : +case JSON_FLOAT : +case JSON_KEYWORD : +case JSON_STRING : + lexer->emit (lexer, lexer->token, new_state, lexer->x, lexer->y); +case JSON_SKIP : +QDECREF (lexer->token); + lexer->token =qstring_new (); + new_state =IN_START ; +break ; +case ERROR : +return -EINVAL ; +default : +break ; + } + lexer->state = new_state; + }while (!char_consumed);return 0 ; }
@@ -334,7 +346,7 @@ int json_lexer_feed(JSONLexer *lexer, const char *buffer, size_t size)int json_lexer_flush (JSONLexer *lexer) { -return json_lexer_feed_char (lexer,0 ); +return lexer->state ==IN_START ?0 :json_lexer_feed_char (lexer,0 ); }void json_lexer_destroy (JSONLexer *lexer)
Signature · 1 line
1.7.1.251.g92a7
PATCH 9/10 remove unnecessary lookaheads
In reply to Luiz CapitulinoFrom: Paolo Bonzini <[email protected]>
--- json-lexer.c | 48 ++++++++++++++++-------------------------------- 1 files changed, 16 insertions(+), 32 deletions(-)
json-lexer.c +16 -32
@@ -29,7 +29,6 @@enum json_lexer_state {ERROR =0 , -IN_DONE_STRING ,IN_DQ_UCODE3 ,IN_DQ_UCODE2 ,IN_DQ_UCODE1 ,
@@ -57,9 +56,7 @@ enum json_lexer_state {IN_ESCAPE_I ,IN_ESCAPE_I6 ,IN_ESCAPE_I64 , -IN_ESCAPE_DONE ,IN_WHITESPACE , -IN_OPERATOR_DONE , IN_START, };
@@ -72,10 +69,6 @@ enum json_lexer_state { (json_lexer[(old_state)][0] == (terminal))static const uint8_t json_lexer[][256 ] = { - [IN_DONE_STRING ] = { -TERMINAL (JSON_STRING ), - }, -/* double quote string */ [IN_DQ_UCODE3 ] = { ['0' ...'9' ] =IN_DQ_STRING ,
@@ -112,7 +105,7 @@ static const uint8_t json_lexer[][256] = { [IN_DQ_STRING] = { [1 ... 0xFF] = IN_DQ_STRING, ['\\'] = IN_DQ_STRING_ESCAPE, - ['"'] = IN_DONE_STRING, + ['"'] = JSON_STRING, },/* single quote string */
@@ -151,7 +144,7 @@ static const uint8_t json_lexer[][256] = { [IN_SQ_STRING] = { [1 ... 0xFF] = IN_SQ_STRING, ['\\'] = IN_SQ_STRING_ESCAPE, - ['\''] = IN_DONE_STRING, + ['\''] = JSON_STRING, },/* Zero */
@@ -217,27 +210,18 @@ static const uint8_t json_lexer[][256] = { ['\n' ] =IN_WHITESPACE , }, - /* operator */ - [IN_OPERATOR_DONE] = { -TERMINAL (JSON_OPERATOR ), - }, - /* escape */ - [IN_ESCAPE_DONE ] = { -TERMINAL (JSON_ESCAPE ), - }, - [IN_ESCAPE_LL] = { - ['d' ] =IN_ESCAPE_DONE , + ['d' ] =JSON_ESCAPE , }, [IN_ESCAPE_L ] = { - ['d' ] =IN_ESCAPE_DONE , + ['d' ] =JSON_ESCAPE , ['l' ] =IN_ESCAPE_LL , }, [IN_ESCAPE_I64 ] = { - ['d' ] =IN_ESCAPE_DONE , + ['d' ] =JSON_ESCAPE , }, [IN_ESCAPE_I6 ] = {
@@ -249,11 +233,11 @@ static const uint8_t json_lexer[][256] = { }, [IN_ESCAPE] = { - ['d'] = IN_ESCAPE_DONE, - ['i'] = IN_ESCAPE_DONE, - ['p'] = IN_ESCAPE_DONE, - ['s'] = IN_ESCAPE_DONE, - ['f'] = IN_ESCAPE_DONE, + ['d'] = JSON_ESCAPE, + ['i'] = JSON_ESCAPE, + ['p'] = JSON_ESCAPE, + ['s'] = JSON_ESCAPE, + ['f'] = JSON_ESCAPE, ['l'] = IN_ESCAPE_L, ['I'] = IN_ESCAPE_I, },
@@ -265,12 +249,12 @@ static const uint8_t json_lexer[][256] = { ['0'] = IN_ZERO, ['1' ... '9'] = IN_NONZERO_NUMBER, ['-'] = IN_NEG_NONZERO_NUMBER, - ['{'] = IN_OPERATOR_DONE, - ['}'] = IN_OPERATOR_DONE, - ['['] = IN_OPERATOR_DONE, - [']'] = IN_OPERATOR_DONE, - [','] = IN_OPERATOR_DONE, - [':'] = IN_OPERATOR_DONE, + ['{'] = JSON_OPERATOR, + ['}'] = JSON_OPERATOR, + ['['] = JSON_OPERATOR, + [']'] = JSON_OPERATOR, + [','] = JSON_OPERATOR, + [':'] = JSON_OPERATOR, ['a' ... 'z'] = IN_KEYWORD, ['%'] = IN_ESCAPE, [' '] = IN_WHITESPACE,
Signature · 1 line
1.7.1.251.g92a7
PATCH 10/10 hxtool: Fix line number reporting on SQMP/EQMP errors
In reply to Luiz CapitulinoFrom: Jan Kiszka <[email protected]>
--- hxtool | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
hxtool +2 -0
@@ -59,6 +59,7 @@ hxtoqmp() { IFS= flag=0 + line=1 while read -r str; do case "$str" in HXCOMM*)
@@ -87,6 +88,7 @@ hxtoqmp() test $flag -eq 1 && echo "$str" ;; esac + line=$((line+1)) done }
Signature · 1 line
1.7.1.251.g92a7
Quoted 10 lines · Luiz Capitulino
Hi Anthony,
The following QMP/Monitor patches have been sent to the list and look good
to me. I have also tested most of them.The changes (since
0e2029a063405091ee34170ef71aa321715e4357) are available in
the following repository:
git://repo.or.cz/qemu/qmp-unstable.gitfor-anthony
Pulled. Thanks.
Regards,
Anthony Liguori
Quoted 26 lines
Jan Kiszka (1):
hxtool: Fix line number reporting on SQMP/EQMP errorsLuiz Capitulino (6):
json-lexer: Initialize 'x' and 'y'
json-lexer: Handle missing escapes
qjson: Handle "\f"
check-qjson: Add more escape tests
json-lexer: Drop 'buf'
json-streamer: Don't useqdict_put_obj()Paolo Bonzini (3):
add some tests for invalid JSON
implement optional lookahead in json lexer
remove unnecessary lookaheadscheck-qjson.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++- hxtool | 2 + json-lexer.c | 114 ++++++++++++++++++++++++++---------------------------- json-parser.c | 4 ++ json-streamer.c | 8 ++-- qjson.c | 3 + 6 files changed, 177 insertions(+), 66 deletions(-)