我在找一个旧的,很有问题的C程序使用gcc -fsanitize=address编译时,在运行程序本身时出现此错误:

==635==ERROR: AddressSanitizer: strcpy-param-overlap: memory ranges [0x7f37e8cfd5b5,0x7f37e8cfd5b8) and [0x7f37e8cfd5b5, 0x7f37e8cfd5b8) overlap
    #0 0x7f390c3a8552 in __interceptor_strcpy /build/gcc/src/gcc/libsanitizer/asan/asan_interceptors.cc:429
    #1 0x56488e5c1a08 in backupExon src/BackupGenes.c:72
    #2 0x56488e5c2df1 in backupGene src/BackupGenes.c:134
    #3 0x56488e5c426e in BackupArrayD src/BackupGenes.c:227
    #4 0x56488e5c0bb1 in main src/geneid.c:583
    #5 0x7f390b6bfee2 in __libc_start_main (/usr/lib/libc.so.6+0x26ee2)
    #6 0x56488e5bf46d in _start (/home/darked89/proj_soft/geneidc/crg_github/geneidc/bin/geneid+0x1c46d)

0x7f37e8cfd5b5 is located 3874229 bytes inside of 37337552-byte region [0x7f37e894b800,0x7f37eace71d0)
allocated by thread T0 here:
    #0 0x7f390c41bce8 in __interceptor_calloc /build/gcc/src/gcc/libsanitizer/asan/asan_malloc_linux.cc:153
    #1 0x56488e618728 in RequestMemoryDumpster src/RequestMemory.c:801
    #2 0x56488e5bfcea in main src/geneid.c:305
    #3 0x7f390b6bfee2 in __libc_start_main (/usr/lib/libc.so.6+0x26ee2)


错误是由以下行引起的:
/* backupExon src/BackupGenes.c:65 */
strcpy(d->dumpSites[d->ndumpSites].subtype, E->Acceptor->subtype);


我把它换成了:
memmove(d->dumpSites[d->ndumpSites].subtype, E->Acceptor->subtype,
strlen(d->dumpSites[d->ndumpSites].subtype));

错误消失了,用两个不同的数据输入产生的程序输出与更改前得到的结果相同顺便说一句,更多的strcpy错误仍然在源代码中我需要确认这是解决问题的方法。
问题及其他代码如下:
https://github.com/darked89/geneidc/issues/2

最佳答案

假设E->Acceptor->subtype至少和d->dumpSites[d->ndumpSites].subtype一样长,那么就没有问题了如果你还没有的话,你可能想先检查一下实际上,您还需要一个+1来复制字符串结束符(\0),谢谢您发现它。
之前的代码做出了不同的假设:它假设d->dumpSites[d->ndumpSites].subtype至少与E->Acceptor->subtype一样长(基本上相反)。
真正的等价物是:

memmove(
    d->dumpSites[d->ndumpSites].subtype,
    E->Acceptor->subtype,
    strlen(E->Acceptor->subtype) + 1
);

这是修复代码以允许重叠的正确方法。

关于c - 修复AddressSanitizer:与memmove的strcpy-param-overlap吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58217308/

10-09 19:55