Search code examples
c++c++20address-sanitizerstdformat

Address sanitizer stack-use-after-scope with std::format_args


I am getting a stack-use-after-scope error when trying to use std::format_args along with std::vformat(). A simple code to reproduce it is:

#include <format>
#include <iostream>
#include <string>

int main() {
    int i = 42;
    std::string s = "hello";
    std::format_args args = std::make_format_args(i, s);
    std::cout << std::vformat("Number: {}, Word: {}", args) << '\n';

    return 0;
}

My i and s variables should still be in scope and are not r-values, so I'm not sure what is triggering the error. If I remove args and make a call to std::make_format_args() directly in the call to std::vformat(), everything works as expected. What am I missing?

Here is my command to compile the above code: clang++-17 --std=c++23 -stdlib=libc++ -fsanitize=address fmt.cpp.


Solution

  • std::make_format_args(i, s); does not return std::format_args args. The result of the unspecified type can be implicitly converted to std::format_args args that keeps a dangling reference to some internal state in the returned temporary object.

    The proper usage is:

    auto args = std::make_format_args(i, s);
    

    https://godbolt.org/z/Tozs7a9bP

    The better usage is:

    std::cout << std::vformat("Number: {}, Word: {}", std::make_format_args(i, s)) << '\n';