Skip to content

Replace sprintf() with snprintf() - #6862

Merged
0xdaryl merged 1 commit into
eclipse-omr:masterfrom
knn-k:useSnprintf
Feb 15, 2023
Merged

0xdaryl merged 1 commit into
eclipse-omr:masterfrom
knn-k:useSnprintf

Conversation

@knn-k

@knn-k knn-k commented Jan 12, 2023

Copy link
Copy Markdown
Contributor

This commit replaces sprintf() with snprintf().

Signed-off-by: KONNO Kazuhiro konno@jp.ibm.com

@knn-k

knn-k commented Jan 12, 2023

Copy link
Copy Markdown
Contributor Author

This commit partially fixes eclipse-openj9/openj9#16517.

@dmitripivkine dmitripivkine left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change in ConcurrentGCIncrementalUpdate.cpp looks good to me

Comment thread tools/hookgen/HookGen.cpp
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';

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this line because snprintf() always terminates the output with '\0'.

@knn-k

knn-k commented Jan 13, 2023

Copy link
Copy Markdown
Contributor Author

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this line in doc/CodingStandard.md:

sprintf() should not be used. Use the omrstr_printf() function from the port library, instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the code by using omrstr_printf() in ConcurrentGCIncrementalUpdate.cpp.

@knn-k

knn-k commented Jan 18, 2023

Copy link
Copy Markdown
Contributor Author

What would be the best way for eliminating sprintf() in the tools directory, where you cannot simply call omrstr_printf()?

@keithc-ca

Copy link
Copy Markdown
Contributor

What would be the best way for eliminating sprintf() in the tools directory, where you cannot simply call omrstr_printf()?

Things in the tools directory cannot use the port library (they are intended to run on the host, not the target), so I think snprintf() is probably the right answer.

@knn-k
knn-k marked this pull request as draft February 2, 2023 04:41
@knn-k
knn-k marked this pull request as ready for review February 2, 2023 05:12
@knn-k

knn-k commented Feb 2, 2023

Copy link
Copy Markdown
Contributor Author

I added the macro definition of snprintf for Visual Studio older than 2015.
I don't have the build environment for testing it, however.

Comment thread tools/hookgen/HookGen.cpp Outdated
#if defined(OMR_OS_WINDOWS)
#include <windows.h>
#define strdup _strdup
#if defined(_MSC_VER) && _MSC_VER < 1900

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parentheses please:

#if defined(_MSC_VER) && (_MSC_VER < 1900)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment thread tools/tracegen/Port.hpp Outdated
#define strdup _strdup
#define stat _stat
#define snprintf _snprintf
#if defined(_MSC_VER) && _MSC_VER < 1900

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing parentheses.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment thread tools/tracegen/TraceHeaderWriter.cpp Outdated
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is taken from the original code:

parmString = (char *)Port::omrmem_calloc(1, (parmCount * sizeof(char) * 5) + 1);

I removed * sizeof(char).

Comment thread tools/tracegen/TraceHeaderWriter.cpp Outdated
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@knn-k

knn-k commented Feb 3, 2023

Copy link
Copy Markdown
Contributor Author

The PortSockTest failures on x86 macOS is independent of this PR. See Issue #6516.

@0xdaryl 0xdaryl self-assigned this Feb 14, 2023
@0xdaryl

0xdaryl commented Feb 14, 2023

Copy link
Copy Markdown
Contributor

Jenkins build all

@0xdaryl

0xdaryl commented Feb 15, 2023

Copy link
Copy Markdown
Contributor

I am satisfied the RISC-V failure is not related to this PR. Merging.

@0xdaryl
0xdaryl merged commit cd9f5d0 into eclipse-omr:master Feb 15, 2023
@knn-k
knn-k deleted the useSnprintf branch February 15, 2023 04:26
@knn-k knn-k mentioned this pull request Feb 15, 2023
@pshipton

Copy link
Copy Markdown
Contributor

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.

@knn-k

knn-k commented Feb 15, 2023

Copy link
Copy Markdown
Contributor Author

I opened #6895 for reverting this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants