For those who haven't seen it, CCAN [1] is a great collection of reusable C code, much of which specifically exists to work around the kinds of issues mentioned in OP. It turns “oh crap I should probably roll my own” into “wait I bet someone has already re-rolled this”.

[1] https://ccodearchive.net/

Please note that the above link has been claimed by squatters and isn’t the right link for CCAN anymore! The maintainer suggests [1] just using the GitHub repo [2] instead.

[1] https://lists.ozlabs.org/pipermail/ccan/2022-September/00141...

[2] https://github.com/rustyrussell/ccan/