欢迎投稿,推荐或自荐文章/软件/资源等,评论区留言
本期文章由 Amniesia HNY Damon 赞助
最近的热门是windows蓝屏事件了,其实国内外安全都有关系户
本周内容较少
资讯
标准委员会动态/ide/编译器信息放在这里
编译器信息最新动态推荐关注hellogcc公众号 本周更新 264期
文章
Safer code in C with lifetime bounds
https://lemire.me/blog/2024/07/26/safer-code-in-c-with-lifetime-bounds/
llvm和msvc支持生命周期检查,返回string_view有概率悬空,用错
代码语言:javascript复制std::string_view my_get_host(std::string_view url_string) {
auto url = ada::parse(url_string).value();
return url.get_host();
}
比如这种用法明显就是错的,加上编译检查能抓出来
代码语言:javascript复制#ifndef __has_cpp_attribute
#define ada_lifetime_bound
#elif __has_cpp_attribute(msvc::lifetimebound)
#define ada_lifetime_bound [[msvc::lifetimebound]]
#elif __has_cpp_attribute(clang::lifetimebound)
#define ada_lifetime_bound [[clang::lifetimebound]]
#elif __has_cpp_attribute(lifetimebound)
#define ada_lifetime_bound [[lifetimebound]]
#else
#define ada_lifetime_bound
#endif
...
std::string_view get_host() const noexcept ada_lifetime_bound
编译报错
代码语言:javascript复制fun.cpp:8:10: warning: address of stack memory associated with local variable 'url' returned [-Wreturn-stack-address]
8 | return url.get_host();
想要了解可以看这里 https://clang.llvm.org/docs/AttributeReference.html#lifetimebound
strlcpy and how CPUs can defy common sense strlcpy and how CPUs can defy common sense
https://nrk.neocities.org/articles/cpu-vs-common-sense
strlcpy 实现openbsd和glibc实现不同,openbsd是这样的
代码语言:javascript复制size_t strlcpy(char *dst, const char *src, size_t dsize)
{
const char *osrc = src;
size_t nleft = dsize;
if (nleft != 0) while (--nleft != 0) { /* Copy as many bytes as will fit. */
if ((*dst = *src ) == '