microsoft / microsoft/STL

`<format>`: `format_to_n` missing difference type casting

Open
#3,271 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug format
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

This bug belongs to difference type casting category.

https://github.com/microsoft/STL/blob/4483e87f6e5aa5c4598404fc01eecb7e547dd439/stl/inc/format#L3551-L3557

The type of _Max is iter_difference_t<O>, but the parameter type of _Fmt_iterator_buffer is ptrdiff_t, which does not guarantee that it can be implicitly converted by the former.

contrived testcase:

#include <ranges>
#include <format>
#include <vector>
#include <iostream>

int main() {
  std::vector<char> v(10);
  auto r = std::views::iota(0ULL) | 
    std::views::transform([&](std::size_t i) -> auto& { return v[i]; });
  auto str = std::format_to_n(r.begin(), 5, "{}", "hello");
  std::cout << v.data() << "\n";
}

https://godbolt.org/z/5doen3P5d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at stl/inc/format around lines 3551-3557 and compare the _Max and _Fmt_iterator_buffer parameter types with the provided format_to_n testcase. Verify the corrected difference-type handling using that testcase and confirm the formatting behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.