strcpy 中的 Valgrind 错误重叠是什么?
what is Valgrind error overlap in strcpy?
我遇到了这个错误,我从来没有遇到过使用 valgrind 的问题,你们有什么意见吗?我是 C 的新手,还在学习。谢谢!
这是我在使用 valgrind 时遇到的错误。
==8410== Source and destination overlap in strcpy(0xfff000560, 0xfff000560)
==8410== at 0x4C2E272: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8410== by 0x40126B: trim (in /********/********/********/private/********/********/********/calls)
==8410== by 0x40154B: main (in /********/********/********/private/********/********/********/calls)
==8410==
==8410== Source and destination overlap in strcpy(0xfff000560, 0xfff000560)
==8410== at 0x4C2E272: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8410== by 0x40126B: trim (in /********/********/********/private/********/********/********/calls)
==8410== by 0x4016C6: main (in /********/********/********/private/********/********/********/calls)
==8410==
==8410==
==8410== HEAP SUMMARY:
==8410== in use at exit: 0 bytes in 0 blocks
==8410== total heap usage: 30 allocs, 30 frees, 1,288 bytes allocated
==8410==
==8410== All heap blocks were freed -- no leaks are possible
==8410== For counts of detected and suppressed errors, rerun with: -v
==8410== ERROR SUMMARY: 19 errors from 2 contexts (suppressed: 0 from 0)
我的方法
static char *trim(char *s)
{
int i;
for (i = 0; isspace(s[i]); i++) // skip leading
;
int from = i; // start here
for (; s[i]; i++) // move until end
;
// move backwards, skip trailing
for (i--; i>from && isspace(s[i]); i--)
;
s[i+1] = '[=11=]'; // stop here
strcpy(s, &s[from]); // copy this part to beginning
return s;
}
更改了这一行
strcpy(s, &s[from]);
到
//# of characters
memmove(s, &s[from], i - from + 1);
我遇到了这个错误,我从来没有遇到过使用 valgrind 的问题,你们有什么意见吗?我是 C 的新手,还在学习。谢谢!
这是我在使用 valgrind 时遇到的错误。
==8410== Source and destination overlap in strcpy(0xfff000560, 0xfff000560)
==8410== at 0x4C2E272: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8410== by 0x40126B: trim (in /********/********/********/private/********/********/********/calls)
==8410== by 0x40154B: main (in /********/********/********/private/********/********/********/calls)
==8410==
==8410== Source and destination overlap in strcpy(0xfff000560, 0xfff000560)
==8410== at 0x4C2E272: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8410== by 0x40126B: trim (in /********/********/********/private/********/********/********/calls)
==8410== by 0x4016C6: main (in /********/********/********/private/********/********/********/calls)
==8410==
==8410==
==8410== HEAP SUMMARY:
==8410== in use at exit: 0 bytes in 0 blocks
==8410== total heap usage: 30 allocs, 30 frees, 1,288 bytes allocated
==8410==
==8410== All heap blocks were freed -- no leaks are possible
==8410== For counts of detected and suppressed errors, rerun with: -v
==8410== ERROR SUMMARY: 19 errors from 2 contexts (suppressed: 0 from 0)
我的方法
static char *trim(char *s)
{
int i;
for (i = 0; isspace(s[i]); i++) // skip leading
;
int from = i; // start here
for (; s[i]; i++) // move until end
;
// move backwards, skip trailing
for (i--; i>from && isspace(s[i]); i--)
;
s[i+1] = '[=11=]'; // stop here
strcpy(s, &s[from]); // copy this part to beginning
return s;
}
更改了这一行
strcpy(s, &s[from]);
到
//# of characters
memmove(s, &s[from], i - from + 1);