V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
11232as
V2EX  ›  C++

有关" Use After Free"的问题

  •  
  •   11232as · 2021-04-30 00:07:46 +08:00 · 1904 次点击
    这是一个创建于 1054 天前的主题,其中的信息可能已经有所发展或是发生改变。

    C++学艺不精看不懂这个错,请各位大佬帮忙看一下这里为什么会报一个Use After Free啊。

    Result<pcrepp, pcrepp::compile_error> pcrepp::from_str(std::string pattern, int options)
    {
        const char *errptr;
        int eoff;
        auto code = pcre_compile(pattern.c_str(),
                                 options | PCRE_UTF8,
                                 &errptr,
                                 &eoff,
                                 nullptr);
    
        if (!code) {
            return Err(compile_error{errptr, eoff});
        }
    
        return Ok(pcrepp(std::move(pattern), code));//这一行报错
    }
    //报错信息
    //call to `Result` eventually accesses memory that was invalidated by call to `free()` on line 86 indirectly during the call to `Ok`.
    //84.     }
    //85. 
    //86.     return Ok(pcrepp(std::move(pattern), code));
    //        ^
    //87. }
    //88. 
    
    

    Ok方法

    template<typename T, typename CleanT = typename std::decay<T>::type>
    types::Ok<CleanT> Ok(T&& val) {
        return types::Ok<CleanT>(std::forward<T>(val));
    }
    

    Result的构造函数

        Result(types::Ok<T> ok)
            : ok_(true)
        {
            storage_.construct(std::move(ok));
        }
    
    

    谢谢各位大佬了[抱拳]

    第 1 条附言  ·  2021-04-30 22:13:59 +08:00

    storage_.construct 的部分

        void construct(types::Ok<T> ok)
        {
            new (&storage_) T(std::move(ok.val));
            initialized_ = true;
        }
    
    5 条回复    2021-05-06 17:24:39 +08:00
    Inn0Vat10n
        1
    Inn0Vat10n  
       2021-04-30 01:22:07 +08:00
    Ok 的这一层转换,拿 左值引用 引用 右值了
    yazoox
        2
    yazoox  
       2021-04-30 10:24:47 +08:00
    楼主,请教一下。
    template<typename T, typename CleanT = typename std::decay<T>::type>
    你这里,定义模板,最后这个 std::decay<T>::type 为什么要添加?我看你下面函数体,也没有用到呢......
    wutiantong
        3
    wutiantong  
       2021-04-30 11:33:05 +08:00
    可能错在 storage_.construct 这里面
    araraloren
        4
    araraloren  
       2021-04-30 15:25:28 +08:00
    我还以为我在看 rust 。。
    wutiantong
        5
    wutiantong  
       2021-05-06 17:24:39 +08:00
    https://github.com/tstack/lnav

    这么高关注度的项目,为啥不去提 issue 呢?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3507 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 11:05 · PVG 19:05 · LAX 04:05 · JFK 07:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.