malloc和memcpy的漏洞

问题描述

void *copy_elements(void *ele_src[],int ele_cnt,size_t ele_size) 
{ 
    /*
     * Allocate buffer for ele_cnt objects,each of ele_size bytes 
     * and copy from locations designated by ele_src
     */

    void *result = malloc(ele_cnt * ele_size); 

    if (result == NULL)
        /* malloc failed */
        return NULL; 
   
    void *next = result;
    int i;

    for (i = 0; i < ele_cnt; i++) {
        /* Copy object i to destination */ 
        memcpy(next,ele_src[i],ele_size);
        /* Move pointer to next memory region */ 
        next += ele_size;
    }

    return result; 
}

以上代码存在漏洞问题。在某些ele_cntele_size值下,代码可能会崩溃。我怀疑这与mallocmemcpy发生的无符号转换有关,但我不确定如何正确利用它。任何帮助将不胜感激!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)