Skip to content

Commit 0f329e0

Browse files
[sanitizer_symbolizer] Cast arguments for format strings in markup (#89815)
When compiling the common sanitizer libraries, there are many warnings about format specifiers, similar to: compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup.cpp:31:32: warning: format specifies type 'void *' but the argument has type 'uptr' (aka 'unsigned long') [-Wformat] 31 | buffer->AppendF(kFormatData, DI->start); | ~~~~~~~~~~~ ^~~~~~~~~ compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup_constants.h:33:46: note: format string is defined here 33 | constexpr const char *kFormatData = "{{{data:%p}}}"; | ^~ | %lu compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup.cpp:46:43: warning: format specifies type 'void *' but the argument has type 'uptr' (aka 'unsigned long') [-Wformat] 46 | buffer->AppendF(kFormatFrame, frame_no, address); | ~~~~~~~~~~~~ ^~~~~~~ compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup_constants.h:36:48: note: format string is defined here 36 | constexpr const char *kFormatFrame = "{{{bt:%u:%p}}}"; | ^~ | %lu ... This is because `uptr` is dependent on the platform, and can be either `unsigned long long`, `unsigned long`, or `unsigned int`. To fix the warnings, cast the arguments to the expected type of the format strings.
1 parent 39ed3c6 commit 0f329e0

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void MarkupStackTracePrinter::RenderData(InternalScopedString *buffer,
2828
const char *format, const DataInfo *DI,
2929
const char *strip_path_prefix) {
3030
RenderContext(buffer);
31-
buffer->AppendF(kFormatData, DI->start);
31+
buffer->AppendF(kFormatData, reinterpret_cast<void *>(DI->start));
3232
}
3333

3434
bool MarkupStackTracePrinter::RenderNeedsSymbolization(const char *format) {
@@ -43,12 +43,13 @@ void MarkupStackTracePrinter::RenderFrame(InternalScopedString *buffer,
4343
const char *strip_path_prefix) {
4444
CHECK(!RenderNeedsSymbolization(format));
4545
RenderContext(buffer);
46-
buffer->AppendF(kFormatFrame, frame_no, address);
46+
buffer->AppendF(kFormatFrame, frame_no, reinterpret_cast<void *>(address));
4747
}
4848

4949
bool MarkupSymbolizerTool::SymbolizePC(uptr addr, SymbolizedStack *stack) {
5050
char buffer[kFormatFunctionMax];
51-
internal_snprintf(buffer, sizeof(buffer), kFormatFunction, addr);
51+
internal_snprintf(buffer, sizeof(buffer), kFormatFunction,
52+
reinterpret_cast<void *>(addr));
5253
stack->info.function = internal_strdup(buffer);
5354
return true;
5455
}
@@ -118,7 +119,8 @@ static void RenderMmaps(InternalScopedString *buffer,
118119
// module.base_address == dlpi_addr
119120
// range.beg == dlpi_addr + p_vaddr
120121
// relative address == p_vaddr == range.beg - module.base_address
121-
buffer->AppendF(kFormatMmap, range.beg, range.end - range.beg, moduleId,
122+
buffer->AppendF(kFormatMmap, reinterpret_cast<void *>(range.beg),
123+
range.end - range.beg, static_cast<int>(moduleId),
122124
accessBuffer.data(), range.beg - module.base_address());
123125

124126
buffer->Append("\n");

compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup_constants.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ constexpr uptr kFormatFunctionMax = 64; // More than big enough for 64-bit hex.
3333
constexpr const char *kFormatData = "{{{data:%p}}}";
3434

3535
// One frame in a backtrace (printed on a line by itself).
36-
constexpr const char *kFormatFrame = "{{{bt:%u:%p}}}";
36+
constexpr const char *kFormatFrame = "{{{bt:%d:%p}}}";
3737

3838
// Module contextual element.
39-
constexpr const char *kFormatModule = "{{{module:%d:%s:elf:%s}}}";
39+
constexpr const char *kFormatModule = "{{{module:%zu:%s:elf:%s}}}";
4040

4141
// mmap for a module segment.
42-
constexpr const char *kFormatMmap = "{{{mmap:%p:0x%x:load:%d:%s:0x%x}}}";
42+
constexpr const char *kFormatMmap = "{{{mmap:%p:0x%zx:load:%d:%s:0x%zx}}}";
4343

4444
// Dump trigger element.
4545
#define FORMAT_DUMPFILE "{{{dumpfile:%s:%s}}}"

0 commit comments

Comments
 (0)