g++ 中抛出异常是否存在已知问题? Purify 导致 SIGABRT?

发布于 2024-10-15 18:01:46 字数 1448 浏览 1 评论 0原文

实际上,我真正的问题是:以下代码中指示的行是否有任何问题(“导致 SIGABRT”):

char* myFunc(char *param) {
  char* leaked = new char[80]; // Intentionally leaked
  if (param == NULL) throw logic_error("Parameter was null!"); // Causes SIGABRT
  strcpy(leaked, param);
  // Missing return, but never gets this far, so should be okay.
}

void test_non_happy_myFunc()
{
  try {
    myFunc(NULL);
  } catch (logic_error&) {
    cout << "Test succeeded!" << endl;
    return;
  }

  cout << "Test FAILED!" << endl;
}

int main()
{
  test_non_happy_myFunc();
}

我正在尝试提出一个最小的测试用例发送给 IBM/Rational 以证明存在问题与他们的净化软件,所以我首先由 SO 社区运行它。是的,我故意在第二行泄漏内存,是的,我知道抛出异常时指针“泄漏”被初始化。

当使用 g++ 编译时,上面的代码在 purify 之外正常运行,但在 purify 内部运行时会导致核心转储。我在上面的代码中是否犯了一个菜鸟错误(使 SIGABRT 成为我的错),或者我可以将矛头指向 IBM/Rational Purify?

编辑:(说明)

在没有 purify 的情况下在命令行上运行,上面的完整程序打印:

Test succeeded!

在 purify 内部运行,purify 报告:

COR: Fatal core dump
This is occurring while in thread 1299:
        _p450static    [rtlib.o]
        abort          [libc.so.6]
        uw_init_context_1 [unwind-dw2.c:1256]
        _Unwind_RaiseException [unwind.inc:88]
        __cxa_throw    [eh_throw.cc:78]
        myFunc(char*)  [exception_test.cc:9]
        test_non_happy_myFunc() [exception_test.cc:17]
        main           [exception_test.cc:28]

请注意,在先决条件包含等之后,第 9 行最终成为行 I表明的。

Actually, my real question is: is there anything wrong with the indicated line in the following code ("Causes SIGABRT"):

char* myFunc(char *param) {
  char* leaked = new char[80]; // Intentionally leaked
  if (param == NULL) throw logic_error("Parameter was null!"); // Causes SIGABRT
  strcpy(leaked, param);
  // Missing return, but never gets this far, so should be okay.
}

void test_non_happy_myFunc()
{
  try {
    myFunc(NULL);
  } catch (logic_error&) {
    cout << "Test succeeded!" << endl;
    return;
  }

  cout << "Test FAILED!" << endl;
}

int main()
{
  test_non_happy_myFunc();
}

I'm trying to come up with a minimal test case to send to IBM/Rational to prove that there's an issue with their purify software, so I'm running it by the S.O. community first. Yes, I'm intentionally leaking memory on the 2nd line, and yes, I know the pointer "leaked" is unitialized when the exception is thrown.

The above code runs normally outside of purify when compiled with g++, but causes a core dump when run inside of purify. Did I make a rookie mistake in the above code (making the SIGABRT my fault), or can I point the finger at IBM/Rational Purify?

Edit: (clarifications)

Run on the command line without purify, the above complete program prints:

Test succeeded!

Run inside of purify, purify reports:

COR: Fatal core dump
This is occurring while in thread 1299:
        _p450static    [rtlib.o]
        abort          [libc.so.6]
        uw_init_context_1 [unwind-dw2.c:1256]
        _Unwind_RaiseException [unwind.inc:88]
        __cxa_throw    [eh_throw.cc:78]
        myFunc(char*)  [exception_test.cc:9]
        test_non_happy_myFunc() [exception_test.cc:17]
        main           [exception_test.cc:28]

Note that after prerequisite includes and such, line 9 winds up being the line I indicated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你如我软肋 2024-10-22 18:01:46

除了 myFunc 中缺少 return 语句之外,我在上面的代码中没有看到任何错误。然而,在将责任归咎于其他人的代码(特别是广泛使用的代码)之前,我会仔细检查在此之前没有发生任何不好的事情(正如你所知,如果一百万个执行指令前调用 UB 守护进程的东西可能仍然有可能,只有现在才会有效果可见)。

仅当仅使用 main 调用 test_non_happy_myFunc 编译显示的代码并且没有自定义全局分配器等花哨的东西仍然显示问题时,我才会将搜索移至您的工具(即纯度、编译器等)在百分百确定问题确实存在之前我不会停止。

Except for the missing return statement in myFunc I don't see anything wrong in the above code. Before however putting the blame on code by others (especially widely used code) I'd double check that nothing bad happened BEFORE this (as you know if something summoned UB daemons one million executed instructions ago may still be possible that only now there will be visible effects).

Only if compiling just the shown code with main just calling test_non_happy_myFunc and without fancy stuff like custom global allocators still shows the problem then I'd move the search to your tools (i.e. purity, compiler, etc.) and I wouldn't stop before being 100% positive the problem is there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文