Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace sprintf with snprintf in many places #20474

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pshipton
Copy link
Member

There is one remaining place that will require a coordinated merge with OMR.

Copy link
Contributor

@dsouzai dsouzai left a comment

Choose a reason for hiding this comment

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

I believe in the compiler files you need to use TR::snprintfTrunc; @jdmpapin could you confirm? (Something to do with windows and snprintf not being defined by default).

@jdmpapin
Copy link
Contributor

jdmpapin commented Nov 1, 2024

At call sites where we're really confident that the buffer is large enough, TR::snprintfNoTrunc would be better, since it asserts that there is no truncation. But for a blanket replacement I think it's fine not to try to make that determination

The point of TR::snprintfTrunc was to smooth over portability problems with MSVC. Apparently their snprintf has been fixed for some time:

Beginning with the UCRT in Visual Studio 2015 and Windows 10, snprintf is no longer identical to _snprintf. The snprintf behavior is now C99 standard conformant.

If we're OK limiting ourselves to versions of the MSVC runtime library that include this change, then plain snprintf will be fine. Otherwise, the compiler should still use TR::snprintfTrunc, and outside of the compiler you would probably want to take account of the fact that old MSVC snprintf can fail to null-terminate its output

@pshipton
Copy link
Member Author

pshipton commented Nov 1, 2024

We've moved up to VS2022, and VS2013 and earlier are out of support so I think it's fine.

runtime/compiler/control/CompilationThread.cpp Outdated Show resolved Hide resolved
runtime/compiler/control/J9CompilationStrategy.cpp Outdated Show resolved Hide resolved
curMsg += msgLen;
maxMsgLen -= msgLen;
}
else
Copy link
Contributor

Choose a reason for hiding this comment

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

Please correct the indentation here.

curMsg += msgLen;
maxMsgLen -= msgLen;
}
else
Copy link
Contributor

Choose a reason for hiding this comment

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

indentation

curMsg += msgLen;
maxMsgLen -= msgLen;
}
else
Copy link
Contributor

Choose a reason for hiding this comment

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

indentation; also several places below

