문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

The %x specifier writes to an int parameter, but you are passing pointers to uint8_t instead. Since int is a larger type on your platform, you are overwriting memory when those fields are written out. Pass in pointers to int instead and convert them to your required type as appropriate.

You will want to do this everywhere else you use this code too!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top