From 2c2fc64cf33ab6c93cea07310438bc965c828d8b Mon Sep 17 00:00:00 2001 From: Peter Shipton Date: Mon, 4 Nov 2024 10:34:42 -0500 Subject: [PATCH] Replace sprintf with snprintf in networking Signed-off-by: Peter Shipton --- .../unix/native/libnet/PlainDatagramSocketImpl.c | 7 ++++++- .../windows/native/libnet/NetworkInterface_winXP.c | 9 +++++++-- .../native/libnet/TwoStacksPlainDatagramSocketImpl.c | 11 ++++++++--- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/java.base/unix/native/libnet/PlainDatagramSocketImpl.c b/src/java.base/unix/native/libnet/PlainDatagramSocketImpl.c index c0d5699bbd8..4f94544fb37 100644 --- a/src/java.base/unix/native/libnet/PlainDatagramSocketImpl.c +++ b/src/java.base/unix/native/libnet/PlainDatagramSocketImpl.c @@ -22,6 +22,11 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ +/* + * =========================================================================== + * (c) Copyright IBM Corp. 2024, 2024 All Rights Reserved + * =========================================================================== + */ #include #include #include @@ -1535,7 +1540,7 @@ jobject getMulticastInterface(JNIEnv *env, jobject this, int fd, jint opt) { index); if (ni == NULL) { char errmsg[255]; - sprintf(errmsg, + snprintf(errmsg, sizeof(errmsg), "IPV6_MULTICAST_IF returned index to unrecognized interface: %d", index); JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", errmsg); diff --git a/src/java.base/windows/native/libnet/NetworkInterface_winXP.c b/src/java.base/windows/native/libnet/NetworkInterface_winXP.c index 92b015e8bb7..85432eaed1f 100644 --- a/src/java.base/windows/native/libnet/NetworkInterface_winXP.c +++ b/src/java.base/windows/native/libnet/NetworkInterface_winXP.c @@ -22,6 +22,11 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ +/* + * =========================================================================== + * (c) Copyright IBM Corp. 2024, 2024 All Rights Reserved + * =========================================================================== + */ #include "net_util.h" #include "NetworkInterface.h" @@ -367,10 +372,10 @@ int getAllInterfacesAndAddresses (JNIEnv *env, netif **netifPP) goto err; } if (ptr->IfType == IF_TYPE_TUNNEL) { - sprintf (newname, "tun%d", tun); + snprintf (newname, sizeof(newname), "tun%d", tun); tun ++; } else { - sprintf (newname, "net%d", net); + snprintf (newname, sizeof(newname), "net%d", net); net ++; } nif->name = malloc (strlen(newname)+1); diff --git a/src/java.base/windows/native/libnet/TwoStacksPlainDatagramSocketImpl.c b/src/java.base/windows/native/libnet/TwoStacksPlainDatagramSocketImpl.c index 22cc59995be..d056870fc8f 100644 --- a/src/java.base/windows/native/libnet/TwoStacksPlainDatagramSocketImpl.c +++ b/src/java.base/windows/native/libnet/TwoStacksPlainDatagramSocketImpl.c @@ -22,7 +22,12 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ -#include +/* + * =========================================================================== + * (c) Copyright IBM Corp. 2024, 2024 All Rights Reserved + * =========================================================================== + */ + #include #include "net_util.h" #include "NetworkInterface.h" @@ -1852,7 +1857,7 @@ jobject getMulticastInterface(JNIEnv *env, jobject this, int fd, int fd1, jint o index); if (ni == NULL) { char errmsg[255]; - sprintf(errmsg, + snprintf(errmsg, sizeof(errmsg), "IPV6_MULTICAST_IF returned index to unrecognized interface: %d", index); JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", errmsg); @@ -1951,7 +1956,7 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_socketGetOption int size = 0; char errmsg[255 + 31]; getErrorString(errno, tmpbuf, sizeof(tmpbuf)); - sprintf(errmsg, "error getting socket option: %s", tmpbuf); + snprintf(errmsg, sizeof(errmsg), "error getting socket option: %s", tmpbuf); JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", errmsg); return NULL; }