我的代码有错误,但我不知道怎么检查,有人可以帮帮我吗? 3Q [英] My code have a error,but I don't know how to check it,anyone can help me ? 3Q

查看:63
本文介绍了我的代码有错误,但我不知道怎么检查,有人可以帮帮我吗? 3Q的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char* MDString (char* string)
{
    MD5_CTX context;
    unsigned char digest[16];
    char output1[32];
    static char output[33] = {""};
    unsigned int len = strlen(string);
    int i;

    MD5Init(&context);
    MD5Update(&context, (unsigned char*)string, len);
    MD5Final(digest, &context);
    
    for (i = 0; i < 16; i++)
    {
        sprintf(&(output1[2*i]),"%02x",(unsigned char)digest[i]);
        sprintf(&(output1[2*i+1]),"%02x",(unsigned char)(digest[i]<<4));
    }

    for(i=0;i<=32;i++)
        output[i] = output1[i];

    return output;
}

推荐答案

您的代码包含 output1的缓冲区溢出在循环。最后一个循环执行 i 是15,缓冲区的索引是i * 2 + 1 = 31. sprintf 将在此位置向缓冲区​​打印两个字符和一个终止NULL(写入output1 [31]到output1 [33])。所以 output1 的大小必须是34。
Your code contains a buffer overflow for output1 in the loop. With the last loop execution i is 15 and the index into the buffer is i*2+1 = 31. sprintf will print two chars and a terminating NULL to the buffer at this position (writing to output1[31] to output1[33]). So the size of output1 must be 34.


这篇关于我的代码有错误,但我不知道怎么检查,有人可以帮帮我吗? 3Q的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