<div dir="auto">I think there is some bogging down in worrying about whether the size parameter to snprintf is the size of the destination buffer or a restriction of what is printed. However for what the warning is telling you it being a restriction on what is printed is sufficient to provide the truncation warning.<div dir="auto"><br></div><div dir="auto">As far as snprintf is concerned the size parameter may as well be the size of the output buffer as it is not allowed to write more than that.<div dir="auto"><br></div><div dir="auto">The warning is telling you that you told snprintf to write at most 30 characters to buf but if your ssid array contains a long enough string then snprintf will produce truncated output as it will stop as soon as it would have to write 30 characters into buf.</div><div dir="auto"><br></div><div dir="auto">I would also note that truncation can be an unintended consequence. Use of snprintf merely avoids unintended memory write related consequences.</div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, 31 Jul 2024, 09:37 Charles Manning, <<a href="mailto:cdhmanning@gmail.com" rel="noreferrer noreferrer" target="_blank">cdhmanning@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>There is a solid reason to use snprintf rather than sprintf.</div><div><br></div><div>snprintf will prevent writing off the end of the buffer.<br><br></div><div>char str[5];</div><div><br></div><div>sprintf(str,"%s", "This is a long string which will overwrite the end of the buffer and cause unintended consequences");</div><div>snprints(str, sizeof(str),"%s", "This is a long string but it will be truncated to fit in the buffer so no unintended consequences");<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jul 31, 2024 at 7:16 AM Simeon Pilgrim <<a href="mailto:simeon.pilgrim@gmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">simeon.pilgrim@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto"><div dir="ltr"></div><div dir="ltr">Ok, good point, if we ignore the history of the language. Ignore the warning and what itis pointing. Heck let ignore the fact you read a terse man page.</div><div dir="ltr"><br></div><div dir="ltr">Why are you use snprintf, instead of sprintf, and what did you want tohappen, because the all the non string saffety trick are valid formatting rules. Aka use %*s and two variables </div><div dir="ltr"><br><blockquote type="cite">On 30 Jul 2024, at 11:55 PM, Robin Gilks <<a href="mailto:gb7ipd@gmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">gb7ipd@gmail.com</a>> wrote:<br><br></blockquote></div><blockquote type="cite"><div dir="ltr"><div dir="ltr">Absolutely correct<div>As I quoted previously from the man page<br></div><i>The functions snprintf() and vsnprintf() write at most size bytes (including the terminating null byte ('\0')) to str.<br></i>Note there is no qualification of what 'size' is referring to, only that it is a restriction of what is written, nothing to indicate that it's the length of str.<div><div>That's the whole problem - sloppy specifications!</div><br></div><div>I have a standard question I always ask of people who are specifying anything: "what colour should it be?"</div><div>This avoids the exclamation from the developer one day before delivery of: "colour, colour - nobody told us it had to have a colour!!"</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jul 30, 2024 at 10:00 PM Bevin Brett <<a href="mailto:bevin_brett@hotmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">bevin_brett@hotmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>
<div dir="ltr">
<div style="text-align:left;text-indent:0px;margin:0px;font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Getting rid of the struct and the indexing, what you have is</div>
<div style="text-align:left;text-indent:0px;margin:0px;font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="text-align:left;text-indent:0px;margin:0px;font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
char ssid[30];</div>
<div style="text-align:left;text-indent:0px;margin:0px;font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
char buf[100]</div>
<div style="text-align:left;text-indent:0px;margin:0px;font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="text-align:left;text-indent:0px;margin:0px;font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
snprintf (buf, sizeof (ssid), "SSID %s\n", ssid);<br>
<br>
</div>
<div style="text-align:left;text-indent:0px;margin:0px;font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
which is a request to write 5 chars ("SSID ") followed by any number of chars ending in a NUL (perhaps overrunning the end of ssid if there are no NUL in it) followed by \n and a trailing NUL into the first upto 30 characters of buf. The warning is assuming
(incorrectly) that ssid will contain less than 30 non-NULL characters, hence decides may need upto 37 characters (should have been 36) (5 + 29 + 1 + 1) and you have only supplied 30</div>
<div style="text-align:left;text-indent:0px;margin:0px;font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="text-align:left;text-indent:0px;margin:0px;font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
/Bevin</div>
</div>
_______________________________________________<br>
Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" rel="noreferrer noreferrer noreferrer" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
<a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">http://lists.ourshack.com/pipermail/chchrobotics/</a><br>
Meetings usually 3rd Monday each month. See <a href="http://kiwibots.org" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">http://kiwibots.org</a> for venue, directions and dates.<br>
When replying, please edit your Subject line to reflect new subjects.</div></blockquote></div>
<span>_______________________________________________</span><br><span>Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" rel="noreferrer noreferrer noreferrer" target="_blank">Chchrobotics@lists.ourshack.com</a></span><br><span><a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer noreferrer noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a></span><br><span>Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="noreferrer noreferrer noreferrer" target="_blank">http://lists.ourshack.com/pipermail/chchrobotics/</a></span><br><span>Meetings usually 3rd Monday each month. See <a href="http://kiwibots.org" rel="noreferrer noreferrer noreferrer" target="_blank">http://kiwibots.org</a> for venue, directions and dates.</span><br><span>When replying, please edit your Subject line to reflect new subjects.</span></div></blockquote></div>_______________________________________________<br>
Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" rel="noreferrer noreferrer noreferrer" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
<a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">http://lists.ourshack.com/pipermail/chchrobotics/</a><br>
Meetings usually 3rd Monday each month. See <a href="http://kiwibots.org" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">http://kiwibots.org</a> for venue, directions and dates.<br>
When replying, please edit your Subject line to reflect new subjects.</blockquote></div>
_______________________________________________<br>
Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" rel="noreferrer noreferrer noreferrer" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
<a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">http://lists.ourshack.com/pipermail/chchrobotics/</a><br>
Meetings usually 3rd Monday each month. See <a href="http://kiwibots.org" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">http://kiwibots.org</a> for venue, directions and dates.<br>
When replying, please edit your Subject line to reflect new subjects.</blockquote></div>