Repository navigation
Replace sprintf() with snprintf() - #6862
Conversation
|
This commit partially fixes eclipse-openj9/openj9#16517. |
dmitripivkine
left a comment
There was a problem hiding this comment.
Change in ConcurrentGCIncrementalUpdate.cpp looks good to me
| if (NULL != exampleBuf) { | ||
| /* If we fail to allocate room for the example just skip writing it */ | ||
| int length = sprintf(exampleBuf, (char *)example, name, description, name, structName); | ||
| exampleBuf[length] = '\0'; |
There was a problem hiding this comment.
I removed this line because snprintf() always terminates the output with '\0'.
|
Build jobs in eclipse-openj9/openj9#16550 show that this commit does not break OpenJ9 builds. |
|
|
||
| if (_extensions->cardCleaningPasses > 0) { | ||
| sprintf(pass1Factor, "%.3f", _cardCleaningFactorPass1); | ||
| snprintf(pass1Factor, bufLen, "%.3f", _cardCleaningFactorPass1); |
There was a problem hiding this comment.
I don't think snprintf is available with all compilers (e.g. some older Visual Studio versions). We need to check that it's available in the minimum version supported by OMR (and downstream projects).
There was a problem hiding this comment.
OpenJ9 uses Visual Studio 2017 as a minimum, so that project should have no problem with this change.
I couldn't find anything in OMR itself that specifies a minimum version, but I did find this code in omrsig_internal.hpp that anticipates support for several older versions.
There was a problem hiding this comment.
I found this line in doc/CodingStandard.md:
sprintf() should not be used. Use the omrstr_printf() function from the port library, instead.
There was a problem hiding this comment.
I updated the code by using omrstr_printf() in ConcurrentGCIncrementalUpdate.cpp.
|
What would be the best way for eliminating |
Things in the |
|
I added the macro definition of |
| #if defined(OMR_OS_WINDOWS) | ||
| #include <windows.h> | ||
| #define strdup _strdup | ||
| #if defined(_MSC_VER) && _MSC_VER < 1900 |
There was a problem hiding this comment.
Parentheses please:
#if defined(_MSC_VER) && (_MSC_VER < 1900)
| #define strdup _strdup | ||
| #define stat _stat | ||
| #define snprintf _snprintf | ||
| #if defined(_MSC_VER) && _MSC_VER < 1900 |
| 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 * sizeof(char) * 5) + 1; |
There was a problem hiding this comment.
My understanding is that the language guarantees that sizeof(char) == 1. I'm not sure the point of multiplying by sizeof(char), or is it just for clarity?
There was a problem hiding this comment.
It is taken from the original code:
parmString = (char *)Port::omrmem_calloc(1, (parmCount * sizeof(char) * 5) + 1);
I removed * sizeof(char).
| if (test) { | ||
| /* 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)); | ||
| size_t bufLen = strlen(testMacroTemplate) + strlen(name) + strlen(module) + 8; |
There was a problem hiding this comment.
While you're here, I see no reason not to allow room for the most digits possible in unsigned int (i.e. 7 -> 19) removing that assumption.
There was a problem hiding this comment.
Unsigned int requires 10 digits (4294967295) in 32 bit, and 20 digits (18446744073709551615) in the ILP64 model.
The author of the original code says (Millions of trace points are unlikely.) in the comment.
There was a problem hiding this comment.
Changed the code and the comment to allow 20 digits for %u.
This commit replaces sprintf() with snprintf(). Signed-off-by: KONNO Kazuhiro <konno@jp.ibm.com>
|
The PortSockTest failures on x86 macOS is independent of this PR. See Issue #6516. |
|
Jenkins build all |
|
I am satisfied the RISC-V failure is not related to this PR. Merging. |
|
This change hasn't yet promoted to OpenJ9, but considering #6881 broke z/OS IBM Java 8 compilation on V2R1, I expect this one will as well. |
|
I opened #6895 for reverting this. |
This commit replaces sprintf() with snprintf().
Signed-off-by: KONNO Kazuhiro konno@jp.ibm.com