I have this piece of code that if compiled as debug works properly, if compiled in release processes the first sequence then exits.
Here is a dump of the data fed to the code:
00 19 01 00 - 00 63 01 00 - 00 67 01 00 - 00 68 01 00
//this is a variable length sequence 00 19 01 00
//0x0019 -> first value (WORD)
//0x01 -> 1B payload (BYTE)
//0x00 -> Value of 00 (BYTEs)
size_t parse(BYTE* DATA, size_t out_size){
BYTE t[258]; // my buffer
size_t b, //byte counter
i; //sequence counter
for (b=0 ,i = 0; i<33&& b<out_size; i++ ){
memset(&t, 0, sizeof(t));
int err=-1;
err = memcpy_s( &(t.m_type),sizeof(t)-b, DATA + b,sizeof(t.m_type)); b += sizeof(t.m_type); //2
if (err) { printf("!!Memcpy_s failed!!n"); return 0; }
err = memcpy_s( &(t.m_len), sizeof(t)-b, DATA+b,sizeof(t.m_len)); b += sizeof(t.m_len); //1
if (err) { printf("!!Memcpy_s failed!!n"); return 0; }
//this second way works also in release
//err = memcpy_s(&t, sizeof(t)-b, DATA + b, 3); b += 2; b += 1; // single step copy
//if (err) { printf("!!Memcpy_s failed!!n"); return 0; }
// this piece of code execute in both cases
err = memcpy_s( &(t.m_value), sizeof(t)-b, DATA + b, t.m_len); b += t.m_len;
if (err) { printf("!!Memcpy_s failed!!n"); return 0; }
//memcpy(&out[ i ], &t, sizeof(t));
out[ i ] = t;
if (t.isEmpty()) {
// as soon as I get a data structure that is empty I exit
printf("Breaking empty data found i:%d, (b:%d)n", i, b);
break;
}
}
return i;
}
in debug I get in the output a structure containing all 3 the elements correctly parsed
in release I got only the first sequence (into the out variable)
Why?
I’m On visual studio 2022, compiling as x86