gpt4 book ai didi

在终止并正确返回正确输出的程序上找不到 valgrind 错误

转载 作者:太空宇宙 更新时间:2023-11-04 01:47:22 25 4
gpt4 key购买 nike

即使程序正确终止并给我正确的输出,并且没有内存泄漏,valgrind 也会给我一些此类错误:

Invalid read of size 1
==910== at 0x108DD4: fnv_hash_function (user.c:24)
==910== by 0x108E17: hash (user.c:29)
==910== by 0x109A50: icl_hash_find (icl_hash.c:114)
==910== by 0x1094DB: db_request (user.c:197)
==910== by 0x108D2E: main (tuser.c:65)
==910== Address 0x5416f50 is 0 bytes inside a block of size 15 free'd
==910== at 0x4C2E10B: free (vg_replace_malloc.c:530)
==910== by 0x109152: freeKey (user.c:138)
==910== by 0x109CF2: icl_hash_delete (icl_hash.c:192)
==910== by 0x109796: db_request (user.c:222)
==910== by 0x108CF8: main (tuser.c:59)
==910== Block was alloc'd at
==910== at 0x4C2CEDF: malloc (vg_replace_malloc.c:299)
==910== by 0x108BDC: main (tuser.c:35)

hashfnv_hash_function 是这样定义的

static inline unsigned int fnv_hash_function( void *key, int len ) {
unsigned char *p = (unsigned char*)key;
unsigned int h = 2166136261u;
int i;
for ( i = 0; i < len; i++ )
h = ( h * 16777619 ) ^ p[i]; //this is the line 24
return h;
}


unsigned int hash(void *key){
return fnv_hash_function(key, strlen(key));
}

我猜问题出在 ^ 运算符上,但我无法弄清楚问题出在哪里,因为程序以正确的输出终止并且没有段错误。

icl_hash_find 不是我写的函数,而是在我正在使用的库中并以这种方式定义

void *
icl_hash_find(icl_hash_t *ht, void* key)
{
icl_entry_t* curr;
unsigned int hash_val;

if(!ht || !key) return NULL;

hash_val = (* ht->hash_function)(key) % ht->nbuckets;

for (curr=ht->buckets[hash_val]; curr != NULL; curr=curr->next)
if ( ht->hash_key_compare(curr->key, key))
return(curr->data);

return NULL;
}

我在该库的测试套件上尝试了 valgrind,没有发现任何错误,所以我怀疑问题出在那里。

编辑: key 在此 for 循环中分配:

