Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复bug:在MSVC debug环境下,直接value解引用会触发cannot access empty value optional断… #185

Merged

Conversation

TreatTrick
Copy link
Contributor

@TreatTrick TreatTrick commented Dec 11, 2024

…言,导致程序崩溃。
在下面的语句中value其实是一个std::optional,

set_param_bind(param_bind, *value, i, mp, is_null);

直接*value解引用会触发下述语句

_NODISCARD constexpr const _Ty& operator*() const& {
#if _CONTAINER_DEBUG_LEVEL > 0
        _STL_VERIFY(this->_Has_value, "Cannot access value of empty optional");
#endif // _CONTAINER_DEBUG_LEVEL > 0
        return this->_Value;
    }

由于debug环境下_STL_VERIFY(this->_Has_value, "Cannot access value of empty optional");断言命中程序崩溃。解决方法是在解引用之前给std::optional的value先提供一个默认值。确保断言不会触发

@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 46.06%. Comparing base (fd01e4d) to head (18990a7).
Report is 14 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #185      +/-   ##
==========================================
+ Coverage   46.01%   46.06%   +0.04%     
==========================================
  Files          22       22              
  Lines        3260     3263       +3     
==========================================
+ Hits         1500     1503       +3     
  Misses       1760     1760              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

ormpp/mysql.hpp Outdated
Comment on lines 212 to 213
using value_type = typename U::value_type;
value = value_type();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样改好点:

if(!value.has_value()) {
  value = value_type{};
}

只在没有初始化的时候才给默认值。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已按照修改意见修改

@qicosmos
Copy link
Owner

能加一个测试吗,把这个分支的代码覆盖到。

@TreatTrick
Copy link
Contributor Author

我看了一下,这个修改只对mysql.hpp之下的query和query_s函数有影响,然后主要是针对std::optional类型的查询的修改。原来的测试集中已经有了对std::optional的查询测试TEST_CASE("optional"),能够覆盖本次修改。但是我发现测试中只有query的没有写query_s的。我添加了query_s相关的测试

@qicosmos
Copy link
Owner

qicosmos commented Dec 13, 2024

我看了一下,这个修改只对mysql.hpp之下的query和query_s函数有影响,然后主要是针对std::optional类型的查询的修改。原来的测试集中已经有了对std::optional的查询测试TEST_CASE("optional"),能够覆盖本次修改。但是我发现测试中只有query的没有写query_s的。我添加了query_s相关的测试

我的意思是之前会crash说明没测试到,现在用之前crash的case 测试是不是不会再有问题了?

另外,根据ci的提示修改一下代码格式。

@TreatTrick
Copy link
Contributor Author

我本地跑了测试集了,这个修改没有问题了。格式也用clang-format重新刷了

@qicosmos qicosmos merged commit f3a59b9 into qicosmos:master Dec 15, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants