使用g++12/13编译BoringSSL失败,求解决办法
在Debian测试版中用g++-12/13编译BoringSSL失败的解决方法
问题概述
在Debian测试版系统中,使用g++-12或更高版本编译BoringSSL时,会因gcc新的悬空指针检测警告(被当作错误处理)导致编译失败,g++-11及以下版本可正常编译。
编译报错信息
ninja: Entering directory `build/' [459/751] Building C object crypto/CMakeFiles/crypto.dir/bytestring/cbb.c.o FAILED: crypto/CMakeFiles/crypto.dir/bytestring/cbb.c.o /usr/bin/cc -DBORINGSSL_IMPLEMENTATION -Ipath/to/lib/boringssl/include -Werror -Wformat=2 -Wsign-compare -Wwrite-strings -Wvla -Wshadow -Wtype-limits -ggdb -Wall -fvisibility=hidden -fno-common -Wno-free-nonheap-object -Wimplicit-fallthrough -Wformat-signedness -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes -D_XOPEN_SOURCE=700 -O3 -DNDEBUG -std=gnu11 -MD -MT crypto/CMakeFiles/crypto.dir/bytestring/cbb.c.o -MF crypto/CMakeFiles/crypto.dir/bytestring/cbb.c.o.d -o crypto/CMakeFiles/crypto.dir/bytestring/cbb.c.o -c path/to/lib/boringssl/crypto/bytestring/cbb.c In function ‘cbb_add_child’, inlined from ‘CBB_add_asn1’ at path/to/lib/boringssl/crypto/bytestring/cbb.c:361:10, inlined from ‘CBB_add_asn1’ at path/to/lib/boringssl/crypto/bytestring/cbb.c:342:5, inlined from ‘CBB_add_asn1_octet_string’ at path/to/lib/boringssl/crypto/bytestring/cbb.c:544:8: path/to/lib/boringssl/crypto/bytestring/cbb.c:291:14: error: storing the address of local variable ‘child’ in ‘*cbb.child’ [-Werror=dangling-pointer=] 291 | cbb->child = out_child; | ~~~~~~~~~~~^~~~~~~~~~~ path/to/lib/boringssl/crypto/bytestring/cbb.c: In function ‘CBB_add_asn1_octet_string’: path/to/lib/boringssl/crypto/bytestring/cbb.c:543:7: note: ‘child’ declared here 543 | CBB child; | ^~~~~ path/to/lib/boringssl/crypto/bytestring/cbb.c:542:36: note: ‘cbb’ declared here 542 | int CBB_add_asn1_octet_string(CBB *cbb, const uint8_t *data, size_t data_len) { | ~~~~~^~~ In function ‘cbb_add_child’, inlined from ‘CBB_add_asn1’ at path/to/lib/boringssl/crypto/bytestring/cbb.c:361:10, inlined from ‘CBB_add_asn1’ at path/to/lib/boringssl/crypto/bytestring/cbb.c:342:5, inlined from ‘CBB_add_asn1_bool’ at path/to/lib/boringssl/crypto/bytestring/cbb.c:555:8: path/to/lib/boringssl/crypto/bytestring/cbb.c:291:14: error: storing the address of local variable ‘child’ in ‘*cbb.child’ [-Werror=dangling-pointer=] 291 | cbb->child = out_child; | ~~~~~~~~~~~^~~~~~~~~~~ path/to/lib/boringssl/crypto/bytestring/cbb.c: In function ‘CBB_add_asn1_bool’: path/to/lib/boringssl/crypto/bytestring/cbb.c:554:7: note: ‘child’ declared here 554 | CBB child; | ^~~~~ path/to/lib/boringssl/crypto/bytestring/cbb.c:553:28: note: ‘cbb’ declared here 553 | int CBB_add_asn1_bool(CBB *cbb, int value) { | ~~~~~^~~ cc1: all warnings being treated as errors [472/751] Building CXX object CMakeFiles/ssl_test.dir/ssl/ssl_test.cc.o ninja: build stopped: subcommand failed.
原编译步骤
git clone https://github.com/google/boringssl.git cd boringssl cmake -DCMAKE_CXX_COMPILER=g++-12 -DCMAKE_BUILD_TYPE=Release -B build -GNinja ninja -C build
解决方法
方法1:临时禁用悬空指针错误警告
该报错是gcc 12+过度内联分析导致的误判,BoringSSL代码本身不存在实际的悬空指针问题,可通过添加编译选项关闭该警告的错误触发:
修改cmake命令为:
cmake -DCMAKE_CXX_COMPILER=g++-12 -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_C_FLAGS="-Wno-error=dangling-pointer" \ -DCMAKE_CXX_FLAGS="-Wno-error=dangling-pointer" \ -B build -GNinja
之后执行编译:
ninja -C build
方法2:使用官方修复代码
官方已在内部bug追踪系统中记录该问题(编号650),确认是gcc新版本检测逻辑误判。可等待官方合并修复补丁,或自行拉取包含修复的分支编译。若需手动修复,可修改crypto/bytestring/cbb.c中的相关函数,添加__attribute__((noinline))阻止过度内联,避免编译器误判。
内容的提问来源于stack exchange,提问作者LeXav
相关产品推荐
相关产品推荐

