Subject: Re: bug#42656: term.c:1405:25: warning: ‘%d’ directive writing between 2 and 10 bytes into a region of size 3

From: Jeffrey Walton <[email-masked]>
Date: Fri, 28 Aug 2020 02:06:45 -0400

On Sat, Aug 1, 2020 at 3:43 PM Andreas Schwab <[email-masked]> wrote:
>
> On Aug 01 2020, Jeffrey Walton wrote:
>
> > term.c:1405:23: note: directive argument in the range [11, 2147483646]
> >         sprintf (fkey, "f%d", i);
> >                        ^~~~~
>
> That's clearly a compiler bug.  The loop counts i from 11 to 64, so
> 2147483646 can never happen.

Sorry for the late reply.

What I have found is, analysis is catching the case where printf
family returns -1. It is non-obvious, and I think it's a case where a
better message should be supplied.

The best way I have found to handle it is to terminate the string on
failure. This seems to squash the warning in the cases I have
encountered.

  #define TERM_PRINTF(buf, len, rc) \
      buf[rc < 0 ? 0 : rc >= len ? len-1 : rc] = '\0'

  int rc = snprintf(buf, sizeof(buf), "%d", n);
  TERM_PRINTF(buf, sizeof(buf), rc);

I don't know if it will help in this case since I did not perform the
patch. But it is the first thing I would try if I was working the bug.

Jeff