<div dir="ltr">having existed prior to the original functions _snprintf that became the standard snprintf, sure I carry history, that perhaps does not apply. The prior problem where lots of buffer over runs. This class of function is trying to solve this, this is what the help is doing.<div><br></div><div>Perhaps some helpful doc's.<br><br><a href="https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/snprintf-snprintf-snprintf-l-snwprintf-snwprintf-l?view=msvc-170">https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/snprintf-snprintf-snprintf-l-snwprintf-snwprintf-l?view=msvc-170</a><br><br>but at the same time, you did ask "why is it helping me" and I told you why. it all holds. if you want to learn what the help means or not, and where the warnings are coming from... do as you please.</div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, 30 Jul 2024 at 11:21, Robin Gilks <<a href="mailto:gb7ipd@gmail.com">gb7ipd@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="ltr"><div>You make the same assumption as the library coder - that 'size' is the length of the output buffer. The man page says otherwise</div><div><br></div><div><i>The functions snprintf() and vsnprintf() write at most size bytes (including the terminating null byte ('\0')) to str.</i></div><div><i><br></i></div><div><i>This is defining the amount written, not the maximum the outbuffer can hold - nowhere does it say that 'size' is the length of 'str'<br></i></div><div><i><br></i></div></div><br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jul 30, 2024 at 11:12 AM Simeon Pilgrim <<a href="mailto:simeon.pilgrim@gmail.com" 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="ltr">```<br>char* test(uint8_t index)<br>{<br>#define DEST_BUF_SIZE 100<br><br> if (index < NUMRESULTS) {<br> static char buf[DEST_BUF_SIZE];<br><br> snprintf(buf, DEST_BUF_SIZE, "SSID %s\n", scandata[index].ssid);<br> return buf;<br> }<br> return "INVALID INDEX";<br>}<br>```</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, 30 Jul 2024 at 11:05, Simeon Pilgrim <<a href="mailto:simeon.pilgrim@gmail.com" 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="ltr">you have provided a pointer (to memory), AND you have declared it 30 bytes long. This is what YOU HAVE done. The compiler is HELPING YOU by pointing out 37 > 30, which it is.<div><br></div><div>If you want the compiler to help you correctly, tell the compiler the length of the data you have, which is 100.</div><div><br></div><div><br><div><br></div><div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, 30 Jul 2024 at 10:58, Robin Gilks <<a href="mailto:gb7ipd@gmail.com" target="_blank">gb7ipd@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">But I've provided 100 bytes, not 30!!<br>
<br>
On Tue, Jul 30, 2024 at 10:40 AM Simeon Pilgrim<br>
<<a href="mailto:simeon.pilgrim@gmail.com" target="_blank">simeon.pilgrim@gmail.com</a>> wrote:<br>
><br>
> the second parameter should be the size of the destination buffer, thus the code should be:<br>
><br>
> snprintf (buf, 100, "SSID %s\n",<br>
><br>
> or<br>
><br>
> snprintf (buf, sizeof(buf), "SSID %s\n",<br>
><br>
> your format string is 7 tokens (6 + zero termination) + the 30 byte string ssid, which is larger than 30 you have provided.<br>
><br>
> The snprintf functions are "safe truncation" functions, not handy dandy truncation utilities, thus the warning that you might have truncation.<br>
><br>
> On Tue, 30 Jul 2024 at 10:27, Robin Gilks <<a href="mailto:gb7ipd@gmail.com" target="_blank">gb7ipd@gmail.com</a>> wrote:<br>
>><br>
>> Since there are a few C programmers in the group I thought I'd ask<br>
>> this (interesting?) question<br>
>><br>
>> Here is a bit of sample code to illustrate:<br>
>><br>
>> ----------------------------------------------------------------------------<br>
>> #include <stdio.h><br>
>> #include <stdint.h><br>
>><br>
>><br>
>> typedef struct<br>
>> {<br>
>> char ssid[30];<br>
>> int8_t rssi;<br>
>> } ScanResult;<br>
>><br>
>> #define NUMRESULTS 50 // how many tracked simultaneously<br>
>><br>
>> static ScanResult scandata[NUMRESULTS];<br>
>><br>
>> void<br>
>> main (void)<br>
>> {<br>
>> }<br>
>><br>
>><br>
>> char * test (uint8_t index)<br>
>> {<br>
>> static char buf[100];<br>
>><br>
>> snprintf (buf, sizeof (scandata[index].ssid), "SSID %s\n",<br>
>> scandata[index].ssid);<br>
>> return buf;<br>
>><br>
>> }<br>
>> ----------------------------------------------------------------------------<br>
>> Save as test.c; compile with gcc test.c<br>
>><br>
>> The warning indicates that the destination buffer may be too small<br>
>><br>
>> test.c: In function ‘test’:<br>
>> test.c:25:56: warning: ‘%s’ directive output may be truncated writing<br>
>> up to 29 bytes into a region of size 25 [-Wformat-truncation=]<br>
>> 25 | snprintf (buf, sizeof (scandata[index].ssid), "SSID %s\n",<br>
>> scandata[index].ssid);<br>
>> | ^~<br>
>> test.c:25:4: note: ‘snprintf’ output between 7 and 36 bytes into a<br>
>> destination of size 30<br>
>> 25 | snprintf (buf, sizeof (scandata[index].ssid), "SSID %s\n",<br>
>> scandata[index].ssid);<br>
>> |<br>
>><br>
>> This appear to be treating the size parameter in the snprintf as being<br>
>> the size of the output buffer 'buf' (which is 100 bytes long) but<br>
>> surely it should be applying the restriction on the format string that<br>
>> includes scandata[index].ssid (which is 30 bytes long)<br>
>><br>
>> Am I just having brain fade or is that just fundamentally wrong?<br>
>><br>
>> --<br>
>> Robin Gilks<br>
>><br>
>> _______________________________________________<br>
>> Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
>> <a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
>> Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="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" target="_blank">http://kiwibots.org</a> for venue, directions and dates.<br>
>> When replying, please edit your Subject line to reflect new subjects.<br>
><br>
> _______________________________________________<br>
> Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
> <a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
> Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="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" target="_blank">http://kiwibots.org</a> for venue, directions and dates.<br>
> When replying, please edit your Subject line to reflect new subjects.<br>
<br>
_______________________________________________<br>
Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
<a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="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" 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>
</blockquote></div>
_______________________________________________<br>
Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
<a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="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" 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" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
<a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="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" 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>