Bug 658632 - unexpected behaviour when parsing invalid booleans with json_reader_get_boolean_value
unexpected behaviour when parsing invalid booleans with json_reader_get_boole...
Status: RESOLVED FIXED
Product: json-glib
Classification: Core
Component: Parser
0.12.x
Other Linux
: Normal normal
: ---
Assigned To: json-glib-maint
json-glib-maint
:
Depends on:
Blocks:
  Show dependency tree
 
Reported: 2011-09-09 07:20 UTC by Matthieu Paindavoine
Modified: 2012-03-13 22:08 UTC (History)
1 user (show)

See Also:
GNOME target: ---
GNOME version: ---


Attachments
reader: Do more strict validation (10.85 KB, patch)
2011-09-09 13:02 UTC, Emmanuele Bassi (:ebassi)
committed Details | Diff | Review

Description Matthieu Paindavoine 2011-09-09 07:20:17 UTC
When compiling and executing the following snippet, the last assert breaks (g_value_get_boolean). I would have expected the JsonReader to report an error before that.

 GValue yyy = { 0 };
 g_value_init (&yyy, G_TYPE_BOOLEAN);

 static const gchar *zzz = "{ \"value\": 3.1 }";
 parser = json_parser_new ();
 json_parser_load_from_data (parser, zzz, -1, NULL);
 reader = json_reader_new (json_parser_get_root (parser));
 json_reader_read_member (reader, "value");

 
 g_value_set_boolean (&yyy, json_reader_get_boolean_value (reader));
 if (json_reader_get_error (reader) != NULL)
 { 
   g_error ("could not read boolean value");
 }
 g_assert (G_IS_VALUE(&yyy));
 g_assert (G_VALUE_HOLDS_BOOLEAN (&yyy));
 g_assert (g_value_get_boolean (&yyy));
 g_value_unset (&yyy);
Comment 1 Emmanuele Bassi (:ebassi) 2011-09-09 12:23:21 UTC
strictly speaking, the assert is triggered because "3.1" is interpreted to a non-boolean value, and that means json_reader_get_boolean_value() will return FALSE.

JsonReader typed accessors should do more validation, though, so that json_reader_get_error() would warn.
Comment 2 Emmanuele Bassi (:ebassi) 2011-09-09 13:02:45 UTC
Created attachment 196106 [details] [review]
reader: Do more strict validation

JsonReader should perform more strict validation, and put itself in an
error state whenever the user asks for values or information on nodes
that are not there, or not of the right type.
Comment 3 Emmanuele Bassi (:ebassi) 2011-09-09 13:03:44 UTC
Attachment 196106 [details] pushed as 9f75475 - reader: Do more strict validation

this should cause your test to exit at g_error() before reaching the g_assert().
Comment 4 Matthieu Paindavoine 2011-09-09 14:55:47 UTC
(In reply to comment #3)
> Attachment 196106 [details] pushed as 9f75475 - reader: Do more strict validation
> 
> this should cause your test to exit at g_error() before reaching the
> g_assert().

I used the patch in my code and it worked flawlessly.

Wow! feels like my birthday in advance. Merci
Comment 5 André Klapper 2012-03-13 22:08:27 UTC
[Fixing Default QA assignee for json-glib - see 613232#c1. Sorry for bugmail noise.]

Note You need to log in before you can comment on or make changes to this bug.