问题描述
以下code:
的#include<&stdlib.h中GT;
#包括LT&;&string.h中GT;诠释主(){
字符* S =的strdup(保持活动);
如果(strcasestr(S,关闭)){
}
免费(S);
返回0;
}
提供了以下错误在Valgrind的:
== == 13183尺寸为8的读无效
== == 13183在0x4F53F94:__strcasestr_sse42(emmintrin.h:685)
== == 13183通过0x4005BF:主(在/ home /阿龙的/ dev / strtest)
== == 13183地址0x51ce048是大小11 alloc'd的块中的8个字节
== == 13183在0x4C28F9F:的malloc(vg_replace_malloc.c:236)
== == 13183通过0x4EB1441:的strdup(strdup.c:43)
== == 13183通过0x4005A5:主(在/ home /阿龙的/ dev / strtest)
有没有其他人看到了这一点?这种情况与&安培;不优化,使用gcc 4.6.1。
如果这是只在Valgrind的发生,这是不是一个错误。这将是未定义行为的您的code 的读取超出所获得对象的结束的malloc
,但 strcasestr
是实现的一部分,因此可以使用特定于实现的知识:在这种情况下,一个事实,即在阅读长达绝对安全的,你不跨越页边界
The following code:
#include <stdlib.h>
#include <string.h>
int main() {
char *s = strdup("keep-alive");
if(strcasestr(s, "close")) {
}
free(s);
return 0;
}
gives the following error in Valgrind:
==13183== Invalid read of size 8
==13183== at 0x4F53F94: __strcasestr_sse42 (emmintrin.h:685)
==13183== by 0x4005BF: main (in /home/aaron/dev/strtest)
==13183== Address 0x51ce048 is 8 bytes inside a block of size 11 alloc'd
==13183== at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
==13183== by 0x4EB1441: strdup (strdup.c:43)
==13183== by 0x4005A5: main (in /home/aaron/dev/strtest)
Has anyone else seen this? This happens with & without optimizations, using gcc 4.6.1.
If this is only happening in valgrind, it's not an error. It would be undefined behavior for your code to read beyond the end of an object obtained by malloc
, but strcasestr
is part of "the implementation" and thus can use implementation-specific knowledge: in this case, the fact that over-reading is perfectly safe as long as you don't cross a page boundary.
这篇关于在strcasestr读大小无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!