C Wireshark插件导致';堆损坏';
我正在写wireshark剖析器,遇到了奇怪的内存管理问题。我有以下功能:C Wireshark插件导致';堆损坏';,c,memory,memory-management,wireshark,wireshark-dissector,C,Memory,Memory Management,Wireshark,Wireshark Dissector,我正在写wireshark剖析器,遇到了奇怪的内存管理问题。我有以下功能: guint8* foo_hex2bytes(guchar *str, guint len){ ... //allocate memory for the result and start converting res = (guint8*)wmem_alloc(scope, sizeof(guint8)*((len>>1))); for(i=0; i<(len>&g
guint8* foo_hex2bytes(guchar *str, guint len){
...
//allocate memory for the result and start converting
res = (guint8*)wmem_alloc(scope, sizeof(guint8)*((len>>1)));
for(i=0; i<(len>>1); i++){
//check that the two digits under question are HEX digits
if(!isxdigit(*(str+2*i)) || !isxdigit(*(str+2*i+1))){
wmem_free(scope, res);
return NULL;
}
//append the byte
sscanf((const char*)(str+2*i), "%02x", &res[i]);
}
return res;
}
奇怪的是,我在wmem\u-free(NULL,tmp)
行得到了一个windows触发的断点(或调试器外部的普通崩溃)。除了错误发生的时间之外,我无法收集任何真正的调试信息
注意:我修改了我的foo_hex2bytes
以接受第三个wmem_分配器\u t
参数,并简单地传递了wmem_epan_scope()
(或wmem_packet_scope()
在适当的情况下)-这会在关闭应用程序时导致类似的崩溃。我还尝试过使用malloc()
并手动清除所有内存(有时有效,有时返回null指针
,即使应用程序只使用14K内存,而且还有更多可用内存)
编辑:问题似乎存在于
sscanf(…)
行中-我没有分配足够的内存,内存已溢出。通过增加分配大小进行修复。在对foo_hex2bytes
的调用中,您正在传入syskey_str
,但是devkey_str
的长度应该是syskey_str
您得到的断点仅仅意味着由于写入超出分配的内存,堆已损坏。很好地发现。。。不幸的是,这不是问题所在(syskey_str和devkey_str的长度都相同)。这个问题似乎发生在清除内存时(通过手动或在应用程序结束时通过
wmem_free(…)
),我真的不明白为什么会发生这种情况。请注意,你完全正确,这个问题是由内存写访问超出分配造成的。(我建议编辑标题,类似于“Wireshark解析器‘堆损坏’”或类似问题,因为该问题与wmem无关)。
...
if(syskey_str && strlen(syskey_str)){
wmem_free(wmem_epan_scope(), syskey_bytes);
if(tmp = foo_hex2bytes((guchar*)syskey_str, (guint) strlen(syskey_str))){
syskey_len = (guint)strlen(syskey_str)>>1;
syskey_bytes = (guint8*)wmem_alloc(wmem_epan_scope(), strlen(syskey_str)>>1);
memcpy(syskey_bytes, tmp, strlen(syskey_str)>>1);
wmem_free(NULL, tmp);
}
}
...