Stack around variable corrupted

I keep getting this error Run-Time Check Failure #2 - Stack around the variable 'chTemp' was corrupted. from the code below.
I am not sure what it means.
I suspect there is a problem with my pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char chTemp;
        char* chPoint;
        HKEY configKey;
        DWORD dwType;
        DWORD dwSize=200;
        chPoint = &chTemp;
        protocol.analyze_sig_pack(echoBuffer,1024);
        if(RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\ConfApp\\", 0, KEY_ALL_ACCESS, &configKey)==ERROR_SUCCESS)
        {
            
            itoa(protocol.get_integer_value(18),chPoint,10);
            RegSetValueEx(configKey,"ConfRoom",NULL,REG_SZ,(BYTE *)chPoint,dwSize);
            itoa(protocol.get_integer_value(13),chPoint,10);
            RegSetValueEx(configKey,"Passcode",NULL,REG_SZ,(BYTE *)chPoint,dwSize);
        }
        RegCloseKey(configKey);
A char is just that, a character. A single character.

conversely, when things take a char* as a parameter (in this case, itoa), they usually want an array of characters to form a string.

What's happening is itoa is trying to write several characters to chTemp (since it's pointed to by chPoint), but chTemp is only 1 char long, so the other data is spilling over and corrupting the stack.


The way to solve this would be to make chTemp a buffer, instead of a single char:

1
2
3
4
5
6
7
8
9
char chTemp[64];  // now it can hold 64 characters (hopefully that's enough)
//char* chPoint = chTemp;  // this is superfluous...
     //   you don't need chPoint at all, you can just use chTemp

//...

itoa(protocol.get_integer_value(18),chTemp,10);  // now chTemp is large enough to hold the whole string
  // you're giving it.  So no more buffer overflow.  (hopefully, anyway, with these kinds of functions there's no way to
  // know for sure, which is why they should be avoided) 




An alternative and much safer way is to use stringstreams. Then it's virtually impossible to overflow your buffer:

1
2
3
4
5
6
stringstream str;

//...

str << protocol.get_integer_value(18);
RegSetValueEx(configKey,"ConfRoom",NULL,REG_SZ,(const BYTE *)str.str().c_str(),dwSize);




You might also have issues involving the dwSize member, but I really don't have a clue as I'm not familiar at all with this registry nonsense.
thanks alot that worked wonders....i could be wasting hours before finding a solution...but this forum has help me get solutions within an hour max....
Topic archived. No new replies allowed.