overflow.h/Gcc5

本文介绍了一种使用编译器内置函数或通用方法来检测整数运算溢出的技术。对于支持的编译器如Clang和GCC,利用__builtin_add_overflow等内置函数可以直接进行溢出检查。对于不支持内置函数的编译器,则提供了通用实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#if __GNUC__ >= 5 || __has_builtin(__builtin_add_overflow)
/** assume clang and gcc (>=5) to only have builtins for now. */
#  define add_of(a, b, r) __builtin_add_overflow(a, b, r)
#  define sub_of(a, b, r) __builtin_sub_overflow(a, b, r)
#  define mul_of(a, b, r) __builtin_mul_overflow(a, b, r)
#  define of_attr
#else
/** else use these generics, note behaviour of these is not strictly defined. */
#  define add_of(a, b, r) ((*(r) = ((a) + (b))) < (a))
#  define sub_of(a, b, r) ((*(r) = ((a) - (b))) > (a))
#  define mul_of(a, b, r) (((*(r) = ((a) * (b))) || *(r) == 0) && ((a) != 0 && (b) > *(r) / (a)))
#  if __clang__
#     define of_attr __attribute__((optnone)) // Do not optimize above checks, in most systems this will work, but not defined.
#     warning "Using non compiler builtins for overflow checks, this will be undefined for signed integers"
#  elif __GNUC__
#     define of_attr __attribute__((optimize("wrapv"))) // in older GCC we can make this behavior defined
#  else
#     warning "Using non compiler builtins for overflow checks, this will be undefined for signed integers"
#  endif
#endif

Line 7: Char 9: ================================================================= ==22==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x502000000354 at pc 0x55de9091eda9 bp 0x7ffeb85c4260 sp 0x7ffeb85c4258 READ of size 4 at 0x502000000354 thread T0 #0 0x55de9091eda8 in swap<int> /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/move.h:222:13 #1 0x55de9091eda8 in iter_swap<__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > > /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/stl_algobase.h:185:7 #2 0x55de9091eda8 in void std::__reverse<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int>>>>(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int>>>, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int>>>, std::random_access_iterator_tag) /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/stl_algo.h:1062:4 #3 0x55de9091ec00 in reverse<__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > > /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/stl_algo.h:1089:7 #4 0x55de9091ec00 in Solution::rotate(std::vector<int, std::allocator<int>>&, int) solution.cpp:7:9 #5 0x55de9091e4ad in __helper__ solution.cpp:7:18 #6 0x55de9091e4ad in main solution.cpp:7:30 #7 0x7f3b1408a1c9 (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: 6d64b17fbac799e68da7ebd9985ddf9b5cb375e6) #8 0x7f3b1408a28a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: 6d64b17fbac799e68da7ebd9985ddf9b5cb375e6) #9 0x55de90847f44 in _start (solution+0xb2f44) 0x502000000354 is located 0 bytes after 4-byte region [0x502000000350,0x502000000354) allocated by thread T0 here: #0 0x55de9091bb6d in operator new(unsigned long) /root/llvm-project/compiler-rt/lib/asan/asan_new_delete.cpp:86:3 #1 0x55de909381de i
03-09
gcc -Iinclude/ -Isrc/ -DCUDNN -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DCUDNN -c ./src/gemm.c -o obj/gemm.o gcc -Iinclude/ -Isrc/ -DCUDNN -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DCUDNN -c ./src/utils.c -o obj/utils.o In file included from /usr/include/string.h:495, from ./src/utils.c:3: In function 'strncpy', inlined from 'copy_string' at ./src/utils.c:426:5: /usr/include/x86_64-linux-gnu/bits/string_fortified.h:106:10: warning: '__builtin_strncpy' specified bound depends on the length of the source argument [-Wstringop-overflow=] 106 | return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ./src/utils.c: In function 'copy_string': ./src/utils.c:426:22: note: length computed here 426 | strncpy(copy, s, strlen(s)+1); | ^~~~~~~~~ gcc -Iinclude/ -Isrc/ -DCUDNN -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DCUDNN -c ./src/cuda.c -o obj/cuda.o gcc -Iinclude/ -Isrc/ -DCUDNN -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DCUDNN -c ./src/deconvolutional_layer.c -o obj/deconvolutional_layer.o gcc -Iinclude/ -Isrc/ -DCUDNN -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DCUDNN -c ./src/convolutional_layer.c -o obj/convolutional_layer.o ./src/convolutional_layer.c: In function 'get_workspace_size': ./src/convolutional_layer.c:91:9: warning: implicit declaration of function 'cudnnGetConvolutionForwardWorkspaceSize' [-Wimplicit-function-declaration] 91 | cudnnGetConvolutionForwardWorkspaceSize(cudnn_handle(), | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ./src/convolutional_layer.c:91:49: warning: implicit declaration of function 'cudnn_handle' [-Wimplicit-function-declaration] 91 | 这个错误应该如何解决,请提供参考网址
04-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值