Skip to content

Commit

Permalink
Replace sprintf with snprintf in many places
Browse files Browse the repository at this point in the history
There is one remaining place that will require a coordinated merge with
OMR.

Signed-off-by: Peter Shipton <[email protected]>
  • Loading branch information
pshipton committed Nov 1, 2024
1 parent dcca4dd commit 3dbe0ac
Show file tree
Hide file tree
Showing 24 changed files with 354 additions and 143 deletions.
2 changes: 1 addition & 1 deletion runtime/compiler/compile/J9Compilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ J9::Compilation::allocateCompYieldStatsMatrix()
for (int32_t j=0; j < (int32_t)LAST_CONTEXT; j++)
{
char buffer[128];
sprintf(buffer, "%d-%d", i,j);
snprintf(buffer, sizeof(buffer), "%d-%d", i,j);
_compYieldStatsMatrix[i][j].setName(buffer);
}
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/compiler/compile/J9SymbolReferenceTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ J9::SymbolReferenceTable::methodSymRefWithSignature(TR::SymbolReference *origina

int32_t fullSignatureLength = originalMethod->classNameLength() + 1 + originalMethod->nameLength() + effectiveSignatureLength;
char *fullSignature = (char*)trMemory()->allocateMemory(1 + fullSignatureLength, stackAlloc);
sprintf(fullSignature, "%.*s.%.*s%.*s", originalMethod->classNameLength(), originalMethod->classNameChars(), originalMethod->nameLength(), originalMethod->nameChars(), effectiveSignatureLength, effectiveSignature);
snprintf(fullSignature, 1 + fullSignatureLength, "%.*s.%.*s%.*s", originalMethod->classNameLength(), originalMethod->classNameChars(), originalMethod->nameLength(), originalMethod->nameChars(), effectiveSignatureLength, effectiveSignature);
TR_ASSERT(strlen(fullSignature) == fullSignatureLength, "Computed fullSignatureLength must match actual length of fullSignature");
CS2::HashIndex hashIndex = 0;
static char *ignoreMBSCache = feGetEnv("TR_ignoreMBSCache");
Expand Down
4 changes: 2 additions & 2 deletions runtime/compiler/control/CompilationThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1128,8 +1128,8 @@ TR::CompilationInfoPerThread::CompilationInfoPerThread(TR::CompilationInfo &comp
// NOTE:
// the (char *) casts are done because on Z, sprintf expects
// a (char *) instead of a (const char *)
sprintf(_activeThreadName, (char *) selectedActiveThreadName, getCompThreadId());
sprintf(_suspendedThreadName, (char *) selectedSuspendedThreadName, getCompThreadId());
snprintf(_activeThreadName, activeThreadNameLength, (char *) selectedActiveThreadName, getCompThreadId());
snprintf(_suspendedThreadName, suspendedThreadNameLength, (char *) selectedSuspendedThreadName, getCompThreadId());

_initializationSucceeded = true;
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/compiler/control/DLLMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ IDATA J9VMDllMain(J9JavaVM* vm, IDATA stage, void * reserved)
continue;

//char threadName[32]; // make sure the name below does not exceed 32 chars
//sprintf(threadName, "JIT Compilation Thread-%d", curCompThreadInfoPT->getCompThreadId());
//snprintf(threadName, sizeof(threadName), "JIT Compilation Thread-%d", curCompThreadInfoPT->getCompThreadId());

char *threadName = (
curCompThreadInfoPT->compilationThreadIsActive() ?
Expand Down
8 changes: 4 additions & 4 deletions runtime/compiler/control/HookedByTheJit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ TR::OptionSet *findOptionSet(J9Method *method, bool isAOT)

if (methodSignature)
{
sprintf(methodSignature, "%.*s.%.*s%.*s", J9UTF8_LENGTH(className), utf8Data(className), J9UTF8_LENGTH(name), utf8Data(name), J9UTF8_LENGTH(signature), utf8Data(signature));
snprintf(methodSignature, len, "%.*s.%.*s%.*s", J9UTF8_LENGTH(className), utf8Data(className), J9UTF8_LENGTH(name), utf8Data(name), J9UTF8_LENGTH(signature), utf8Data(signature));

TR_FilterBST * filter = 0;
if (TR::Options::getDebug() && TR::Options::getDebug()->getCompilationFilters())
Expand Down Expand Up @@ -469,7 +469,7 @@ static void jitHookInitializeSendTarget(J9HookInterface * * hook, UDATA eventNum
if (sigLen < 1024)
{
char sigC[1024];
sigLen = sprintf(sigC, "%.*s.%.*s%.*s",
sigLen = snprintf(sigC, sizeof(sigC), "%.*s.%.*s%.*s",
J9UTF8_LENGTH(className), utf8Data(className),
J9UTF8_LENGTH(name), utf8Data(name),
J9UTF8_LENGTH(signature), utf8Data(signature));
Expand Down Expand Up @@ -746,7 +746,7 @@ static void jitHookInitializeSendTarget(J9HookInterface * * hook, UDATA eventNum
J9UTF8 * className = J9ROMCLASS_CLASSNAME(J9_CLASS_FROM_METHOD(method)->romClass);
J9UTF8 * name = J9ROMMETHOD_NAME(J9_ROM_METHOD_FROM_RAM_METHOD(method));
J9UTF8 * signature = J9ROMMETHOD_SIGNATURE(J9_ROM_METHOD_FROM_RAM_METHOD(method));
int32_t sigLen = sprintf(buf, "%.*s.%.*s%.*s", J9UTF8_LENGTH(className), utf8Data(className), J9UTF8_LENGTH(name), utf8Data(name), J9UTF8_LENGTH(signature), utf8Data(signature));
int32_t sigLen = snprintf(buf, sizeof(buf), "%.*s.%.*s%.*s", J9UTF8_LENGTH(className), utf8Data(className), J9UTF8_LENGTH(name), utf8Data(name), J9UTF8_LENGTH(signature), utf8Data(signature));
printf("Initial: Signature %s Count %d isLoopy %d isAOT %" OMR_PRIuPTR " is in SCC %d SCCContainsProfilingInfo %d \n",buf,TR::CompilationInfo::getInvocationCount(method),J9ROMMETHOD_HAS_BACKWARDS_BRANCHES(romMethod),
TR::Options::sharedClassCache() ? jitConfig->javaVM->sharedClassConfig->existsCachedCodeForROMMethod(vmThread, romMethod) : 0,
TR::Options::sharedClassCache() ? TR_J9VMBase::get(jitConfig, vmThread, TR_J9VMBase::AOT_VM)->sharedCache()->isClassInSharedCache(J9_CLASS_FROM_METHOD(method)) : 0,containsInfo) ; fflush(stdout);
Expand Down Expand Up @@ -1740,7 +1740,7 @@ static void initThreadAfterCreation(J9VMThread *vmThread)
char fileName[64];
IDATA tracefp= -1;

sprintf(fileName, "%s_" POINTER_PRINTF_FORMAT, pJitConfig->itraceFileNamePrefix, vmThread);
snprintf(fileName, sizeof(fileName), "%s_" POINTER_PRINTF_FORMAT, pJitConfig->itraceFileNamePrefix, vmThread);

if ((tracefp = j9file_open(fileName, EsOpenWrite | EsOpenAppend | EsOpenCreate, 0644)) == -1)
{
Expand Down
Loading

0 comments on commit 3dbe0ac

Please sign in to comment.