Coverity warning: strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);

Matthias Andree matthias.andree at tu-dortmund.de
Sun May 2 10:21:48 UTC 2010


Alfred Perlstein schrieb:
> I notice this code sprinkled through the sources:
>   strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
> 
> This trips up coverity because it does not know for sure
> that the string returned by cam_sim_name() is going to 
> be DEV_IDLEN-1 characters long.

Right. strncpy/strncat are examples for features that the C standards
libc had better not ever had, similar to [f]gets...

> Should we switch these calls to strlcpy?  Is there a smarter
> thing to do to code more defensively?

if dev_name is a vector of char or equally sized types:
(cpi->dev_name)[DEV_IDLEN-1] = '\0';

However, rather than relying on implicit assumptions and inefficiencies,
I'd still prefer memset + strlcpy.

-- 
Matthias Andree


More information about the freebsd-hackers mailing list