malloc内存分配常见的两个错误:runtime error: store to address、、AddressSanitizer: heap-buffer-overflow

2023-10-30

runtime error: store to address 0x6020000000b8 、、、 和AddressSanitizer: heap-buffer-overflow on address 、、、

这两个问题为力扣刷题malloc分配内存空间的常见错误。

问题1:

Line 32: Char 17: runtime error: store to address 0x6020000000b8 with insufficient space for an object of type 'int' [solution.c]
0x6020000000b8: note: pointer points here
 02 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00

问题代码:

  int *a = (int *)malloc(sizeof(a));

解决方案:将sizeof(a)改为sizeof(int)*nums
nums为数组大小

 int *a = (int *)malloc(sizeof(int)*nums);

问题2:

=================================================================
==42==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000001d4 at pc 0x7f0a099c0f2d bp 0x7ffdbdb287e0 sp 0x7ffdbdb27f88
WRITE of size 8 at 0x6020000001d4 thread T0
    #0 0x7f0a099c0f2c  (/lib/x86_64-linux-gnu/libasan.so.5+0x67f2c)
    #3 0x7f0a08e210b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
0x6020000001d4 is located 0 bytes to the right of 4-byte region [0x6020000001d0,0x6020000001d4)
allocated by thread T0 here:
    #0 0x7f0a09a66bc8 in malloc (/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8)
    #3 0x7f0a08e210b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
SUMMARY: AddressSanitizer: heap-buffer-overflow (/lib/x86_64-linux-gnu/libasan.so.5+0x67f2c) 
Shadow bytes around the buggy address:
  0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff8000: fa fa fd fd fa fa 00 00 fa fa 00 00 fa fa 00 00
  0x0c047fff8010: fa fa 00 00 fa fa 00 00 fa fa 05 fa fa fa fd fa
  0x0c047fff8020: fa fa 00 00 fa fa 00 00 fa fa 00 fa fa fa 06 fa
=>0x0c047fff8030: fa fa fd fa fa fa 00 00 fa fa[04]fa fa fa fa fa
  0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==42==ABORTING

问题代码:

   int *a = (int *)malloc(sizeof(int)*nums);

解决方案:数组大小定义过小,导致访问越界,适当增加数组的大小即可。

   int *a = (int *)malloc(sizeof(int)*(nums+1);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

malloc内存分配常见的两个错误:runtime error: store to address、、AddressSanitizer: heap-buffer-overflow 的相关文章

  • 《金融工程》

    金融工程包括各种创新性金融工具和金融工序的涉及 开发 实施 以及对解决金融问题的各种创造性方案的设计 对各种金融工具的基础介绍 概念性金融工具 投资组合理论 资本结构理论 资本资产定价的CAPM模型 有效市场理论 期权定价理论 套利定价理论
  • 基本路径测试法(一看就懂)

    基本路径测试就是在程序控制流图的基础上 通过分析控制构造的环形复杂性 导出基本可行路径集合 从而设计测试用例的方法 基本路径测试具体方法如下 对复杂性程度高的程序做到覆盖所有路径 测试所有可执行路径 是不可能的 根据独立路径概念 某一程序的
  • 图解实时操作系统和非实时操作系统的区别

    转自 http blog csdn net u013752202 article details 53649047 对于实时操作系统 RTOS 和非实时操作系统 你能分别列举出来多少 实时操作系统 uCOS VxWorks RTLinux
  • IDEA构建spring源码

    1 下载spring源码 https github com spring projects spring framework 在tags中选择release 稳定 版本 我下载的是 spring framework 5 2 21 RELEA
  • @NotNull、@NotEmpty、@NotBlank注解用到的依赖

    参考案例 Web环境 才生效 https blog csdn net Aku 2020 article details 125413830 1 NotNull NotEmpty NotBlank注解用到的依赖

随机推荐