char * s; //string used as key
for(int i = 0; i < N; i++){

s = (char *)malloc(NAMELEN * sizeof(char));
sprintf(s, "Utente %d", i);
u = create_user( s , i);

if(!db_request(db, s, u, PUT)){
perror("problema PUT");
exit(EXIT_FAILURE);
}
.
.
.

编辑 2:这是 db_request 的主体:

bool db_request(userbase_t *db, char * key, user_t * u, dbop_t op ){

if(db==NULL || key == NULL ||(op!=DELETE && u==NULL)){
errno = EINVAL;
return false;
}
int lock_index; //indice del lock del bucket
switch(op){
//implementazione PUT
case PUT :
lock_index = db -> table -> hash_function(key) % db->nlocks;
WLOCK(&db->locks[lock_index])
errno = 0;
if(icl_hash_insert(db->table, key, (void *) u)==NULL){
RWUNLOCK(&db->locks[lock_index])
//la chiave e' gia' associata ad un utente
if(errno == EINVAL){
perror("key gia' presente");
}
return false;
}
RWUNLOCK(&db->locks[lock_index])
return true;
//implementazione GET
case GET :
lock_index = db -> table -> hash_function(key) % db->nlocks;
RLOCK(&db->locks[lock_index])

u = icl_hash_find(db->table, (void *)key );

RWUNLOCK(&db->locks[lock_index]);
return true;
//implementazione update
case UPDATE :
//elimina il vecchio e aggiunge il nuovo
lock_index = db -> table -> hash_function(key) % db->nlocks;
WLOCK(&db->locks[lock_index]);

if(icl_hash_delete(db->table, key, freeKey, freeUser)){
perror("problema UPDATE (icl_hash_delete) ");
RWUNLOCK(&db->locks[lock_index]);
return false;
}

if (icl_hash_insert(db->table, key, (void *) u)==NULL){
perror("problema UPDATE (icl_hash_insert)");
RWUNLOCK(&db->locks[lock_index]);
return false;
}
case DELETE :
lock_index = db -> table -> hash_function(key) % db->nlocks;
WLOCK(&db->locks[lock_index]);

if(icl_hash_delete(db->table, key, freeKey, freeUser)){
perror("problema DELETE");
RWUNLOCK(&db->locks[lock_index]);
return false;
}

RWUNLOCK(&db->locks[lock_index]);
return true;
//mai raggiunto
default :
errno = EINVAL;
perror("problema switch op");
return false;

}}

但我找不到问题所在,我开始认为问题出在 icl_hash 库中。

当我在测试函数中对我刚刚删除的元素调用 GET 时,会出现问题。

if(!db_request(db, s , u ,DELETE)){
perror("problema DELETE");
exit(EXIT_FAILURE);
};

//provo a ottenerlo di nuovo
//The error happens here
if(!db_request(db, s , u ,GET)){
perror("GET");
exit(EXIT_FAILURE);
};

get 唯一做的就是调用这个函数:

void *
icl_hash_find(icl_hash_t *ht, void* key)
{
icl_entry_t* curr;
unsigned int hash_val;

if(!ht || !key) return NULL;

hash_val = (* ht->hash_function)(key) % ht->nbuckets;

for (curr=ht->buckets[hash_val]; curr != NULL; curr=curr->next)
if ( ht->hash_key_compare(curr->key, key))
return(curr->data);

return NULL;
}

最佳答案

您的无效访问是对已释放的内存:

Invalid read of size 1
==910== at 0x108DD4: fnv_hash_function (user.c:24)
==910== by 0x108E17: hash (user.c:29)
==910== by 0x109A50: icl_hash_find (icl_hash.c:114)
==910== by 0x1094DB: db_request (user.c:197)
==910== by 0x108D2E: main (tuser.c:65)
==910== Address 0x5416f50 is 0 bytes inside a block of size 15 free'd
==910== at 0x4C2E10B: free (vg_replace_malloc.c:530)
==910== by 0x109152: freeKey (user.c:138)
==910== by 0x109CF2: icl_hash_delete (icl_hash.c:192)
==910== by 0x109796: db_request (user.c:222)
==910== by 0x108CF8: main (tuser.c:59)
==910== Block was alloc'd at
==910== at 0x4C2CEDF: malloc (vg_replace_malloc.c:299)
==910== by 0x108BDC: main (tuser.c:35)

在文件 user.c 中的函数 db_request() 的第 222 行对 icl_hash_delete() 的调用中释放了该 block >。在 user.cdb_request() 的第 197 行调用 icl_hash_find() 时进行了无效访问。你说icl_hash*代码提供给你。

如果没有 db_request() 函数的主体,很难确定发生了什么,但至少有两种可能性。

  1. 您有一个指向已删除条目的悬垂指针,您不应该继续使用它。
  2. icl_hash* 函数中的代码在删除哈希记录后错误处理数据。

如果 icl_hash.c 函数的提供者相当可靠,则可以明智地假设问题出在 db_request() 中的代码中。仔细查看第 197 行和第 222 行,以及传递给 icl_hash_find()icl_hash_delete() 函数的变量(指针)。再次查看这些函数的手册页以了解规则是什么。

如果您不确定 icl_hash.c 中代码的质量,您应该自己创建一个较小的 MCVE,它创建一个哈希表,添加一些行,找到一些行,删除一些条目,并做一些更多的发现。这将帮助您确定您的代码或 icl_hash.c 代码是否存在问题。

由于内存是在 main() 而不是 db_request() 中分配的,您可能需要检查您在该级别所做的事情,但我的猜测是您将该指针传递给 db_request() 并在添加条目时将其所有权交给哈希表,并且 icl_hash_delete() 的规范表示它将释放你交给哈希表的内存,你不小心仍然持有一个指向现在释放的内存的指针(db_request() 函数的参数)。您必须非常小心,以确保您知道谁拥有什么内存——以及该内存何时被释放。

关于在终止并正确返回正确输出的程序上找不到 valgrind 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51127169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com