Skip to content

Incorrect ildasm output due to invalid printf format string. #121686

@achille-fouilleul

Description

@achille-fouilleul

Description

On Linux, with the code from the current main branch, 64-bit values are not all formatted correctly.

Reproduction Steps

Build on Linux, run artifacts/bin/coreclr/linux.x64.Debug/ildasm -all artifacts/bin/coreclr/linux.x64.Debug/IL/System.Private.CoreLib.dll

Expected behavior

The following would be expected:

//    Metadata Stream Header:
//              0x00000000 Reserved
//                    0x02 Major
//                    0x00 Minor
//                    0x05 Heaps
//                    0x0a Rid
//      0x00001f013fb7bf55 MaskValid
//      0x00c416003301fa00 Sorted

Actual behavior

Instead ildasm produces the following output:

//    Metadata Stream Header:
//              0x00000000 Reserved
//                    0x02 Major
//                    0x00 Minor
//                    0x05 Heaps
//                    0x0a Rid
//      0x%016I64x MaskValid
//      0x%016I64x Sorted

Regression?

No.

Known Workarounds

None known.

Configuration

Version: current main.
OS: Debian Linux.
Architecture: x64.
Probably not distro- or architecture-specific.

Other information

This is because the non-standard prefix I64 is used in format strings. The glibc's printf family of function does not recognize I64.
I64x, I64d, I64u, I64i are used in many places, see the output from git grep I64[IiUuDdXx] main -- *.cpp in I64.txt
Suggestion: use the standard PRIx64, PRId64 etc. macros from <cinttypes> instead. Example:

--- a/src/coreclr/ildasm/dasm.cpp
+++ b/src/coreclr/ildasm/dasm.cpp
@@ -1,6 +1,7 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
+#include <cinttypes>
 #include "ildasmpch.h"
 #include <crtdbg.h>
 #include <utilcode.h>
@@ -5460,9 +5461,9 @@ void DumpMetadataHeader(const char *szName, IMAGE_DATA_DIRECTORY *pDir, void* GU
         printLine(GUICookie,szStr);
         sprintf_s(szString,SZSTRING_SIZE,"//                    0x%02x Rid", pMDSH->Rid);
         printLine(GUICookie,szStr);
-        sprintf_s(szString,SZSTRING_SIZE,"//      0x%016I64x MaskValid", (ULONGLONG)GET_UNALIGNED_VAL64(&(pMDSH->MaskValid)));
+        sprintf_s(szString,SZSTRING_SIZE,"//      0x%016" PRIx64 " MaskValid", (uint64_t)GET_UNALIGNED_VAL64(&(pMDSH->MaskValid)));
         printLine(GUICookie,szStr);
-        sprintf_s(szString,SZSTRING_SIZE,"//      0x%016I64x Sorted", (ULONGLONG)GET_UNALIGNED_VAL64(&(pMDSH->Sorted)));
+        sprintf_s(szString,SZSTRING_SIZE,"//      0x%016" PRIx64 " Sorted", (uint64_t)GET_UNALIGNED_VAL64(&(pMDSH->Sorted)));
         printLine(GUICookie,szStr);
     }
 }

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions