git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] fix: check parameters in json-write.c

From
JHJeff Hostetler <git@jeffhostetler.com>
Date
Sep 20, 2023, 20:02 UTC
Message-ID
<dc45106c-d569-3438-d2ff-c3c94b6161d7@jeffhostetler.com>
In-Reply-To
<ZQne3ThSw6HVmNJc@nand.local>
On 9/19/23 1:48 PM, Taylor Blau wrote:
Show 35 quoted lines
> [+cc Jeff Hostetler]
> 
> On Tue, Sep 19, 2023 at 11:54:58AM +0000, mark via GitGitGadget wrote:
>> diff --git a/json-writer.c b/json-writer.c
>> index 005c820aa42..23ba7046e5d 100644
>> --- a/json-writer.c
>> +++ b/json-writer.c
>> @@ -20,6 +20,11 @@ static void append_quoted_string(struct strbuf *out, const char *in)
>>   {
>>   	unsigned char c;
>>
>> +	if (!in || !*in) {
>> +		strbuf_addstr(out, "\"\"");
>> +		return;
>> +	}
> 
>  From reading the implementation of append_quoted_string(), I think that
> the case where "in" is the empty string is already covered. IOW, doing
> something like:
> 
>      struct strbuf buf = STRBUF_INIT;
>      append_quoted_string(&out, "");
>      warning("'%s'", buf.buf);
> 
> would print out something like:
> 
>      warning: '""'
> 
> as expected. Handling a NULL "in" argument is new behavior, but I am not
> sure if it is appropriate to coerce a NULL input into the empty string.
> I've CC'd the author of this code, whose opinion I trust more than my
> own here.
> 
> Thanks,
> Taylor

There are three callers of `append_quoted_string()` and it is static to the json-writer.c code.

Basically, in a JSON object, we have 2 uses:
     {
         "<key>" : "<string-value>",
         "<key>" : <integer>,
         ...
     }
And in a JSON array, we have the other:
     [
         "<string-value>",
         ...
     ]

I suppose it is OK for the 2 string-value cases to assume a NULL pointer could be written as "" in the JSON output. Although, I kinda think a NULL pointer should call BUG() as we have in the various assert_*() routines. It really is a kind of logic error in the caller.

Regardless what we decide for the <string-value> case, in the <key> case, the resulting JSON would not be valid. We need for the key to be a non-empty string. For example { "" : 1 } is not valid JSON. So the key case should call BUG() and not try to hide it.

So I'm leaning towards just making it a BUG() in all cases, but I'm open to the other mixed handling.

Jeff
Previous: Taylor BlauNext: Junio C Hamano
Message 3 of 4 in “fix: check parameters in json-write.c”
  1. fix: check parameters in json-write.cmark via GitGitGadget, Sep 19, 2023
  2. Taylor BlauSep 19, 2023
  3. Jeff HostetlerSep 20, 2023
  4. Junio C HamanoSep 20, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.