Search code examples
cvisual-studio-2010scanfunsigned-integer

Stack around the variable was corrupted when using sscanf_s


I quiet do not understand why I get error "Stack around the variable "tmp" was corrupted".

I use same code in different functions and it works well but now when function is "returning" it throws error mentioned above.

struct frame {
   uint8_t dst[6]; 
   uint8_t src[6];    
};

//fill frame.dst || src exactly same way as code below without any errors or warnings

bool fcn() {

    uint8_t tmp[6];

    sscanf_s("00-00-00-00-00-00", "%x-%x-%x-%x-%x-%x", &tmp[0], &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
    //here I compare tmp[0] == frame.mac[0]...
    return true; 
} //here pops the error while debugging

I use exactly same code in different part of the program but no error what so ever.


Solution

  • Referring C-Standard confirming systems:

    To scan in an 8bit value on a 32bit machine, one needs to use the "hh" length modifier. The half of the half of 32 is 8.


    For VC one needs to use a workaround scanning into unsigned ints.