C++ Logo

std-discussion

Advanced search

format_to (a char array) easy to misuse

From: Olaf van der Spek <ml_at_[hidden]>
Date: Sun, 2 Oct 2022 10:35:19 +0200
Hi,

This snippet overflows the destination buffer. Is format_to too easy to misuse?
Shouldn't it requires an iterator to the end so it could avoid this issue?
It seems like a modern equivalent of fgets()

#include <format>
#include <iostream>

int main()
{
  char d[10] = {};
  std::format_to(d, "0123456789 {}", 42);
  std::cout << std::string_view(d, 10) << std::endl;
}

Received on 2022-10-02 08:35:30