Bugfix/win32 build warnings (#936)

* Changed win32 port of localtime to use secure OS API functions when compiled with MSVC

* Changed all the sprintf to use snprintf instead in BSC tests to ensure null string endings.
This commit is contained in:
Steve Karg
2025-03-03 10:32:28 -06:00
committed by GitHub
parent b0a26cf2cd
commit 7e725ce028
21 changed files with 317 additions and 275 deletions
+2 -1
View File
@@ -466,7 +466,8 @@ static char *ifname_default(void)
if (BIP_Interface_Name[0] != 0) {
return BIP_Interface_Name;
}
strncpy(BIP_Interface_Name, "en0", sizeof(BIP_Interface_Name));
snprintf(BIP_Interface_Name, sizeof(BIP_Interface_Name), "%s", "en0");
return BIP_Interface_Name;
}
+7 -7
View File
@@ -115,9 +115,9 @@ static int arcnet_bind(const char *interface_name)
ARCNET_Socket_Address.sa_data, '\0',
sizeof(ARCNET_Socket_Address.sa_data));
/* Strcpy the interface name into the address */
strncpy(
ARCNET_Socket_Address.sa_data, interface_name,
sizeof(ARCNET_Socket_Address.sa_data) - 1);
snprintf(
ARCNET_Socket_Address.sa_data,
sizeof(ARCNET_Socket_Address.sa_data), "%s", interface_name);
fprintf(
stderr, "arcnet: binding \"%s\"\n", ARCNET_Socket_Address.sa_data);
if (bind(
@@ -140,7 +140,7 @@ static int arcnet_bind(const char *interface_name)
exit(-1);
}
}
strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", interface_name);
rv = ioctl(sock_fd, SIOCGIFHWADDR, &ifr);
if (rv != -1) { /* worked okay */
ARCNET_MAC_Address = ifr.ifr_hwaddr.sa_data[0];
@@ -153,9 +153,9 @@ static int arcnet_bind(const char *interface_name)
ARCNET_Socket_Address.sa_data, '\0',
sizeof(ARCNET_Socket_Address.sa_data));
/* Strcpy the interface name into the address */
strncpy(
ARCNET_Socket_Address.sa_data, interface_name,
sizeof(ARCNET_Socket_Address.sa_data) - 1);
snprintf(
ARCNET_Socket_Address.sa_data, sizeof(ARCNET_Socket_Address.sa_data),
"%s", interface_name);
fprintf(
stderr, "arcnet: MAC=%02Xh iface=\"%s\"\n", ARCNET_MAC_Address,
ARCNET_Socket_Address.sa_data);
+1 -1
View File
@@ -475,7 +475,7 @@ get_local_ifr_ioctl(const char *ifname, struct ifreq *ifr, int request)
int fd;
int rv; /* return value */
strncpy(ifr->ifr_name, ifname, sizeof(ifr->ifr_name) - 1);
snprintf(ifr->ifr_name, sizeof(ifr->ifr_name), "%s", ifname);
ifr->ifr_name[sizeof(ifr->ifr_name) - 1] = 0;
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
+2 -1
View File
@@ -120,7 +120,8 @@ static int ethernet_bind(struct sockaddr *eth_addr, const char *interface_name)
/* Clear the memory before copying */
memset(eth_addr->sa_data, '\0', sizeof(eth_addr->sa_data));
/* Strcpy the interface name into the address */
strncpy(eth_addr->sa_data, interface_name, sizeof(eth_addr->sa_data) - 1);
snprintf(
eth_addr->sa_data, sizeof(eth_addr->sa_data), "%s", interface_name);
fprintf(stderr, "ethernet: binding \"%s\"\n", eth_addr->sa_data);
/* Attempt to bind the socket to the interface */
if (bind(sock_fd, eth_addr, sizeof(struct sockaddr)) != 0) {
+1 -2
View File
@@ -116,8 +116,7 @@ static int network_init(const char *name, int protocol)
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, name, strlen(name));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", name);
if (ioctl(sockfd, SIOCGIFINDEX, &ifr) == -1) {
perror("Unable to get interface index");
return -1;
+1
View File
@@ -10,6 +10,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "bacnet/basic/sys/debug.h"
#include "bacnet/datalink/bsc/bsc-event.h"
#define DEBUG_BSC_EVENT 0
+14 -7
View File
@@ -51,6 +51,8 @@ int gettimeofday(struct timeval *tp, void *tzp)
{
static int tzflag = 0;
struct timezone *tz;
long tz_seconds = 0;
int tz_hours = 0;
/* start calendar time in microseconds */
static LONGLONG usec_timer = 0;
LONGLONG usec_elapsed = 0;
@@ -86,8 +88,10 @@ int gettimeofday(struct timeval *tp, void *tzp)
tzflag++;
}
tz = tzp;
tz->tz_minuteswest = _timezone / 60;
tz->tz_dsttime = _daylight;
(void)_get_timezone(&tz_seconds);
tz->tz_minuteswest = tz_seconds / 60;
(void)_get_daylight(&tz_hours);
tz->tz_dsttime = tz_hours;
}
return 0;
@@ -125,15 +129,18 @@ bool datetime_local(
bool *dst_active)
{
bool status = false;
struct tm *tblock = NULL;
struct tm *tblock;
#if defined(_MSC_VER)
time_t tTemp;
struct tm newtime = { 0 };
__time64_t long_time = 0;
#else
struct timeval tv;
#endif
#if defined(_MSC_VER)
time(&tTemp);
tblock = (struct tm *)localtime(&tTemp);
_time64(&long_time);
(void)localtime_s(&newtime, &long_time);
tblock = &newtime;
#else
if (gettimeofday(&tv, NULL) == 0) {
tblock = (struct tm *)localtime((const time_t *)&tv.tv_sec);
@@ -181,7 +188,7 @@ bool datetime_local(
/* timezone is set to the difference, in seconds,
between Coordinated Universal Time (UTC) and
local standard time */
*utc_offset_minutes = timezone / 60;
*utc_offset_minutes = (int16_t)(timezone / 60);
}
}