Skip to content

Commit

Permalink
Merge pull request #828 from pshipton/sprintf
Browse files Browse the repository at this point in the history
Backport 8307850 to jdk11
  • Loading branch information
keithc-ca authored Oct 29, 2024
2 parents 7eb754a + 9153830 commit bb64c4c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/jdk.jdi/share/native/libdt_shmem/SharedMemoryTransport.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -64,9 +64,9 @@ throwShmemException(JNIEnv *env, char *message, jint errorCode)
char buf[255];

if (shmemBase_getlasterror(msg, sizeof(msg)) == SYS_OK) {
sprintf(buf, "%s: %s\n", message, msg);
snprintf(buf, sizeof(buf), "%s: %s\n", message, msg);
} else {
sprintf(buf, "%s, error code = %d", message, errorCode);
snprintf(buf, sizeof(buf), "%s, error code = %d", message, errorCode);
}
throwException(env, "java/io/IOException", buf);
}
Expand Down
26 changes: 13 additions & 13 deletions src/jdk.jdi/share/native/libdt_shmem/shmemBase.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -227,7 +227,7 @@ createWithGeneratedName(char *prefix, char *nameBuffer, CreateFunc func, void *a
strcpy(nameBuffer, prefix);
if (i > 0) {
char buf[10];
sprintf(buf, ".%d", i+1);
snprintf(buf, sizeof(buf), ".%d", i+1);
strcat(nameBuffer, buf);
}
error = func(nameBuffer, arg);
Expand Down Expand Up @@ -406,22 +406,22 @@ createStream(char *name, Stream *stream)
jint error;
char objectName[MAX_IPC_NAME];

sprintf(objectName, "%s.mutex", name);
snprintf(objectName, sizeof(objectName), "%s.mutex", name);
error = createWithGeneratedName(objectName, stream->shared->mutexName,
createMutex, &stream->mutex);
if (error != SYS_OK) {
return error;
}

sprintf(objectName, "%s.hasData", name);
snprintf(objectName, sizeof(objectName), "%s.hasData", name);
error = createWithGeneratedName(objectName, stream->shared->hasDataEventName,
createEvent, &stream->hasData);
if (error != SYS_OK) {
(void)closeStream(stream, JNI_FALSE);
return error;
}

sprintf(objectName, "%s.hasSpace", name);
snprintf(objectName, sizeof(objectName), "%s.hasSpace", name);
error = createWithGeneratedName(objectName, stream->shared->hasSpaceEventName,
createEvent, &stream->hasSpace);
if (error != SYS_OK) {
Expand Down Expand Up @@ -541,7 +541,7 @@ openConnection(SharedMemoryTransport *transport, jlong otherPID,
return SYS_NOMEM;
}

sprintf(connection->name, "%s.%" PRId64, transport->name, sysProcessGetID());
snprintf(connection->name, sizeof(connection->name), "%s.%" PRId64, transport->name, sysProcessGetID());
error = sysSharedMemOpen(connection->name, &connection->sharedMemory,
&connection->shared);
if (error != SYS_OK) {
Expand Down Expand Up @@ -605,7 +605,7 @@ createConnection(SharedMemoryTransport *transport, jlong otherPID,
return SYS_NOMEM;
}

sprintf(connection->name, "%s.%" PRId64, transport->name, otherPID);
snprintf(connection->name, sizeof(connection->name), "%s.%" PRId64, transport->name, otherPID);
error = sysSharedMemCreate(connection->name, sizeof(SharedMemory),
&connection->sharedMemory, &connection->shared);
if (error != SYS_OK) {
Expand Down Expand Up @@ -700,7 +700,7 @@ openTransport(const char *address, SharedMemoryTransport **transportPtr)

if (strlen(address) >= MAX_IPC_PREFIX) {
char buf[128];
sprintf(buf, "Error: address strings longer than %d characters are invalid\n", MAX_IPC_PREFIX);
snprintf(buf, sizeof(buf), "Error: address strings longer than %d characters are invalid\n", MAX_IPC_PREFIX);
setLastErrorMsg(buf);
closeTransport(transport);
return SYS_ERR;
Expand Down Expand Up @@ -764,7 +764,7 @@ createTransport(const char *address, SharedMemoryTransport **transportPtr)
} else {
if (strlen(address) >= MAX_IPC_PREFIX) {
char buf[128];
sprintf(buf, "Error: address strings longer than %d characters are invalid\n", MAX_IPC_PREFIX);
snprintf(buf, sizeof(buf), "Error: address strings longer than %d characters are invalid\n", MAX_IPC_PREFIX);
setLastErrorMsg(buf);
closeTransport(transport);
return SYS_ERR;
Expand All @@ -782,23 +782,23 @@ createTransport(const char *address, SharedMemoryTransport **transportPtr)
memset(transport->shared, 0, sizeof(SharedListener));
transport->shared->acceptingPID = sysProcessGetID();

sprintf(objectName, "%s.mutex", transport->name);
snprintf(objectName, sizeof(objectName), "%s.mutex", transport->name);
error = createWithGeneratedName(objectName, transport->shared->mutexName,
createMutex, &transport->mutex);
if (error != SYS_OK) {
closeTransport(transport);
return error;
}

sprintf(objectName, "%s.accept", transport->name);
snprintf(objectName, sizeof(objectName), "%s.accept", transport->name);
error = createWithGeneratedName(objectName, transport->shared->acceptEventName,
createEvent, &transport->acceptEvent);
if (error != SYS_OK) {
closeTransport(transport);
return error;
}

sprintf(objectName, "%s.attach", transport->name);
snprintf(objectName, sizeof(objectName), "%s.attach", transport->name);
error = createWithGeneratedName(objectName, transport->shared->attachEventName,
createEvent, &transport->attachEvent);
if (error != SYS_OK) {
Expand Down Expand Up @@ -1169,7 +1169,7 @@ exitTransportWithError(char *message, char *fileName,
jint error;
char buffer[500];

sprintf(buffer, "Shared Memory Transport \"%s\" (%s), line %d: %s\n",
snprintf(buffer, sizeof(buffer), "Shared Memory Transport \"%s\" (%s), line %d: %s\n",
fileName, date, lineNumber, message);
error = (*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_2);
if (error != JNI_OK) {
Expand Down

0 comments on commit bb64c4c

Please sign in to comment.