Skip to content

Commit

Permalink
Merge pull request #843 from pshipton/snprintfn
Browse files Browse the repository at this point in the history
Replace sprintf with snprintf in networking
  • Loading branch information
keithc-ca authored Nov 8, 2024
2 parents abd834e + ba135e5 commit 7bc831a
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 @@ -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 @@ -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 @@ -1864,7 +1869,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 +1988,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 7bc831a

Please sign in to comment.