那些年掉进的坑---内存踩踏实例记录

今天遇到一下奇怪的段错误,研究发现原来是内存写越界了。

函数片段如下 ht_insert_he --> ht_index --> hashfunction

void
ht_insert_he(hash_table *table, hash_entry *entry)
{
    hash_entry *tmp;
    unsigned int index;

    entry->timestamp = time(NULL);
    entry->next = NULL;

    index = ht_index(table, entry->key, entry->key_size);
    tmp = table->array[index]; //段错误出现位置

    ......

}

unsigned int 
ht_index(hash_table *table, void *key, size_t key_size)
{
    uint32_t index; //32位变量
    table->hashfunction(key, key_size, global_seed, &index);
    index %= table->array_size;
    return index;
}

其中hashfunction实现如下
void 
hashfunction(const void *key, const int len, const uint32_t seed, void *out)
{
    const uint8_t * data = (const uint8_t*)key; 
    const int nblocks = len / 16;

	......

    ((uint64_t*)out)[0] = h1; //把最后一个参数当64位进行写操作
    ((uint64_t*)out)[1] = h2;
}


!!!把32位变量index的地址传给了hashfunction,而hashfunction把它当成了64位变量进行写入,破坏了函数栈

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值