Commit graph

22 commits

Author SHA1 Message Date
Pauli Virtanen 22ac5f85cc json: fix high surrogate escapes
Surrogate escapes must add not or 0x10000, as the specified bits go up
0xfffff.
2024-05-05 15:15:54 +03:00
Pauli Virtanen eea18a8a8a test: fix loading json test data
Multiline results were not loaded correctly.
2024-05-05 09:32:43 +00:00
Wim Taymans d4581755e6 spa: Improve JSON error reporting
Add struct spa_error_location that holds information about some parsing
context such as the line and column number, error and line fragment
with the error.

Make spa_json_get_error() fill in the spa_error_location instead. Add
some error codes to the error state and use this to add a parsing reason
to the location.

Add a debug function to log the error location in a nice way. Also
add a FILE based debug context to log to any FILE.

Replace pw_properties_check_string() with
pw_properties_update_string_checked() and add
pw_properties_new_string_checked(). The check string behaviour can still
be done by setting props to NULL but the main purpose is to be able to
avoid parsing the json file twice in the future.

When using the old pw_properties_update_string(), log a warning to the
log when we fail to parse the complete string.

Use the new checked functions and the debug functions to report about
parsing errors in the tools and conf parsing.

This gives errors like:

```
> pw-loopback --playback-props '{ foo =  [ f : g ] }'
error: syntax error in --playback-props: Invalid array separator
line:      1 | { foo =  [ f : g ] }
col:      14 |              ^
```
2024-03-27 15:42:29 +01:00
Wim Taymans 96fb63dfa1 test: fix some compiler warnings 2024-03-26 10:23:37 +01:00
Pauli Virtanen 3775e4e624 spa: json: check object key-value pair parity during parsing
Check each object key is associated with value.  Disallow object or
array valued keys.

Add flag tracking whether the parser is at global top-level or not, as
there we may either be in object context or in a single-value context.

Save depth=0 array flag bit in state, so that spa_json_next preserves
its complete state across calls. The higher-depth flag bits can be in
temporary stack as they are not needed across calls.
2024-03-25 20:49:25 +02:00
Pauli Virtanen 921c8b99db spa: json: allow only ascii in bare values
Control characters probably are an error. We also are not validating any
utf8 here, so disallow bare utf8 too --- one likely should use strings
for such content anyway as spaces are not allowed otherwise.
2024-03-25 20:49:25 +02:00
Pauli Virtanen e63e8b8a37 test: add "strict" JSON tests
Adapt an existing JSON parsing test suite from
https://github.com/nst/JSONTestSuite for SPA JSON.
2024-03-25 20:49:25 +02:00
Pauli Virtanen 31e5823010 spa: json: more parsing errors & add spa_json_get_error
Disallow = and : as bare items in [] containers, as that likely is
"[ { foo = bar } ]" mistyped as "[ foo = bar ]".

Disallow nesting errors, eg. "[ foo bar" or "[ foo bar }".

Fix handling of ", \ and # in bare strings.

Fix ignoring trailing comments.

Add a fixed-size stack (128 levels) to the tokenizer, so that it can
check these at levels below its depth.

When the tokenizer encounters an error, make it and its parents enter
error state where no further input will be processed. This allows caller
to check for parse errors later as convenient.

The error state can be queried using spa_json_get_error, which also
looks up the error line/column position.
2024-03-25 20:49:25 +02:00
Pauli Virtanen 41d7762f8b spa: json: fix string function usage in spa_json_parse_float/int
spa_json_parse_float/int receive non nul-terminated string, so calling
string functions assuming nul-termination is invalid.

Fix by copying data to a buffer before doing parsing.
2024-03-25 19:22:57 +02:00
Barnabás Pőcze 436787d6ba treewide: try to use const char * for string literals 2023-09-16 17:47:35 +02:00
Barnabás Pőcze 934ab3036e treewide: use SPDX tags to specify copyright information
SPDX tags make the licensing information easy to understand and clear,
and they are machine parseable.

See https://spdx.dev for more information.
2023-02-16 10:54:48 +00:00
Wim Taymans dedbf39ab5 tests: whitespace is not allowed in number either 2022-07-11 12:15:40 +02:00
Wim Taymans 302bad96a0 tests: add json float check unit test 2022-07-11 12:05:42 +02:00
Wim Taymans cbbc4baa3f json: avoid converting random string to floats
Only allow the json float characters when converting to float.
This avoids NAN, INF, 0xffe4 etc to be seen as a json float.
2022-07-11 11:50:58 +02:00
Wim Taymans 92e2b7dd0d json: improve unicode parsing
Make sure don't overread the strings.
Don't use sscanf but use a custom hex read function.

Fixes #2340
2022-04-27 08:37:32 +02:00
Wim Taymans 5137015c33 json: convert utf16 to utf8
We work with utf8 string so convert the unicode escape sequences to
utf8.

Fixes #2337
2022-04-26 15:38:22 +02:00
Wim Taymans f2cdb15e0c json: fix \uXXXX parsing
We need exactly 4 hex characters, everything else is refused. We
also copy those characters directly to the output string without
assuming any encoding.

See #2337
2022-04-26 11:01:33 +02:00
Wim Taymans 2b16df4e89 spa: add locale independent spa_dtoa
And use this in spa_json_format_float() where we also avoid invalid
json floats.
Use json float format in some places where we serialize json floats.
Add a unit test.

See #2223
2022-03-21 10:41:44 +01:00
Wim Taymans 93b5d440bc json: add locale independent parse and format functions
Use them in pw-dump.
Add some unit tests.

See #2223
2022-03-20 21:04:33 +01:00
Wim Taymans 947d15a60a json: empty string should not give a valid number 2022-03-20 20:37:26 +01:00
Wim Taymans 611591d0fc json: add spa_json_parse_stringn()
It also checks the destination size.
2022-01-04 12:37:00 +01:00
Peter Hutterer 7909c99ead test: convert two spa tests to pwtest 2021-06-09 18:00:39 +10:00
Renamed from spa/tests/test-json.c (Browse further)