nativenet: Change deprecated sprintf to snprintf

This commit is contained in:
rdb 2025-10-10 14:11:42 +02:00
parent e68a3db7c9
commit 2540648ac9
2 changed files with 5 additions and 2 deletions

View File

@ -133,6 +133,7 @@ check_include_file_cxx(dirent.h PHAVE_DIRENT_H)
check_include_file_cxx(ucontext.h PHAVE_UCONTEXT_H) #TODO doesn't work on OSX, use sys/ucontext.h
check_include_file_cxx(linux/input.h PHAVE_LINUX_INPUT_H)
check_include_file_cxx(stdint.h PHAVE_STDINT_H)
check_include_file_cxx(execinfo.h PHAVE_EXECINFO_H)
# Do we have Posix threads?
#set(HAVE_POSIX_THREADS ${CMAKE_USE_PTHREADS_INIT})

View File

@ -125,13 +125,15 @@ get_ip_port() const {
if (_storage.ss_family == AF_INET) {
getnameinfo(&_addr, sizeof(sockaddr_in), buf, sizeof(buf), nullptr, 0, NI_NUMERICHOST);
sprintf(buf + strlen(buf), ":%hu", get_port());
size_t len = strlen(buf);
snprintf(buf + len, sizeof(buf) - len, ":%hu", get_port());
} else if (_storage.ss_family == AF_INET6) {
// Protect the IPv6 address within square brackets.
buf[0] = '[';
getnameinfo(&_addr, sizeof(sockaddr_in6), buf + 1, sizeof(buf) - 1, nullptr, 0, NI_NUMERICHOST);
sprintf(buf + strlen(buf), "]:%hu", get_port());
size_t len = strlen(buf);
snprintf(buf + len, sizeof(buf) - len, "]:%hu", get_port());
} else {
nassert_raise("unsupported address family");