C++ Logo

std-discussion

Advanced search

Re: format_to (a char array) easy to misuse

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Sun, 2 Oct 2022 13:32:54 +0200
niedz., 2 paź 2022 o 10:35 Olaf van der Spek via Std-Discussion
<std-discussion_at_[hidden]> napisał(a):
>
> 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;
> }

This is more of a problem with raw pointers than this function.
Example on https://en.cppreference.com/w/cpp/utility/format/format_to
show `std::back_inserter(buffer)`.
There are multiple algorithms that work only with a single output iterator.

Question is should standard add a new adapter for input iterator for
filling bound ranges?
Something like `std::range_fill(std::ranges::range auto& x)` that
returns an output iterator that thrown when you try
write past the end.

> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion

Received on 2022-10-02 11:33:05