@@ -222,8 +222,9 @@ class VM_JFRChunkWriter {
writeIntermediateJFRChunkToFile()
{
UDATA written = 0;
char fileName[sizeof(intermediateChunkFileName) + 16 + sizeof(".jfr")];
sprintf(fileName, "%s%lX.jfr", intermediateChunkFileName, _vm->jfrState.jfrChunkCount);
size_t fileNameLen = sizeof(intermediateChunkFileName) + 16 + sizeof(".jfr");
Copy link
Contributor

Choose a reason for hiding this comment

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

const may be necessary here

sprintf(fileName, "%s%lX.jfr", intermediateChunkFileName, _vm->jfrState.jfrChunkCount);
size_t fileNameLen = sizeof(intermediateChunkFileName) + 16 + sizeof(".jfr");
char fileName[fileNameLen];
snprintf(fileName, fileNameLen, "%s%lX.jfr", intermediateChunkFileName, _vm->jfrState.jfrChunkCount);
UDATA len = _bufferWriter->getSize();
IDATA fd = j9file_open(fileName, EsOpenWrite | EsOpenCreate | EsOpenTruncate , 0666);
Copy link
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, please remove the space before the last comma.

@@ -396,16 +396,20 @@ lswRecordSlot(J9StackWalkState * walkState, const void * slotAddress, UDATA slot


static char *
lswDescribeObject(J9VMThread * vmThread, char * dataPtr, J9Class ** class, J9Object * object)
lswDescribeObject(J9VMThread * vmThread, char * dataPtr, UDATA dataSize, J9Class ** class, J9Object * object)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest size_t is preferable to UDATA here and in the signature of lswDescribeSlot().

@@ -427,21 +431,30 @@ lswDescribeSlot(J9VMThread * vmThread, J9StackWalkState * walkState, J9SWSlot *
case LSW_TYPE_INDIRECT_O_SLOT:
case LSW_TYPE_O_SLOT:
if (slot->data) {

char *newPtr = dataPtr;
Copy link
Contributor

Choose a reason for hiding this comment

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

NULL is just as good for an initial value here.


className = J9ROMCLASS_CLASSNAME(class->romClass);
copySize = (J9UTF8_LENGTH(className) <= dataSize) ? J9UTF8_LENGTH(className) : LSW_STRING_MAX - 1;
copySize = (J9UTF8_LENGTH(className) < dataSize) ? J9UTF8_LENGTH(className) : (dataSize > 0 ? dataSize - 1 : 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing parentheses for (dataSize - 1).

@pshipton pshipton force-pushed the sprintf branch 2 times, most recently from 3dbe0ac to 6191097 Compare November 1, 2024 19:58
@pshipton
Copy link
Member Author

pshipton commented Nov 1, 2024

I've pushed an update to address the initial review comments.

@@ -2628,25 +2628,25 @@ TR_J9VMBase::printTruncatedSignature(char *sigBuf, int32_t bufLen, J9UTF8 *class
int32_t sigLen = J9UTF8_LENGTH(className) + J9UTF8_LENGTH(name) + J9UTF8_LENGTH(signature)+2;
if (sigLen < bufLen)
{
sigLen = sprintf(sigBuf, "%.*s.%.*s%.*s", J9UTF8_LENGTH(className), utf8Data(className),
sigLen = snprintf(sigBuf, bufLen, "%.*s.%.*s%.*s", J9UTF8_LENGTH(className), utf8Data(className),
Copy link
Contributor

Choose a reason for hiding this comment

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

A cast should be added until then.

@@ -7051,7 +7054,7 @@ TR_J9VM::sampleSignature(TR_OpaqueMethodBlock * aMethod, char *buf, int32_t bufL
int32_t len = J9UTF8_LENGTH(className)+J9UTF8_LENGTH(name)+J9UTF8_LENGTH(signature)+3;
char * s = len <= bufLen ? buf : (memory ? (char*)memory->allocateHeapMemory(len) : NULL);
if (s)
sprintf(s, "%.*s.%.*s%.*s", J9UTF8_LENGTH(className), utf8Data(className), J9UTF8_LENGTH(name), utf8Data(name), J9UTF8_LENGTH(signature), utf8Data(signature));
snprintf(s, len, "%.*s.%.*s%.*s", J9UTF8_LENGTH(className), utf8Data(className), J9UTF8_LENGTH(name), utf8Data(name), J9UTF8_LENGTH(signature), utf8Data(signature));
Copy link
Contributor

Choose a reason for hiding this comment

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

A cast should be used here ((size_t)len).

sprintf(annNameBuffer,"parm(%d)",(flag & ANNOTATION_PARM_MASK) >> ANNOTATION_PARM_SHIFT);
TR_ASSERT( strlen(annNameBuffer) < ANNO_NAMEBUF_LEN, "buffer length somehow exceeded\n");
size_t len = snprintf(annNameBuffer, sizeof(annNameBuffer), "parm(%d)",(flag & ANNOTATION_PARM_MASK) >> ANNOTATION_PARM_SHIFT);
TR_ASSERT( len < sizeof(annNameBuffer), "buffer length somehow exceeded\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove the space after the opening (.

@@ -5059,7 +5068,8 @@ TR_J9ByteCodeIlGenerator::runMacro(TR::SymbolReference * symRef)
char *secondArgType = nextSignatureArgument(arrayElementType);
int arrayElementTypeLength = secondArgType - arrayElementType;

char *expandedArgsSignature = (char*)comp()->trMemory()->allocateStackMemory(numElements * arrayElementTypeLength + 1);
size_t argsLen = numElements * arrayElementTypeLength + 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing parentheses:

            size_t argsLen = (numElements * arrayElementTypeLength) + 1;

There is one remaining place that will require a coordinated merge with
OMR.

Signed-off-by: Peter Shipton <[email protected]>
@pshipton
Copy link
Member Author

pshipton commented Nov 1, 2024

Pushed the new updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants