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