Skip to content

json-rpc: translate NULL into "null" instead of "(null)". #1118

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

Merged
merged 3 commits into from
Feb 28, 2018

Conversation

bitonic-cjp
Copy link
Contributor

It turns out that #957 is insufficient to solve #908 . This fix is needed to really say "id": null instead of "id": (null).

This is probably an incomplete fix: it would be better to make sure that all cases of NULL are translated into "null". However, I didn't want to touch the tal_fmt implementation. What would be a good approach? make a json_fmt wrapper around tal_fmt to do this? I didn't do this yet: since I'm not too familiar with the code base yet, I didn't want to make too large changes without any guidance.

@ZmnSCPxj
Copy link
Contributor

tal_fmt is a wrapper around vsnprintf, and if you want to be pedantic, a "%s" format string should not be given a NULL as that will lead to undefined behavior. It is simply that the behavior in glibc is to print "(null)" in that case; we cannot rely on all systems doing that.

@ZmnSCPxj
Copy link
Contributor

I believe this is the point which we should modify:

json_command_malformed(
jcon, NULL,
"Invalid token in json input");

It should be json_command_malformed(jcon, "null", "Invalid token in json input");

Then inside both connection_complete_error and connection_complete_ok we should have assert(id != NULL) to catch this in the future.

@bitonic-cjp
Copy link
Contributor Author

Agreed: that seems to be somewhat less ugly than what I made.

Could we somehow ensure that NULL is never passed to tal_fmt through another route? I find it scary to have stumbled upon something that is actually undefined behavior.

@ZmnSCPxj
Copy link
Contributor

Wait for @practicalswift to do regular undefined behavior checking has always been my personal technique, hahaha.

@rustyrussell
Copy link
Contributor

Was about to suggest the same as @ZmnSCPxj : let's kill the root cause, and assert() that it never happens again.

@ZmnSCPxj
Copy link
Contributor

On a more serious note, @bitonic-cjp: undefined behavior is mostly caught by code review and sometimes by automated tools that scan for them, and valgrind can catch some bits of undefined behavior related to usage of memory. @practicalswift might have a tool or two that scans for undefined behavior (and has requested pulls to add such linting to the python parts of our codebase, for instance). I have caught a few bugs myself (e.g. subdaemons blocking SIGCHLD when they should be blocking SIGPIPE, waitanyinvoice blocking even with an invoice to report or reporting an invoice twice) simply from code review. So mostly.... with enough eyes, all bugs are shallow. Hopefully with more people supporting development we can get more bugs found and fixed.

…ver receive NULL arguments. Pass "null" instead, where needed.
@bitonic-cjp
Copy link
Contributor Author

I did what ZmnSCPxj said; now I'm checking other tal_fmt calls.

@ZmnSCPxj
Copy link
Contributor

ACK 59b7f9e

@@ -263,6 +263,8 @@ static void connection_complete_ok(struct json_connection *jcon,
const char *id,
const struct json_result *result)
{
assert(id != NULL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Make sure the indentation matches the surrounding code. Same below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed; I already discovered this myself, and was going to fix it.

@cdecker
Copy link
Member

cdecker commented Feb 28, 2018

ACK 500db24

@cdecker cdecker merged commit f32ebb7 into ElementsProject:master Feb 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants