Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endless loop in deque::shrink_to_fit() #4954

Open
CopaDataDevelopment opened this issue Sep 13, 2024 · 3 comments · May be fixed by #4955
Open

Endless loop in deque::shrink_to_fit() #4954

CopaDataDevelopment opened this issue Sep 13, 2024 · 3 comments · May be fixed by #4955
Labels
bug Something isn't working

Comments

@CopaDataDevelopment
Copy link

Describe the bug

Commit c40a251 (fixing issue #4091, merged into master in issue #4072) introduced an endless loop into deque::shrink_to_fit(). In specific constellations in regards to _First_used_block_idx, _First_unused_block_idx and _Mask the first loop for deallocating unused blocks runs infinetely.

Command-line test case

#define _CRT_SECURE_NO_WARNINGS

#include <windows.h>
#include <math.h>
#include <deque>

int main()
{
  std::deque<int> qu;

  long it = 0;
  while (1)
  {
    size_t numAlloc = rand() + 1;
    for (size_t i = 0; i < numAlloc; i++)
    {
      qu.push_back(0);
    }

    size_t numDealloc = rand() + 1;
    if (it % 100 == 0 || numDealloc > qu.size())
    {
      numDealloc = qu.size();
    }
    for (size_t i = 0; i < numDealloc; i++)
    {
      qu.pop_front();
    }
    qu.shrink_to_fit();

    printf("iteration %d: %lld\n", ++it, qu.size());
  }

  return 0;
}

After about 40 iterations deque::shrink_to_fit get's stuck.

Expected behavior

Termination condition for loop is correct so deque::shrink_to_fit() terminates.

STL version

  • Visual Studio version
    Microsoft Visual Studio Enterprise 2022 
    Version 17.10
    
  • Visual Studio version
    Microsoft Visual Studio Enterprise 2022 
    Version 17.11.1
    
@frederick-vs-ja
Copy link
Contributor

_First_used_block_idx was incorrectly unmasked. In the problematic case, _First_used_block_idx became larger than _Mask, so the loop never ended. I'm fixing this.

@CDAlexanderW
Copy link

CDAlexanderW commented Sep 13, 2024

Can we get this fix backported to 17.11.x please?

@CaseyCarter CaseyCarter added the bug Something isn't working label Sep 13, 2024
@StephanTLavavej
Copy link
Member

Given the timing of when this was reported, I don't believe that 17.11.x will be feasible (as it's odd-numbered and not long-term support). We can likely get it into 17.12 before GA. It may be worth backporting to 17.10, the original release where it regressed, although that's triple the work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
5 participants