Skip to content

Commit b72df27

Browse files
committed
Fixed coverity warning for util.c
1 parent 84ddc4c commit b72df27

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/util.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ get_iface_ip(const char *ifname)
165165
}
166166

167167
/* Get IP of internal interface */
168-
strcpy(if_data.ifr_name, ifname);
168+
strncpy(if_data.ifr_name, ifname, 15);
169+
if_data.ifr_name[15] = '\0';
169170

170171
/* Get the IP address */
171172
if (ioctl(sockd, SIOCGIFADDR, &if_data) < 0) {
@@ -188,7 +189,8 @@ get_iface_mac(const char *ifname)
188189
struct ifreq ifr;
189190
char *hwaddr, mac[13];
190191

191-
strcpy(ifr.ifr_name, ifname);
192+
strncpy(ifr.ifr_name, ifname, 15);
193+
ifr.ifr_name[15] = '\0';
192194

193195
s = socket(AF_INET, SOCK_DGRAM, 0);
194196
if (-1 == s) {
@@ -229,14 +231,16 @@ get_ext_iface(void)
229231
input = fopen("/proc/net/route", "r");
230232
if (NULL == input) {
231233
debug(LOG_ERR, "Could not open /proc/net/route (%s).", strerror(errno));
234+
free(gw);
235+
free(device);
232236
return NULL;
233237
}
234238
while (!feof(input)) {
235239
/* XXX scanf(3) is unsafe, risks overrun */
236-
if ((fscanf(input, "%s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s\n", device, gw) == 2)
240+
if ((fscanf(input, "%15s %15s %*s %*s %*s %*s %*s %*s %*s %*s %*s\n", device, gw) == 2)
237241
&& strcmp(gw, "00000000") == 0) {
238242
free(gw);
239-
debug(LOG_INFO, "get_ext_iface(): Detected %s as the default interface after try %d", device, i);
243+
debug(LOG_INFO, "get_ext_iface(): Detected %s as the default interface after trying %d", device, i);
240244
fclose(input);
241245
return device;
242246
}

0 commit comments

Comments
 (0)