Skip to content

Commit d4bfc6a

Browse files
Fix potential UB in string concatenation for NFLog device name. (#1844)
* Fix potential UB in string concatenation for NFLog device name. Fixes a build failure with GCC 12.2.0 on aarch64 due to overly aggressive optimizations and inlining when concatenating C-style and temporary C++ strings using operator+. The original code caused a -Werror=restrict warning about possible memory overlap in std::char_traits<char>::copy. ```code inlined from ‘pcpp::internal::PcapHandle pcpp::PcapLiveDevice::doOpen(const DeviceConfiguration&)’ at app/connectivity/build/rpi64-rls/_deps/pcapplusplus-src/Pcap++/src/PcapLiveDevice.cpp:386:23: app/connectivity/3rdparty/cross/rpi64/tools/gcc-12.2.0-64/aarch64-linux-gnu/include/c++/12.2.0/bits/char_traits.h:431:56: error: ‘void* __builtin_memcpy(void*, const void*, long unsigned int)’ accessing 9223372036854775810 or more bytes at offsets [2, 9223372036854775807] and 1 may overlap up to 9223372036854775813 bytes at offset -3 [-Werror=restrict] 431 | return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n)); | ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~ ``` * add comment * apply clang-format --------- Co-authored-by: Liu, An-Chi <phy.tiger@gmail.com>
1 parent 1674286 commit d4bfc6a

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

Pcap++/src/PcapLiveDevice.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,8 @@ namespace pcpp
427427

428428
if (isNflogDevice())
429429
{
430-
device_name += ":" + std::to_string(config.nflogGroup & 0xffff);
430+
device_name += ":"; // prevent UB in string concatenation
431+
device_name += std::to_string(config.nflogGroup & 0xffff);
431432
}
432433

433434
auto pcap = internal::PcapHandle(pcap_create(device_name.c_str(), errbuf));

0 commit comments

Comments
 (0)