Skip to content

libutil: fix getdelim error handling, JSON OOM propagation, and raw terminal check - #605

Merged
millert merged 4 commits into
sudo-project:mainfrom
fQwQf:libutil-getdelim-json-term
Nov 26, 2026
Merged

millert merged 4 commits into
sudo-project:mainfrom
fQwQf:libutil-getdelim-json-term

Conversation

@fQwQf

@fQwQf fQwQf commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Three small bugfixes in libsudo_util:

  1. sudo_getdelim(): preserve the cursor across reallocarray() and restore bytes properly

    • cp - *buf was evaluated after reallocarray(*buf, ...), referencing pointers into the previous (potentially freed) allocation. Save the offset before reallocating.
    • On the error path, ungetc(*cp--, fp) pushed back the unwritten byte at *cp, never pushed back (*buf)[0], and omitted an (unsigned char) cast. Use ungetc((unsigned char)*--cp, fp) so all read bytes are pushed back in reverse order.
  2. JSON: propagate container name allocation failures

    • sudo_json_open_object_v1() and sudo_json_open_array_v1() ignored the return value of json_append_string(). On allocation failure they could emit a truncated :{ or :[ and return true. Check json_append_string() before appending the delimiter, matching the value-append helpers.
  3. sudo_term_raw_v1(): compare the requested attributes with orig_term

    • sudo_term_raw_v1() previously called sudo_term_is_raw_int(&term) on a zero-initialized term (c_cc[VMIN] was 0, not 1) before copying orig_term, making the early return dead code. Compute the requested raw termios from orig_term first (applying SUDO_TERM_ISIG and SUDO_TERM_OFLAG), and skip tcsetattr_nobg() only when !changed and all resulting c_*flag / c_cc fields already match orig_term.

Testing: full build and make check pass (json_test: 263 tests, 0 errors; sudo_getdelim() error pushback and sudo_term_raw_v1() verified by code inspection alongside the full suite).

…order

Compute the buffer offset before calling reallocarray(), and push
already-read bytes back with ungetc((unsigned char)*--cp, fp).
Check the return value of json_append_string() in
sudo_json_open_object_v1() and sudo_json_open_array_v1().
Compute the desired raw termios from orig_term first and compare all
requested attributes against orig_term before skipping tcsetattr_nobg().
Comment thread lib/util/getdelim.c Resolved
Comment thread lib/util/term.c Resolved
Make the saved offset const in sudo_getdelim(), and remove the replacement attribute comparison in sudo_term_raw_v1() so the dead sudo_term_is_raw_int() check is simply deleted without altering existing behavior.
@fQwQf

fQwQf commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Done. Thanks for the review!

@millert
millert merged commit bf29939 into sudo-project:main Nov 26, 2026
5 checks passed
@fQwQf
fQwQf deleted the libutil-getdelim-json-term branch November 27, 2026 00:12
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.

2 participants