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

Re: [PATCH 6/6] strbuf-safe: add init and release methods

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 21, 2026, 21:44 UTC
Message-ID
<xmqqld8ul1ny.fsf@gitster.g>
In-Reply-To
<dea925f31647e7c08f3fa467b8058351b463f593.1789736540.git.gitgitgadget@gmail.com>
"Derrick Stolee via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 12 quoted lines
> +int jw_release(struct json_writer *jw)
>  {
> -	strbuf_release(&jw->json);
> -	strbuf_release(&jw->open_stack);
> +	enum safe_result result = SUCCESS;
> +
> +	/* attempt both removals without short-circuiting. */
> +	result = sstrbuf_release(&jw->json) || result;
> +	result = sstrbuf_release(&jw->open_stack) || result;
> +
> +	return result;
>  }
This is puzzling in a few ways.

"enum safe_result" so far has been SUCCESS==0 and MEMORY_ERROR==1. Presumably in some future we would gain other kind of error symbols, but when that happens is this meant to act as an enumeration of different kinds errors? Or an enumeration of bitmasks that can signal different kinds of errors?

If we mean "enum safe_result" is an enumeration of different kinds of errors, then the "result" variable and the returned value from here would be able to report a *single* kind of error, and it may be common to report the first error we encounter, in which case

    enum safe_result result = SUCCESS;
    enum safe_result res;
    res = sstrbuf_release(&jw->json);
    if (!result && res)
	result = res;
    res = sstrbuf_release(&jw->open_stack);
    if (!result && res)
	result = res;
    return result;

would be slightly longer, far easier to reason about, and is a lot more futureproof. What you wrote, with "||", does not really allow anything other than "is it still zero, or coalesce any non-zero value to 1".

On the other hand, if we mean "enum safe_result" is an enumeration of bitmasks, each bit representing different kind of error, then

    enum safe_result result = 0;
    result |= sstrbuf_release(&jw->json);
    result |= sstrbuf_release(&jw->open_stack);
    return result;

would probably be what you want. That way you can add different functions that returns different bit to signal a different kind of error and or it in.

    result |= some_function();
Previous: Derrick Stolee via GitGitGadgetNext: Junio C Hamano
Message 13 of 17 in “[RFC] Create a 'safe' strbuf API”
  1. 0/6 [RFC] Create a 'safe' strbuf APIDerrick Stolee via GitGitGadget, Sep 18, 2026
  2. 1/6 strbuf: add header for 'safe' APIDerrick Stolee via GitGitGadget, Sep 18, 2026
  3. Junio C HamanoSep 21, 2026
  4. Mark C. Chu-CarrollSep 23, 2026
  5. Junio C HamanoSep 23, 2026
  6. 2/6 wrapper: initialize GIT_ALLOC_LIMIT proactivelyDerrick Stolee via GitGitGadget, Sep 18, 2026
  7. 3/6 wrapper: create safe_memory_limit_check()Derrick Stolee via GitGitGadget, Sep 18, 2026
  8. Junio C HamanoSep 21, 2026
  9. 4/6 strbuf-safe: add sstrbuf_grow()Derrick Stolee via GitGitGadget, Sep 18, 2026
  10. Junio C HamanoSep 21, 2026
  11. 5/6 json-writer: include strbuf-safe.hDerrick Stolee via GitGitGadget, Sep 18, 2026
  12. 6/6 strbuf-safe: add init and release methodsDerrick Stolee via GitGitGadget, Sep 18, 2026
  13. Junio C HamanoSep 21, 2026
  14. Junio C HamanoSep 21, 2026
  15. Phillip WoodSep 19, 2026
  16. Jeff KingSep 23, 2026
  17. Derrick StoleeOct 6, 2026

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.