Skip to content

Commit

Permalink
Merge pull request #6895 from eclipse/revert-6862-useSnprintf
Browse files Browse the repository at this point in the history
Revert "Replace sprintf() with snprintf()"
  • Loading branch information
0xdaryl committed Feb 16, 2023
2 parents 15d8a0a + acadf4d commit 79f110f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 46 deletions.
15 changes: 7 additions & 8 deletions gc/base/standard/ConcurrentGCIncrementalUpdate.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018, 2023 IBM Corp. and others
* Copyright (c) 2018, 2021 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -790,20 +790,19 @@ MM_ConcurrentGCIncrementalUpdate::updateTuningStatisticsInternal(MM_EnvironmentB
if (_extensions->debugConcurrentMark) {
OMRPORT_ACCESS_FROM_ENVIRONMENT(env);

const size_t bufLen = 10;
char pass1Factor[bufLen];
char pass2Factor[bufLen];
char pass1Factor[10];
char pass2Factor[10];

if (_extensions->cardCleaningPasses > 0) {
omrstr_printf(pass1Factor, bufLen, "%.3f", _cardCleaningFactorPass1);
sprintf(pass1Factor, "%.3f", _cardCleaningFactorPass1);
} else {
omrstr_printf(pass1Factor, bufLen, "%s", "N/A");
sprintf(pass1Factor, "%s", "N/A");
}

if (_extensions->cardCleaningPasses > 1) {
omrstr_printf(pass2Factor, bufLen, "%.3f", _cardCleaningFactorPass2);
sprintf(pass2Factor, "%.3f", _cardCleaningFactorPass2);
} else {
omrstr_printf(pass2Factor, bufLen, "%s", "N/A");
sprintf(pass2Factor, "%s", "N/A");
}

omrtty_printf("Update tuning statistics: Total Traced=\"%zu\" (Pass 2 KO=\"%zu\") Total Cleaned=\"%zu\" (Pass 2 KO=\"%zu\")\n",
Expand Down
8 changes: 3 additions & 5 deletions tools/hookgen/HookGen.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2015, 2023 IBM Corp. and others
* Copyright (c) 2015, 2021 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -28,9 +28,6 @@
#if defined(OMR_OS_WINDOWS)
#include <windows.h>
#define strdup _strdup
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf(s, n, format, ...) _snprintf_s(s, n, _TRUNCATE, format, __VA_ARGS__)
#endif /* defined(_MSC_VER) && (_MSC_VER < 1900) */
#endif /* defined(OMR_OS_WINDOWS) */

#include "HookGen.hpp"
Expand Down Expand Up @@ -217,7 +214,8 @@ HookGen::writeEventToPublicHeader(const char *name, const char *description, con
char *exampleBuf = (char *)malloc(bufSize + 1);
if (NULL != exampleBuf) {
/* If we fail to allocate room for the example just skip writing it */
snprintf(exampleBuf, bufSize + 1, (char *)example, name, description, name, structName);
int length = sprintf(exampleBuf, (char *)example, name, description, name, structName);
exampleBuf[length] = '\0';

writeComment(_publicFile, exampleBuf);

Expand Down
6 changes: 2 additions & 4 deletions tools/tracegen/Port.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014, 2023 IBM Corp. and others
* Copyright (c) 2014, 2018 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -46,9 +46,7 @@
#if defined(OMR_OS_WINDOWS)
#define strdup _strdup
#define stat _stat
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf(s, n, format, ...) _snprintf_s(s, n, _TRUNCATE, format, __VA_ARGS__)
#endif /* defined(_MSC_VER) && (_MSC_VER < 1900) */
#define snprintf _snprintf
#define PATH_SEP "\\"
#define dirstat struct _stat
#define PORT_INVALID_FIND_FILE_HANDLE ((intptr_t) INVALID_HANDLE_VALUE)
Expand Down
40 changes: 15 additions & 25 deletions tools/tracegen/TraceHeaderWriter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014, 2023 IBM Corp. and others
* Copyright (c) 2014, 2017 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -214,9 +214,8 @@ TraceHeaderWriter::tpTemplate(FILE *fd, unsigned int overhead, unsigned int test
char *testNop = NULL;
char *testMacroTemplate = (char *) "#define TrcEnabled_%s (%s_UtActive[%u] != 0)\n";
char *testNopTemplate = (char *) "#define TrcEnabled_%s (0)\n";
size_t parmBufLen = (parmCount * 5) + 1;

parmString = (char *)Port::omrmem_calloc(1, parmBufLen);
parmString = (char *)Port::omrmem_calloc(1, (parmCount * sizeof(char) * 5) + 1);
if (NULL == parmString) {
eprintf("Failed to allocate memory");
goto failed;
Expand All @@ -230,31 +229,27 @@ TraceHeaderWriter::tpTemplate(FILE *fd, unsigned int overhead, unsigned int test
pos = parmString;

if (test) {
/* Allow 20 digits for tracepoints + 1 for the null byte. */
size_t bufLen = strlen(testMacroTemplate) + strlen(name) + strlen(module) + 21;
testMacro = (char *)Port::omrmem_calloc(1, bufLen);
/* Allow 7 digits for tracepoints + 1 for the null byte. (Millions of trace points are unlikely.) */
testMacro = (char *)Port::omrmem_calloc(1, (strlen(testMacroTemplate) + strlen(name) + strlen(module) + 8));
if (NULL == testMacro) {
eprintf("Failed to allocate memory");
goto failed;
}
snprintf(testMacro, bufLen, testMacroTemplate, name, module, id);
sprintf(testMacro, testMacroTemplate, name, module, id);

bufLen = strlen(testMacroTemplate) + strlen(name) + 1;
testNop = (char *)Port::omrmem_calloc(1, bufLen);
testNop = (char *)Port::omrmem_calloc(1, (strlen(testMacroTemplate) + strlen(name) + 1));
if (NULL == testNop) {
eprintf("Failed to allocate memory");
goto failed;
}
snprintf(testNop, bufLen, testNopTemplate, name);
sprintf(testNop, testNopTemplate, name);
} else {
testMacro = (char *) "";
testNop = (char *) "";
}

for (unsigned int i = 0; i < parmCount; i++) {
int n = snprintf(pos, parmBufLen, ", P%u", i + 1);
pos += n;
parmBufLen -= n;
pos += sprintf(pos, ", P%u", i + 1);
}

if (auxiliary) {
Expand Down Expand Up @@ -349,9 +344,8 @@ TraceHeaderWriter::tpAssert(FILE *fd, unsigned int overhead, unsigned int test,
char *testnop = NULL;
char *testmacrotemplate = (char *) "#define TrcEnabled_%s (%s_UtActive[%u] != 0)\n";
char *testnoptemplate = (char *) "#define TrcEnabled_%s (0)\n";
size_t parmBufLen = (parmCount * 5) + 1;

parmString = (char *)Port::omrmem_calloc(1, parmBufLen);
parmString = (char *)Port::omrmem_calloc(1, (parmCount * sizeof(char) * 5) + 1);
if (NULL == parmString) {
eprintf("Failed to allocate memory");
goto failed;
Expand All @@ -366,31 +360,27 @@ TraceHeaderWriter::tpAssert(FILE *fd, unsigned int overhead, unsigned int test,
pos = parmString;

if (test) {
/* Allow 20 digits for tracepoints + 1 for the null byte. */
size_t bufLen = strlen(testmacrotemplate) + strlen(name) + strlen(module) + 21;
testmacro = (char *)Port::omrmem_calloc(1, bufLen);
/* Allow 7 digits for tracepoints + 1 for the null byte. (Millions of trace points are unlikely.) */
testmacro = (char *)Port::omrmem_calloc(1, (strlen(testmacrotemplate) + strlen(name) + strlen(module) + 8));
if (NULL == testmacro) {
eprintf("Failed to allocate memory");
goto failed;
}
snprintf(testmacro, bufLen, testmacrotemplate, name, module, id);
sprintf(testmacro, testmacrotemplate, name, module, id);

bufLen = strlen(testmacrotemplate) + strlen(name) + 1;
testnop = (char *)Port::omrmem_calloc(1, bufLen);
testnop = (char *)Port::omrmem_calloc(1, (strlen(testmacrotemplate) + strlen(name) + 1));
if (NULL == testnop) {
eprintf("Failed to allocate memory");
goto failed;
}
snprintf(testnop, bufLen, testnoptemplate, name);
sprintf(testnop, testnoptemplate, name);
} else {
testmacro = (char *) "";
testnop = (char *) "";
}

for (unsigned int i = 0; i < parmCount; i++) {
int n = snprintf(pos, parmBufLen, ", P%u", i + 1);
pos += n;
parmBufLen -= n;
pos += sprintf(pos, ", P%u", i + 1);
}

if (0 <= fprintf(fd, TP_ASSERT_TEMPLATE
Expand Down
7 changes: 3 additions & 4 deletions tools/tracemerge/DATMerge.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014, 2023 IBM Corp. and others
* Copyright (c) 2014, 2020 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -149,9 +149,8 @@ DATMerge::merge(J9TDFOptions *options, const char *fromFileName)
if (0 != fclose(toFile)) {
perror("fclose error");
}
size_t bufLen = strlen(toFileName) + strlen(".new") + 1;
char *newToFileName = (char *)Port::omrmem_calloc(1, bufLen);
snprintf(newToFileName, bufLen, "%s.new", toFileName);
char *newToFileName = (char *)Port::omrmem_calloc(1, strlen(toFileName) + strlen(".new") + 1);
sprintf(newToFileName, "%s.new", toFileName);

char *buffer = (char *)Port::omrmem_calloc(1, 1024);

Expand Down

0 comments on commit 79f110f

Please sign in to comment.