gcc 检查数组索引是否超出Dr内存中的范围

fnatzsnv  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(173)

我需要使用Dr.Memory检查数组索引是否超出范围。在这种情况下,我不使用动态内存分配。
当我用Dr.Memory检查此代码时。我使用的是Windows 11。

int main() {
    clock_t start, end;
    float cpu_time_used;
    start = clock();

    int array[5] = {2, 4, 5, 6, 7};
    int number = array[10];
    printf("This is a number: %i\n", number);

    end = clock();
    cpu_time_used = ((float) (end - start)) / CLOCKS_PER_SEC;
    printf("\nTotal speed  was %f\n", cpu_time_used);

    return EXIT_SUCCESS;
}

我从Dr.Memory得到这个输出。正如你所看到的,没有抱怨索引超出范围。为什么?

Dr. Memory version 2.5.0 build 0 built on Oct 18 2021 03:01:22
Windows version: WinVer=105;Rel=2009;Build=22000;Edition=Professional
Dr. Memory results for pid 9640: "CControl.exe"
Application cmdline: ""C:\Users\dmn\OneDrive - ITH\Dokument\Projekt\Eclipse\CControl\Debug\CControl.exe""
Recorded 124 suppression(s) from default C:\Program Files (x86)\Dr. Memory\bin64\suppress-default.txt

Error #1: UNADDRESSABLE ACCESS beyond top of stack: reading 0x000000164a9ff980-0x000000164a9ff988 8 byte(s)
# 0 ___chkstk_ms                            [../../../../../src/gcc-11.2.0/libgcc/config/i386/cygwin.S:132]
# 1 _pei386_runtime_relocator
# 2 __tmainCRTStartup
# 3 .l_start    
# 4 ntdll.dll!RtlUserThreadStart
Note: @0:00:00.097 in thread 18604
Note: 0x000000164a9ff980 refers to 664 byte(s) beyond the top of the stack 0x000000164a9ffc18
Note: instruction: or     $0x0000000000000000 (%rcx) -> (%rcx)

Error #2: UNADDRESSABLE ACCESS beyond top of stack: reading 0x000000164a9ffa50-0x000000164a9ffa58 8 byte(s)
# 0 ___chkstk_ms                       [../../../../../src/gcc-11.2.0/libgcc/config/i386/cygwin.S:132]
# 1 __pformat_int.isra.0               [../../../../../src/gcc-11.2.0/libgcc/config/i386/cygwin.S:138]
# 2 __mingw_pformat                    [../../../../../src/gcc-11.2.0/libgcc/config/i386/cygwin.S:138]
# 3 __mingw_vfprintf                   [../../../../../src/gcc-11.2.0/libgcc/config/i386/cygwin.S:138]
# 4 printf      
# 5 main        
Note: @0:00:00.135 in thread 18604
Note: 0x000000164a9ffa50 refers to 8 byte(s) beyond the top of the stack 0x000000164a9ffa58
Note: instruction: or     $0x0000000000000000 (%rcx) -> (%rcx)

Error #3: UNINITIALIZED READ: reading register rcx
# 0 __pformat_int.isra.0               [../../../../../src/gcc-11.2.0/libgcc/config/i386/cygwin.S:138]
# 1 __mingw_pformat                    [../../../../../src/gcc-11.2.0/libgcc/config/i386/cygwin.S:138]
# 2 __mingw_vfprintf                   [../../../../../src/gcc-11.2.0/libgcc/config/i386/cygwin.S:138]
# 3 printf      
# 4 main        
Note: @0:00:00.135 in thread 18604
Note: instruction: test   %rcx %rcx

Error #4: POSSIBLE LEAK 98 direct bytes 0x00000153bd9001c0-0x00000153bd900222 + 0 indirect bytes
# 0 replace_malloc                   [d:\a\drmemory\drmemory\common\alloc_replace.c:2580]
# 1 msvcrt.dll!malloc_crt
# 2 msvcrt.dll!_setargv  
# 3 msvcrt.dll!_getmainargs
# 4 pre_cpp_init
# 5 msvcrt.dll!initterm  
# 6 __tmainCRTStartup
# 7 .l_start    
# 8 ntdll.dll!RtlUserThreadStart

===========================================================================
FINAL SUMMARY:

DUPLICATE ERROR COUNTS:
    Error #   1:      2
    Error #   2:      2

SUPPRESSIONS USED:

ERRORS FOUND:
      2 unique,     4 total unaddressable access(es)
      1 unique,     1 total uninitialized access(es)
      0 unique,     0 total invalid heap argument(s)
      0 unique,     0 total GDI usage error(s)
      0 unique,     0 total handle leak(s)
      0 unique,     0 total warning(s)
      0 unique,     0 total,      0 byte(s) of leak(s)
      1 unique,     1 total,     98 byte(s) of possible leak(s)
ERRORS IGNORED:
      3 unique,     3 total,    113 byte(s) of still-reachable allocation(s)
         (re-run with "-show_reachable" for details)
Details: C:\Users\dmn\AppData\Roaming\Dr. Memory\DrMemory-CControl.exe.9640.000\results.txt
zrfyljdw

zrfyljdw1#

你不需要Dr Memory来检测这个,也不需要Valgrind,只要在编译的时候加上-fsanitize=address就行了。

相关问题