<div dir="ltr">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><div><br></div><div>or </div><div><br></div><div>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.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, 30 Jul 2024 at 10:27, 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">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.</blockquote></div>