[chbot] Weirdness in parameter interpretation in C lib function

Volker Kuhlmann list57 at top.geek.nz
Tue Jul 30 04:56:45 BST 2024


On Tue 30 Jul 2024 10:26:22 NZST +1200, Robin Gilks wrote:

> Here is a bit of sample code to illustrate:

The second arg to snprintf is documented as "most bytes written, incl
\0". You're meant to supply your buffer size. Compared with sprintf
snprintf is safe because it will not write past the end of the buffer
(assuming you supply correct arguments).

It's all a bit insane because of the history of C. Sane would be to
determine the buffer size automatically and to never write after that.

If the number of characters that would be written exceeds the number
supplied in arg 2 of snprintf it is unclear tome from the docs whether
the buffer is zero terminated (another insanity).

Volker

-- 
Volker Kuhlmann
http://volker.top.geek.nz/	Please do not CC list postings to me.



More information about the Chchrobotics mailing list