Skip to content

MemorySanitizer: use-of-uninitialized-value #4609

@xTachyon

Description

@xTachyon

This code:

#include <cstdio>
#include <fmt/color.h>
#include <fmt/format.h>

int main() {
    printf("a");
    fmt::print(fmt::fg({}), "b");
}

Platform: Ubuntu 24.04.3
Compiler: Ubuntu clang version 21.1.5 (++20251023083335+45afac62e373-1exp120251023083454.54)

Compiled and ran with memory sanitizer on the latest fmt master produces the following:

Uninitialized bytes in memchr at offset 0 inside [0x718000000000, 2)
==1877268==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x59bbcbe3000f in fmt::v12::detail::glibc_file<_IO_FILE>::needs_flush() const .../fmt/include/fmt/format-inl.h:1552:12
    #1 0x59bbcbcfd4de in fmt::v12::detail::file_print_buffer<_IO_FILE, void>::~file_print_buffer() .../fmt/include/fmt/format-inl.h:1687:24
    #2 0x59bbcbcf0491 in fmt::v12::vprint(_IO_FILE*, fmt::v12::basic_string_view<char>, fmt::v12::basic_format_args<fmt::v12::context>) .../fmt/include/fmt/format-inl.h:1742:1
    #3 0x59bbcbcdd0c8 in void fmt::v12::print<fmt::v12::basic_string_view<char>>(_IO_FILE*, fmt::v12::fstring<fmt::v12::basic_string_view<char>>::t, fmt::v12::basic_string_view<char>&&) .../fmt/include/fmt/base.h:2982:39
    #4 0x59bbcbcdb9b8 in fmt::v12::vprint(_IO_FILE*, fmt::v12::text_style, fmt::v12::basic_string_view<char>, fmt::v12::basic_format_args<fmt::v12::context>) .../fmt/include/fmt/color.h:500:3
    #5 0x59bbcbcdac96 in void fmt::v12::print<>(_IO_FILE*, fmt::v12::text_style, fmt::v12::fstring<>::t) .../fmt/include/fmt/color.h:514:3
    #6 0x59bbcbcda03c in void fmt::v12::print<>(fmt::v12::text_style, fmt::v12::fstring<>::t) .../fmt/include/fmt/color.h:528:10
    #7 0x59bbcbcd9c73 in main .../tmp/cmake_test/the_bin/main.cpp:7:5
    #8 0x7ae21ee2a1c9 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    #9 0x7ae21ee2a28a in __libc_start_main csu/../csu/libc-start.c:360:3
    #10 0x59bbcbc3b574 in _start (.../tmp/cmake_test/build/the_bin/the_bin+0x34574) (BuildId: eb41b054cafd37969ac772da3dfac73234bb46c7)

  Uninitialized value was created by a heap allocation
    #0 0x59bbcbc7a566 in malloc (.../tmp/cmake_test/build/the_bin/the_bin+0x73566) (BuildId: eb41b054cafd37969ac772da3dfac73234bb46c7)
    #1 0x7ae21ee851b4 in _IO_file_doallocate libio/filedoalloc.c:101:7
    #2 0x7ae21ee95523 in _IO_doallocbuf libio/genops.c:347:9
    #3 0x7ae21ee92f8f in _IO_file_overflow libio/fileops.c:745:4
    #4 0x7ae21ee93aae in _IO_new_file_xsputn libio/fileops.c:1244:11
    #5 0x7ae21ee93aae in _IO_file_xsputn libio/fileops.c:1197:1
    #6 0x7ae21ee60cc8 in __printf_buffer_flush_to_file stdio-common/printf_buffer_to_file.c:59:20
    #7 0x7ae21ee60cc8 in __printf_buffer_to_file_done stdio-common/printf_buffer_to_file.c:120:3
    #8 0x7ae21ee6b742 in __vfprintf_internal stdio-common/vfprintf-internal.c:1545:14
    #9 0x59bbcbc84cc7 in vprintf (.../tmp/cmake_test/build/the_bin/the_bin+0x7dcc7) (BuildId: eb41b054cafd37969ac772da3dfac73234bb46c7)

SUMMARY: MemorySanitizer: use-of-uninitialized-value .../fmt/include/fmt/format-inl.h:1552:12 in fmt::v12::detail::glibc_file<_IO_FILE>::needs_flush() const
Exiting

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions