Skip to content

Commit

Permalink
Replace sprintf with snprintf in networking
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Shipton <Peter_Shipton@ca.ibm.com>
  • Loading branch information
pshipton committed Nov 4, 2024
1 parent 4ff1022 commit 2c2fc64
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 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 @@ -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);
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 @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include <malloc.h>
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2024, 2024 All Rights Reserved
* ===========================================================================
*/
#include <malloc.h>

#include "net_util.h"
#include "NetworkInterface.h"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 2c2fc64

Please sign in to comment.