Skip to content

Commit

Permalink
Merge pull request #406 from pshipton/sprintf17
Browse files Browse the repository at this point in the history
Replace sprintf with snprintf in networking
  • Loading branch information
keithc-ca authored Nov 5, 2024
2 parents 3ee7a6f + 1a4c959 commit 53e0100
Show file tree
Hide file tree
Showing 3 changed files with 20 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 @@ -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,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 <malloc.h>

#include "net_util.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 53e0100

Please sign in to comment.