-
Notifications
You must be signed in to change notification settings - Fork 8k
json: fix parsing first array-array element #36340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, clearly this needs a commit message to explain what's going on. It's a bug with parsing nested arrays, I guess? Just write up an example of a mis-parsed input string and a quick description of how the fix works. I frankly don't understand how this works at all.
Beyond that, some nitpicking about the code.
tests/lib/json/src/main.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure on our stance on utf-8 embedded in C code, but I know this isn't allowed per ISO C and I'm reasonably certain one or more of the various coding standards we try to adopt makes this illegal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you're right, this file used utf-8 before and I'm just following the existing style of the rest of the code.
If we wanna enforce ISO-C utf-8 rules we should add a CI check for it and fix the whole zephyr repo.
That would be out of scope for this PR though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gcc at least defaults to UTF-8 as the input and string literal encoding. I don't know how this support is in other compilers. But, in this case, this is merely adding an additional test case that is similar to others. Fixing the UTF-8 aspect would be a separate effort.
lib/os/json.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not if (val) {
here and skip the changes above? Seems like this is just adding code for no reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO it's more natural to check the pointer you're about to write to for NULL instead of some other variable.
Also, the hope is that it prevents bugs caused by future changes, e.g. if element doesn't rely on var
anymore.
lib/os/json.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't pass the string in val down to decode_value(), how does it have anything to parse? Not understanding how this works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here, val
was just passed through from the argument to this function.
val
is just a pointer to the start of the parent struct so arr_parse
can do offset calculations.
arr_parse
is the only place where this value is actually being used and everyone else just passed it through.
You answered my nitpicks, but not the core question: what does this actually fix? What were we parsing wrong before, and why? |
tests/lib/json/src/main.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gcc at least defaults to UTF-8 as the input and string literal encoding. I don't know how this support is in other compilers. But, in this case, this is merely adding an additional test case that is similar to others. Fixing the UTF-8 aspect would be a separate effort.
include/data/json.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@d3zd3z Should I do this to fix the compliance check? That looks awkward but it works.
.n_elements = \ | |
1 + ZERO_OR_COMPILE_ERROR(elem_descr_len_ == 1), \ | |
.n_elements = \ | |
1 + \ | |
ZERO_OR_COMPILE_ERROR( \ | |
elem_descr_len_ == 1 \ | |
), \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's probably reasonable. This code is kind of overly deep anyway, but we're not about fixing that aspect.
0a8bf1d
to
23d70f7
Compare
Previously, the first element inside the array-array didn't contain the decoded data. Signed-off-by: Michael Zimmermann <[email protected]>
23d70f7
to
b727869
Compare
No description provided.