From: Yang Luo Date: Fri, 29 Jul 2016 08:44:59 +0000 (+0800) Subject: Fix MSVC compile warnings on the WinPcap specific code. X-Git-Tag: libpcap-1.8.1~118^2~7 X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/5074e7d95b82853668e05445399ee119ef7d0ad8 Fix MSVC compile warnings on the WinPcap specific code. --- diff --git a/missing/win_snprintf.c b/missing/win_snprintf.c index 6e74d859..65a8ea1a 100644 --- a/missing/win_snprintf.c +++ b/missing/win_snprintf.c @@ -6,7 +6,7 @@ pcap_vsnprintf(char *str, size_t str_size, const char *format, va_list args) { int ret; - ret = _vsnprintf(str, str_size, format, args); + ret = _vsnprintf_s(str, str_size, _TRUNCATE, format, args); /* * XXX - _vsnprintf() and _snprintf() do *not* guarantee diff --git a/pcap-new.c b/pcap-new.c index b7aa886e..ab48e252 100644 --- a/pcap-new.c +++ b/pcap-new.c @@ -166,7 +166,7 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all } /* Copy the new device identifier into the correct memory location */ - strncpy(dev->name, tmpstring, strlen(tmpstring) + 1); + strlcpy(dev->name, tmpstring, strlen(tmpstring) + 1); /* Create the new device description */ @@ -189,7 +189,7 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all } /* Copy the new device description into the correct memory location */ - strncpy(dev->description, tmpstring, strlen(tmpstring) + 1); + strlcpy(dev->description, tmpstring, strlen(tmpstring) + 1); dev = dev->next; } @@ -312,7 +312,7 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all return -1; } - strncpy(dev->name, tmpstring, stringlen); + strlcpy(dev->name, tmpstring, stringlen); dev->name[stringlen] = 0; @@ -331,7 +331,7 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all } /* Copy the new device description into the correct memory location */ - strncpy(dev->description, tmpstring, stringlen + 1); + strlcpy(dev->description, tmpstring, stringlen + 1); pcap_close(fp); } @@ -514,7 +514,7 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all } /* Copy the new device name into the correct memory location */ - strncpy(dev->name, tmpstring2, stringlen + 1); + strlcpy(dev->name, tmpstring2, stringlen + 1); } if (findalldevs_if.desclen) @@ -546,7 +546,7 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all } /* Copy the new device description into the correct memory location */ - strncpy(dev->description, tmpstring2, stringlen + 1); + strlcpy(dev->description, tmpstring2, stringlen + 1); } dev->flags = ntohl(findalldevs_if.flags); @@ -668,10 +668,10 @@ int pcap_createsrcstr(char *source, int type, const char *host, const char *port { case PCAP_SRC_FILE: { - strncpy(source, PCAP_SRC_FILE_STRING, PCAP_BUF_SIZE); + strlcpy(source, PCAP_SRC_FILE_STRING, PCAP_BUF_SIZE); if ((name) && (*name)) { - strncat(source, name, PCAP_BUF_SIZE); + strlcat(source, name, PCAP_BUF_SIZE); return 0; } else @@ -683,27 +683,27 @@ int pcap_createsrcstr(char *source, int type, const char *host, const char *port case PCAP_SRC_IFREMOTE: { - strncpy(source, PCAP_SRC_IF_STRING, PCAP_BUF_SIZE); + strlcpy(source, PCAP_SRC_IF_STRING, PCAP_BUF_SIZE); if ((host) && (*host)) { if ((strcspn(host, "aAbBcCdDeEfFgGhHjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ")) == strlen(host)) { /* the host name does not contains alphabetic chars. So, it is a numeric address */ /* In this case we have to include it between square brackets */ - strncat(source, "[", PCAP_BUF_SIZE); - strncat(source, host, PCAP_BUF_SIZE); - strncat(source, "]", PCAP_BUF_SIZE); + strlcat(source, "[", PCAP_BUF_SIZE); + strlcat(source, host, PCAP_BUF_SIZE); + strlcat(source, "]", PCAP_BUF_SIZE); } else - strncat(source, host, PCAP_BUF_SIZE); + strlcat(source, host, PCAP_BUF_SIZE); if ((port) && (*port)) { - strncat(source, ":", PCAP_BUF_SIZE); - strncat(source, port, PCAP_BUF_SIZE); + strlcat(source, ":", PCAP_BUF_SIZE); + strlcat(source, port, PCAP_BUF_SIZE); } - strncat(source, "/", PCAP_BUF_SIZE); + strlcat(source, "/", PCAP_BUF_SIZE); } else { @@ -712,17 +712,17 @@ int pcap_createsrcstr(char *source, int type, const char *host, const char *port } if ((name) && (*name)) - strncat(source, name, PCAP_BUF_SIZE); + strlcat(source, name, PCAP_BUF_SIZE); return 0; } case PCAP_SRC_IFLOCAL: { - strncpy(source, PCAP_SRC_IF_STRING, PCAP_BUF_SIZE); + strlcpy(source, PCAP_SRC_IF_STRING, PCAP_BUF_SIZE); if ((name) && (*name)) - strncat(source, name, PCAP_BUF_SIZE); + strlcat(source, name, PCAP_BUF_SIZE); return 0; } @@ -801,7 +801,7 @@ int pcap_parsesrcstr(const char *source, int *type, char *host, char *port, char { /* We're on a local capture */ if (*ptr) - strncpy(tmpname, ptr, PCAP_BUF_SIZE); + strlcpy(tmpname, ptr, PCAP_BUF_SIZE); /* Clean the host name, since it is a remote capture */ /* NOTE: the host name has been assigned in the previous "ntoken= sscanf(...)" line */ @@ -815,9 +815,9 @@ int pcap_parsesrcstr(const char *source, int *type, char *host, char *port, char } if (host) - strcpy(host, tmphost); + strlcpy(host, tmphost, PCAP_BUF_SIZE); if (port) - strcpy(port, tmpport); + strlcpy(port, tmpport, PCAP_BUF_SIZE); if (type) *type = tmptype; @@ -830,7 +830,7 @@ int pcap_parsesrcstr(const char *source, int *type, char *host, char *port, char */ if (tmpname[0]) { - strcpy(name, tmpname); + strlcpy(name, tmpname, PCAP_BUF_SIZE); } else { @@ -851,7 +851,7 @@ int pcap_parsesrcstr(const char *source, int *type, char *host, char *port, char if (*ptr) { if (name) - strncpy(name, ptr, PCAP_BUF_SIZE); + strlcpy(name, ptr, PCAP_BUF_SIZE); if (type) *type = PCAP_SRC_FILE; @@ -872,7 +872,7 @@ int pcap_parsesrcstr(const char *source, int *type, char *host, char *port, char if ((source) && (*source)) { if (name) - strncpy(name, source, PCAP_BUF_SIZE); + strlcpy(name, source, PCAP_BUF_SIZE); if (type) *type = PCAP_SRC_IFLOCAL; @@ -979,7 +979,7 @@ pcap_t *pcap_open(const char *source, int snaplen, int flags, int read_timeout, break; default: - strcpy(errbuf, "Source type not supported"); + strlcpy(errbuf, "Source type not supported", PCAP_ERRBUF_SIZE); return NULL; } return fp; @@ -1254,7 +1254,7 @@ int pcap_remoteact_list(char *hostlist, char sep, int size, char *errbuf) return -1; } - strcat(hostlist, hoststr); + strlcat(hostlist, hoststr, PCAP_ERRBUF_SIZE); hostlist[len - 1] = sep; hostlist[len] = 0; diff --git a/pcap-win32.c b/pcap-win32.c index 1c993be6..68d9264a 100644 --- a/pcap-win32.c +++ b/pcap-win32.c @@ -1225,7 +1225,7 @@ pcap_setfilter_win32_dag(pcap_t *p, struct bpf_program *fp) { if(!fp) { - strncpy(p->errbuf, "setfilter: No filter specified", sizeof(p->errbuf)); + strlcpy(p->errbuf, "setfilter: No filter specified", sizeof(p->errbuf)); return (-1); } diff --git a/portability.h b/portability.h index 5a35b361..0d115b30 100644 --- a/portability.h +++ b/portability.h @@ -79,6 +79,14 @@ extern "C" { #ifdef _MSC_VER #define strdup _strdup + #define sscanf sscanf_s +char *tokbuf; + #define strltok(x, y) \ + strtok_s((x), (y), &tokbuf) + #define strlcat(x, y, z) \ + strncat_s((x), (z), (y), _TRUNCATE) +#else + #define strltok strtok #endif /* diff --git a/sockutils.c b/sockutils.c index 947637a6..0408a2fa 100644 --- a/sockutils.c +++ b/sockutils.c @@ -862,7 +862,7 @@ int sock_check_hostlist(char *hostlist, const char *sep, struct sockaddr_storage return -2; } - token = strtok(temphostlist, sep); + token = strltok(temphostlist, sep); /* it avoids a warning in the compilation ('addrinfo used but not initialized') */ addrinfo = NULL; @@ -886,7 +886,7 @@ int sock_check_hostlist(char *hostlist, const char *sep, struct sockaddr_storage SOCK_ASSERT(errbuf, 1); /* Get next token */ - token = strtok(NULL, sep); + token = strltok(NULL, sep); continue; } @@ -911,7 +911,7 @@ int sock_check_hostlist(char *hostlist, const char *sep, struct sockaddr_storage addrinfo = NULL; /* Get next token */ - token = strtok(NULL, sep); + token = strltok(NULL, sep); } if (addrinfo) @@ -1105,7 +1105,7 @@ int sock_getascii_addrport(const struct sockaddr_storage *sockaddr, char *addres (memcmp(&((struct sockaddr_in6 *) sockaddr)->sin6_addr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", sizeof(struct in6_addr)) == 0)) { if (address) - strncpy(address, SOCKET_NAME_NULL_DAD, addrlen); + strlcpy(address, SOCKET_NAME_NULL_DAD, addrlen); return retval; } } @@ -1121,13 +1121,13 @@ int sock_getascii_addrport(const struct sockaddr_storage *sockaddr, char *addres if (address) { - strncpy(address, SOCKET_NO_NAME_AVAILABLE, addrlen); + strlcpy(address, SOCKET_NO_NAME_AVAILABLE, addrlen); address[addrlen - 1] = 0; } if (port) { - strncpy(port, SOCKET_NO_PORT_AVAILABLE, portlen); + strlcpy(port, SOCKET_NO_PORT_AVAILABLE, portlen); port[portlen - 1] = 0; }