【问题标题】:PVS studio fails to find an incorrect usage of use after freePVS studio免费后找不到错误使用方式
【发布时间】:2017-09-11 17:31:14
【问题描述】:

我的小应用程序使用sqlpp11 访问数据库时遇到了一个错误。 ASAN 在免费使用后中止了该程序,因为我使用了不正确的 API。在试图找出问题时,我尝试了 PVS,但没有成功。因此,我分享了代码 sn-p 作为在您的软件中添加额外检查的机会。

错误的代码是:

Record result;  // this is the native struct
demo_dao::Record records;  // this is the generated struct
auto const & record =
    store.db (select (all_of (records)).from (records).where (record.id == static_cast<long> (id))).front ();
// free has happened now
...
// use after free happens now
result.conditions = Conditions {record.Conditions.value ()};

正确的用法是:

auto result = store.db (select (all_of (records)).from (records).where (record.id == static_cast<long> id)));
auto const & record = result.front();

【问题讨论】:

    标签: pvs-studio


    【解决方案1】:

    感谢您的提示,谢尔盖!我们的 TODO for C++ 诊断中已经有一个类似的案例,并将在未来的某个时间实现它,尽管我无法给你任何估计。

    【讨论】:

      猜你喜欢
      • 2021-04-04
      • 1970-01-01
      • 2018-01-24
      • 2021-05-17
      • 1970-01-01
      • 2021-08-04
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多