[Mesa-dev] [PATCH] d3dadapter9: use snprintf(..., "%s", ...) instead of strncpy

Andre Heider a.heider at gmail.com
Mon Nov 26 12:23:19 UTC 2018


On 25/11/2018 17:23, Axel Davy wrote:
> Reading 
> https://developers.redhat.com/blog/2018/05/24/detecting-string-truncation-with-gcc-8/ 
> 
> I think the snprintf variant suffers from the same issue, and the 
> compiler is just not yet able to detect it,
> and send the same warning (but it might do in later gcc versions).

In this case we care about the terminating NULL (which strncpy() does 
not ensure) and not really about the truncation, because all these 
chunks are about D3DADAPTER_IDENTIFIER9.Description with a fixed size of 
512 chars.

Even if those spots would get truncated, you won't get a warning about 
it, see [0] ;)

> Probably a better fix would be to copy with a max size of 
> sizeof(drvid->Description)-1 and do
> drvid->Description[sizeof(drvid->Description)-1] = '\0';
> (Though the webpage says only doing the assignment should be sufficient 
> to please gcc).

Sure, but then mesa might as well import a helper like strlcpy() [1] 
instead of doing that locally.

For these few uncritical spots I think snprintf() is just fine, even if 
a format specifier of just "%s" looks weird. But that's your call.

Thanks,
Andre

[0] 97ae5a85 "meson+autotools: get rid of spammy GCC warning 
-Wformat-truncation"
[1] https://cgit.freedesktop.org/libbsd/tree/src/strlcpy.c


More information about the mesa-dev mailing list