关于string

学术版

FanMingxuan @ 2024-11-29 10:44:44

考虑如下代码:

string s;
cin>>s;
cout<<s[s.length()];

理论上应该是 RE 的。

但是在以及本机上面都没有 RE,而是直接随机字符。

请问对 string 越界访问是否会造成 RE。

这个事情是我今天敲 KMP 板子的时候发现以前的 AC 代码没有在匹配到完整的模式串以后强制回退一步。


by Heil_Deutsch @ 2024-11-29 10:49:05

@FanMingxuan 不用考虑是否RE,写代码时避免就好了


by Kazeno_Akina @ 2024-11-29 10:49:20

@FanMingxuan 我记得如果越界越的不多它会自己认为某个地方的值就是你要访问的值。然而这块内存你没用过呢就成随机了。

但是越界稍微大一点应该就 RE 了。


by liyixin0514 @ 2024-11-29 10:58:56

还真是,NOI Linux 开 fsanitize 和 Wall 都不会报错,越十几位 fsanitize 就会报 RE,但是不开 fsanitize 越上万位才会 RE。


by FanMingxuan @ 2024-11-29 11:02:58

对,帖子里写的就是linux和本机都不会挂,但是不知为什么文中的的Linux消失了@liyixin0514


by FanMingxuan @ 2024-11-29 11:06:13

而且据addressSan内存图看出来,可访问的地址甚至是不连续的,在一堆0000中间夹着几个fafa

Shadow bytes around the buggy address:
  0x502ffffffe00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x502ffffffe80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x502fffffff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x502fffffff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x503000000000: fa fa 00 00 00 fa fa fa 00 00 00 07 fa fa fa fa
=>0x503000000080: fa[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x503000000100: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x503000000180: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x503000000200: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x503000000280: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x503000000300: 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

@liyixin0514


by liyixin0514 @ 2024-11-29 11:12:02

@FanMingxuan 看了 https://zhuanlan.zhihu.com/p/510507837,不是很懂啊,写代码的时候尽量避免就好了。


by FanMingxuan @ 2024-11-29 11:14:06

彳亍 @liyixin0514


|