libc ++ std :: search_n中的这个崩溃是一个错误吗?

dpj*_*dpj 10 c++ stl c++11 libc++

我尽可能地缩小了它,这似乎是一个错误......

#include <algorithm>
#include <vector>

int main(int argc, char *argv[])
{
  // Crashes
  std::vector<uint8_t> bs{1, 0, 0};
  std::search_n(bs.begin(), bs.end(), 3, 1);

  // Does not crash
  std::vector<uint8_t> bs{1, 0};
  std::search_n(bs.begin(), bs.end(), 2, 1);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我明白了

Segmentation fault: 11
Run Code Online (Sandbox Code Playgroud)

我希望我没有错误地使用std :: search_n :)

目前使用LLDB似乎无法单步执行STL实现.

版本信息:

$clang --version
Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.3.0
Thread model: posix
Run Code Online (Sandbox Code Playgroud)

证据;)

13:06:47 ~/bug$ cat bug.cc
#include <algorithm>
#include <vector>

int main(int argc, char *argv[])
{
  std::vector<uint8_t> bs{1, 0, 0};
  std::search_n(bs.begin(), bs.end(), 3, 1);

  // std::vector<uint8_t> bs{1, 0};
  // std::search_n(bs.begin(), bs.end(), 2, 1);

  return 0;
}
13:06:52 ~/bug$ clang++ -std=c++11 -stdlib=libc++ bug.cc -o bug
13:07:36 ~/bug$ ./bug
Segmentation fault: 11
13:07:42 ~/bug$
Run Code Online (Sandbox Code Playgroud)

小智 6

它似乎是search_n中的一个错误,它也为我崩溃了(Xcode 4.6.1).我想在__search_n中测试

if (__first == __s)  // return __last if no element matches __value_
Run Code Online (Sandbox Code Playgroud)

需要是

if (__first >= __s)  // return __last if no element matches __value_
Run Code Online (Sandbox Code Playgroud)

会发生什么是算法开始匹配,然后不匹配并重新开始; 这个新的起始点超出__s,这是模式长度的逻辑最后可能的起始点.旧测试只测试了平等性,而不是"超越性".有了修复,它不再对我崩溃.

  • 同意这是正确的解决方案.感谢错误报告和修复程序.承诺修订178764. (2认同)