微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

这是在libc中崩溃std :: search_n一个错误?

我尽可能缩小了这一点,似乎是一个错误
#include <algorithm>
#include <vector>

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

  // Does not crash
  std::vector<uint8_t> bs{1,2,1);

  return 0;
}

我得到

Segmentation fault: 11

我希望我没有使用std :: search_n不正确:)

目前,使用LLDB的方法似乎不可行.

版本信息:

$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

证据;)

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

int main(int argc,char *argv[])
{
  std::vector<uint8_t> bs{1,1);

  // std::vector<uint8_t> bs{1,0};
  // std::search_n(bs.begin(),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$

解决方法

它似乎是search_n中的一个错误,它也为我崩溃(Xcode 4.6.1).我认为在__search_n测试
if (__first == __s)  // return __last if no element matches __value_

需要是

if (__first >= __s)  // return __last if no element matches __value_

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

原文地址:https://www.jb51.cc/c/114471.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