From acadf4d2521f797ea6a869c002b47932dd7b9670 Mon Sep 17 00:00:00 2001 From: KONNO Kazuhiro <33821890+knn-k@users.noreply.github.com> Date: Thu, 16 Feb 2023 08:57:10 +0900 Subject: [PATCH] Revert "Replace sprintf() with snprintf()" --- .../ConcurrentGCIncrementalUpdate.cpp | 15 ++++--- tools/hookgen/HookGen.cpp | 8 ++-- tools/tracegen/Port.hpp | 6 +-- tools/tracegen/TraceHeaderWriter.cpp | 40 +++++++------------ tools/tracemerge/DATMerge.cpp | 7 ++-- 5 files changed, 30 insertions(+), 46 deletions(-) diff --git a/gc/base/standard/ConcurrentGCIncrementalUpdate.cpp b/gc/base/standard/ConcurrentGCIncrementalUpdate.cpp index 8644a3d78b..e0f56b8f7f 100644 --- a/gc/base/standard/ConcurrentGCIncrementalUpdate.cpp +++ b/gc/base/standard/ConcurrentGCIncrementalUpdate.cpp @@ -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 @@ -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", diff --git a/tools/hookgen/HookGen.cpp b/tools/hookgen/HookGen.cpp index 729b7e1855..cfff242543 100644 --- a/tools/hookgen/HookGen.cpp +++ b/tools/hookgen/HookGen.cpp @@ -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 @@ -28,9 +28,6 @@ #if defined(OMR_OS_WINDOWS) #include #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" @@ -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); diff --git a/tools/tracegen/Port.hpp b/tools/tracegen/Port.hpp index 4d11596e20..1f53f68c5a 100644 --- a/tools/tracegen/Port.hpp +++ b/tools/tracegen/Port.hpp @@ -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 @@ -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) diff --git a/tools/tracegen/TraceHeaderWriter.cpp b/tools/tracegen/TraceHeaderWriter.cpp index 57c05e1795..c0ad864411 100644 --- a/tools/tracegen/TraceHeaderWriter.cpp +++ b/tools/tracegen/TraceHeaderWriter.cpp @@ -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 @@ -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; @@ -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) { @@ -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; @@ -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 diff --git a/tools/tracemerge/DATMerge.cpp b/tools/tracemerge/DATMerge.cpp index 73c95e38d8..3b265346bc 100644 --- a/tools/tracemerge/DATMerge.cpp +++ b/tools/tracemerge/DATMerge.cpp @@ -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 @@ -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);