microsoft / microsoft/STL

<valarray>: `slice_array`/`gslice_array` incorrectly bind to function arguments of type `slice`/`gslice`

Open
#1,630 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the bug

MSVC STL's slice_array and gslice_array derive from slice and gslice, respectively.
https://github.com/microsoft/STL/blob/47cd70315d560e4fe1ac699c0eb8ec8bfc5cb658/stl/inc/valarray#L1382-L1384
https://github.com/microsoft/STL/blob/47cd70315d560e4fe1ac699c0eb8ec8bfc5cb658/stl/inc/valarray#L1560-L1562

When a function overload set with an overload for both valarray<bool> and slice is called with a slice_array<bool> argument, the valarray<bool> overload should be selected (slice_array<bool> is implicitly convertible to valarray<bool>) according to the standard. However, with MSVC STL, the slice overload is incorrectly selected because it is the base class of slice_array<bool>, and derived-to-base conversion is better than user-defined conversion in overload resolution.

Command-line test case

D:\Temp>type slice.cpp
#include <cassert>
#include <valarray>
using namespace std;

int main() {
    valarray<int> a(10);
    valarray<bool> b(false, 20);
    valarray<int> c = a[b[slice(0, 10, 2)]];
    assert(c.size() == 0);
    return 0;
}
D:\Temp>cl /EHsc /W4 /WX slice.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29812 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

slice.cpp
Microsoft (R) Incremental Linker Version 14.28.29812.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:slice.exe
slice.obj

D:\Temp>.\slice.exe
Assertion failed: c.size() == 0, file slice.cpp, line 9

STL version

https://github.com/microsoft/STL/commit/c7b14059332afcc512ba245390bb0acf5bbbb0ed

Additional context

This bug is discovered when reviewing #1627, where slice_array<int> == slice_array<int> incorrectly calls the newly added operator==(const slice&, const slice&).

vNext note: Resolving this issue will require breaking binary compatibility. We won't be able to accept pull requests for this issue until the vNext branch is available. See #169 for more information.

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 stl/inc/valarray at the slice_array and gslice_array definitions linked in the issue, then run the provided slice.cpp command-line test case. The fix is blocked until the vNext branch is available because it requires breaking binary compatibility; done means the valarray overload is selected instead of the slice overload.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.