Bug 85418 - -Wformat-truncation on inlinned function
Summary: -Wformat-truncation on inlinned function
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 7.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2018-04-16 14:58 UTC by Mohammad Azim Khan
Modified: 2018-04-16 19:58 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mohammad Azim Khan 2018-04-16 14:58:35 UTC
See following code:

```
#include <stdio.h>

void f(const char s[5], size_t len)
{
    char b[5];
    snprintf(b, len, "%s", s); 
}

int main()
{
    f("Hello World", 4); 
    return 0;
}
```

On compiling it with -O2/3 f() gets inlinned and snprintf format checks apply to it even though parameters to snprintf are parameters within f().
Comment 1 Martin Sebor 2018-04-16 19:58:02 UTC
The warning is intended (and designed to trigger across inlined function calls).  The call to snprintf() truncates the string which may be unintended.

To avoid the warning either use the return value of the snprintf function to take some action (i.e., handle the truncation) or use the len argument to specify the precision of the %s directive like so:

    snprintf (b, sizeof b, "%.*s", (int)len, s);