microsoft / microsoft/STL

Containers should correctly handle unusual `size_type`/`difference_type`

Open
#5,546 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the bug

Currently, the following program doesn't compile with MSVC STL.

#include <memory>
#include <string>

template <class T>
struct small_ator {
    using value_type = T;

    using size_type       = unsigned short; // !!!
    using difference_type = short; // !!!

    small_ator() = default;
    template <class U>
    constexpr small_ator(const small_ator<U>&) noexcept {}

    T* allocate(size_type n) {
        return std::allocator<T>{}.allocate(n);
    }

    void deallocate(T* p, size_type n) {
        return std::allocator<T>{}.deallocate(p, n);
    }

    friend bool operator==(const small_ator&, const small_ator&) = default;
    template <class U>
    friend constexpr bool operator==(const small_ator&, const small_ator<U>&) {
        return true;
    }
};

int main() {
    std::basic_string<char, std::char_traits<char>, small_ator<char>> s = "hello world";
}

In this case, the size_type is changed in integral promotion, so std::max doesn't work.

Command-line test case

https://godbolt.org/z/xdcqab8xj

Expected behavior

This program compiles, with no C4244 warning (or its friend).

STL version

Effectively, all existing versions since open-sourcing.

Additional context

We should also audit cases where

  • the size_type or difference_type is narrower than int, or
  • on 32-bit platforms, the size_type or difference_type are 64-bit.

And keeps that there're no compile errors or warnings on narrowing, for all containers and container adaptors that can have unusual size_type and difference_type types.

Tasks

Containers:

  • basic_string
  • deque
  • forward_list
  • hive
  • list
  • vector
  • map
  • multimap
  • set
  • multiset
  • unordered_map
  • unordered_multimap
  • unordered_set
  • unordered_multiset

(array and inplace_vector are unrelated, as their size_type and difference_type must be size_t and ptrdiff_t respectively.)

Container adaptors (need to verify adaption of standard sequence containers):

  • priority_queue
  • queue
  • stack
  • flat_map
  • flat_multimap
  • flat_set
  • flat_multiset

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 with the linked Godbolt reproduction and trace the basic_string implementation, then inspect the listed container and adaptor entry points for unusual size_type and difference_type handling. Done means the listed cases compile without narrowing warnings or errors, including the provided small_ator example and the requested 32-bit variations.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.