[PATCH] Fix some warnings from Coverity

Jeremy Allison jra at samba.org
Thu Dec 1 19:44:11 UTC 2016


On Thu, Dec 01, 2016 at 08:34:37PM +0100, Andreas Schneider wrote:
> Hi,
> 
> some minor things, they will make Coverity happy.
> 
> 
> Review and push appreciated.

In:

[PATCH 2/2] libsocket: Make sure ifr.ifr_name is null-terminated

Found by Coverity

Signed-off-by: Andreas Schneider <asn at samba.org>
---
 lib/socket/interfaces.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/socket/interfaces.c b/lib/socket/interfaces.c
index dacd118..991829d 100644
--- a/lib/socket/interfaces.c
+++ b/lib/socket/interfaces.c
@@ -146,7 +146,7 @@ static void query_iface_speed_from_name(const char *name, uint64_t *speed)
        }
 
        ZERO_STRUCT(ifr);
-       strncpy(ifr.ifr_name, name, IF_NAMESIZE);
+       snprintf(ifr.ifr_name, IF_NAMESIZE, "%s", name);
 
        ifr.ifr_data = (void *)&edata;
        edata.cmd = ETHTOOL_GLINK;

Is the snprintf safe ? It also doesn't nul terminate
if strlen(name) > IF_NAMESIZE.

 From the man page:

       The  functions snprintf() and vsnprintf() do not write more than size bytes (including the terminating null byte
       ('\0')).  If the output was truncated due to this limit then the  return  value  is  the  number  of  characters
       (excluding the terminating null byte) which would have been written to the final string if enough space had been
       available.  Thus, a return value of size or more means that the output was truncated.   (See  also  below  under
       NOTES.)

Note the:

 "which would have been written to the final string if enough space had been
available".

If we want to ignore errors (and this function
returns void) we should be using strlcpy() here I think.

Either that or make query_iface_speed_from_name() return
an error and check the return from snprintf().



More information about the samba-technical mailing list