Commit graph

19 commits

Author SHA1 Message Date
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)