Skip to content

Commit

Permalink
Replace sprintf with snprintf in networking
Browse files Browse the repository at this point in the history
Backport ibmruntimes/openj9-openjdk-jdk17#406

Signed-off-by: Peter Shipton <Peter_Shipton@ca.ibm.com>
  • Loading branch information
pshipton committed Nov 7, 2024
1 parent abd834e commit ea28f6a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/java.base/unix/native/libnet/PlainDatagramSocketImpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <errno.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -1579,7 +1584,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);
Expand Down
9 changes: 7 additions & 2 deletions src/java.base/windows/native/libnet/NetworkInterface_winXP.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -347,10 +352,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1864,7 +1864,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);
Expand Down Expand Up @@ -1983,7 +1983,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;
}
Expand Down

0 comments on commit ea28f6a

Please sign in to comment.