Re: a few more patches
Craig Small <[email protected]> Thu, 17 Feb 2022 18:47:02 +1100
| Newsgroups | gmane.linux.procps.devel |
|---|---|
| Message-ID | <CALy8Cw57wG=kw=AyxPqcaZED=cjaGZD0h3QoeNybC7Xz0SPpYg@mail.gmail.com> |
Hi Jim,
They're both pushed thanks.
I can't find an email if I gave specifics about the warnings or not, but
they are below.
This seems a bit of a bogus warning. You are using snprintf() with a %s
which could be a long string and limiting it to 4 characters.
The issue is, not checking the return value. But! you know what the source
string is (another window name) so it won't be larger than 4 and truncate.
There are probably three ways ahead with this:
* Disable the warning when using --enable-harden-flags
* Add a compiler note to not complain
* Change the top.c code to check the return value
I see not a lot of value of this warning, especially in this context;
what's your view?
I found
https://stackoverflow.com/questions/51534284/how-to-circumvent-format-truncation-warning-in-gcc/51536663
quite useful too.
CC top/top.o
top/top.c: In function ‘keys_window’:
top/top.c:4163:55: warning: ‘%s’ directive output may be truncated writing
up to 127 bytes into a region of size 4 [-Wformat-truncation=]
4163 | snprintf(q->rc.winname, sizeof(q->rc.winname), "%s", name);
| ^~
......
5403 | if (tmp[0] && tmp[0] != kbd_ESC) win_names(w, tmp);
| ~~~
top/top.c:4163:7: note: ‘snprintf’ output between 1 and 128 bytes into a
destination of size 4
4163 | snprintf(q->rc.winname, sizeof(q->rc.winname), "%s", name);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
top/top.c:4164:49: warning: ‘%s’ directive output may be truncated writing
up to 127 bytes into a region of size between 0 and 4 [-Wformat-truncation=]
4164 | snprintf(q->grpname, sizeof(q->grpname), "%d:%s", q->winnum,
name);
| ^~
......
5403 | if (tmp[0] && tmp[0] != kbd_ESC) win_names(w, tmp);
| ~~~
top/top.c:4164:4: note: ‘snprintf’ output between 3 and 140 bytes into a
destination of size 6
4164 | snprintf(q->grpname, sizeof(q->grpname), "%d:%s", q->winnum,
name);
|
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CCLD top/top
If you want to see how it happens:
$ gcc -Wformat -o test1 test1.c
test1.c: In function ‘main’:
test1.c:8:43: warning: ‘%s’ directive output may be truncated writing up to
99 bytes into a region of size 4 [-Wformat-truncation=]
8 | snprintf(smallbuf, sizeof(smallbuf), "%s", buf);
| ^~ ~~~
test1.c:8:5: note: ‘snprintf’ output between 1 and 100 bytes into a
destination of size 4
8 | snprintf(smallbuf, sizeof(smallbuf), "%s", buf);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$ cat test1.c
#include <stdio.h>
int main()
{
char buf[100] = "123";
char smallbuf[4];
snprintf(smallbuf, sizeof(smallbuf), "%s", buf);
printf("Small buffer is >%s<\n", smallbuf);
return 0;
}
On Wed, 16 Feb 2022 at 07:26, Jim Warner <[email protected]> wrote:
> Hi Craig,
>
> Here are some more minor patches for our two branches.
>
> Thanks in advance for the pushes.
>
> Regards,
>
> Jim