“称为纯虚函数"在 gcc 4.4 上,但不在较新版本或 clang 3.4 上

本文介绍了“称为纯虚函数"在 gcc 4.4 上,但不在较新版本或 clang 3.4 上的处理方法,对大家解决问题具有一定的参考价值

问题描述

我有一个 MCVE,它在使用 g++ 4.4.7 版编译时在我的一些机器上崩溃,但可以在 clang++ 3.4.2 版和 g++ 6.3 版中使用.

I've got an MCVE which, on some of my machines crashes when compiled with g++ version 4.4.7 but does work with clang++ version 3.4.2 and g++ version 6.3.

我想知道它是来自未定义的行为还是来自这个古老版本的 gcc 的实际错误.

I'd like some help to know if it comes from undefined behavior or from an actual bug of this ancient version of gcc.

#include <cstdlib>

class BaseType
{
public:
    BaseType() : _present( false ) {}
    virtual ~BaseType() {}

    virtual void clear() {}

    virtual void setString(const char* value, const char* fieldName)
    {
        _present = (*value != '');
    }

protected:
    virtual void setStrNoCheck(const char* value) = 0;

protected:
    bool _present;
};

// ----------------------------------------------------------------------------------

class TypeTextFix : public BaseType
{
public:
    virtual void clear() {}

    virtual void setString(const char* value, const char* fieldName)
    {
        clear();
        BaseType::setString(value, fieldName);
        if( _present == false ) {
            return; // commenting this return fix the crash. Yes it does!
        }
        setStrNoCheck(value);
    }

protected:
    virtual void setStrNoCheck(const char* value) {}
};

// ----------------------------------------------------------------------------------

struct Wrapper
{
    TypeTextFix _text;
};

int main()
{
    {
        Wrapper wrapped;
        wrapped._text.setString("123456789012", NULL);
    }
    // if I add a write to stdout here, it does not crash oO
    {
        Wrapper wrapped;
        wrapped._text.setString("123456789012", NULL); // without this line (or any one), the program runs just fine!
    }
}

编译 &运行

g++ -O1 -Wall -Werror thebug.cpp && ./a.out
pure virtual method called
terminate called without an active exception
Aborted (core dumped)

这实际上是最小的,如果删除此代码的任何功能,它就可以正常运行.

This is actually minimal, if one removes any feature of this code, it runs correctly.

代码片段在使用 -O0 编译时可以正常工作,BUT 在每个标志使用 -O0 +flag 编译时仍然可以正常工作 定义的 -O1GnuCC 文档.

The code snippet works fine when compiled with -O0, BUT it still works fine when compiled with -O0 +flag for every flag of -O1 as defined on GnuCC documentation.

生成一个核心转储,可以从中提取回溯:

A core dump is generated from which one can extract the backtrace:

(gdb) bt
#0  0x0000003f93e32625 in raise () from /lib64/libc.so.6
#1  0x0000003f93e33e05 in abort () from /lib64/libc.so.6
#2  0x0000003f98ebea7d in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib64/libstdc++.so.6
#3  0x0000003f98ebcbd6 in ?? () from /usr/lib64/libstdc++.so.6
#4  0x0000003f98ebcc03 in std::terminate() () from /usr/lib64/libstdc++.so.6
#5  0x0000003f98ebd55f in __cxa_pure_virtual () from /usr/lib64/libstdc++.so.6
#6  0x00000000004007b6 in main ()

<小时>

请随时在评论中询问测试或详细信息.问:


Feel free to ask for tests or details in the comments. Asked:

  • 实际代码吗?是的!它是!一个字节一个字节.我已经检查并重新检查了.

  • Is it the actual code? Yes! it is! byte for byte. I've checked and rechecked.

您使用的是哪个版本的 GnuCC?

What exact version of GnuCC du you use?

$ g++ --version
g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16)
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  • 我们能看到生成的程序集吗?是的,这里是 pastebin.com

    推荐答案

    这是 FSF GCC 中不存在的 Red Hat 特定错误.这不是您的代码中的问题.

    This is a Red Hat-specific bug not present in FSF GCC. It is not a problem in your code.

    在同时具有 CentOS 6 的 GCC 和 FSF GCC 4.4.7 的系统上,同时生成程序集列表并查看两者之间的差异,会跳出一点:

    On a system with both CentOS 6's GCC, and FSF GCC 4.4.7, having both generate an assembly listing and viewing the differences between the two, one bit jumps out:

    CentOS 6 的 GCC 生成

    CentOS 6's GCC generates

    movq $_ZTV8BaseType+16, (%rsp)
    

    而 FSF GCC 4.4.7 生成

    whereas FSF GCC 4.4.7 generates

    movq $_ZTV11TypeTextFix+16, (%rsp)
    

    换句话说,Red Hat 的 GCC 补丁之一导致它错误地设置了 vtable.这是您的 main 函数的一部分,您可以在 .L48: 之后不久在您自己的程序集清单中看到它.

    In other words, one of Red Hat's GCC patches makes it set up the vtable incorrectly. This is part of your main function, you can see it in your own assembly listing shortly after .L48:.

    Red Hat 为其 GCC 版本应用了许多补丁,其中一些补丁是影响代码生成的补丁.不幸的是,其中一个似乎有意想不到的副作用.

    Red Hat applies many patches to its version of GCC, and some of them are patches that affect code generation. Unfortunately, one of them appears to have an unintended side effect.

    这篇关于“称为纯虚函数"在 gcc 4.4 上,但不在较新版本或 clang 3.4 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

  • admin_action_{$_REQUEST[‘action’]}

    do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

    日期:2020-09-02 17:44:16 浏览:1159

    admin_footer-{$GLOBALS[‘hook_suffix’]}

    do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

    日期:2020-09-02 17:44:20 浏览:1060

    customize_save_{$this->id_data[‘base’]}

    do_action( "customize_save_{$this-&gt;id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

    日期:2020-08-15 15:47:24 浏览:799

    customize_value_{$this->id_data[‘base’]}

    apply_filters( "customize_value_{$this-&gt;id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

    日期:2020-08-15 15:47:24 浏览:885

    get_comment_author_url

    过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

    日期:2020-08-10 23:06:14 浏览:925

    network_admin_edit_{$_GET[‘action’]}

    do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

    日期:2020-08-02 09:56:09 浏览:872

    network_sites_updated_message_{$_GET[‘updated’]}

    apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

    日期:2020-08-02 09:56:03 浏览:854

    pre_wp_is_site_initialized

    过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

    日期:2020-07-29 10:15:38 浏览:825

    WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

    你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

    日期:2020-10-03 21:18:25 浏览:1691

    谷歌的SEO是什么

    SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

    日期:2020-10-30 17:23:57 浏览:1296