Click here to Skip to main content
15,885,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
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;
}
Posted
Updated 9-Oct-14 16:36pm
v2
Comments
bryce 9-Oct-14 21:23pm    
erm - what seems to be your issue? other than "it has an error"
DingguoHong 10-Oct-14 0:31am    
Stack around the variable 'output1' was corrupted.
Sergey Alexandrovich Kryukov 10-Oct-14 1:50am    
"Stack around the variable" is not anything reasonable. I don't understand what could it possibly mean. A variable is somewhere in the middle of a stack frame. Stack is always get corrupted around the frame boundaries...
—SA
DingguoHong 10-Oct-14 0:37am    
sorry ,my english is not good ...=.=
George Jonsson 9-Oct-14 22:39pm    
Where does the error occur?

1 solution

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.
 
Share this answer
 
Comments
nv3 10-Oct-14 3:47am    
My 5.
[no name] 10-Oct-14 4:06am    
Well done. I missed the format specifier - actually the sprintf.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900