I'm getting this warning
warning C4309: 'initializing' : truncation of constant value
and when I try to execute my dll it only sends 4 bytes instead of the 10 bytes.
What could be wrong?
Here is my code:
int WINAPI MySend(SOCKET s, const char* buf, int len, int flags)
{
cout << "[SEND:" << len << "] ";
for ( int i = 0; i < len; i++ ) {
printf( "%02x ", static_cast<unsigned char>( buf[i] ) );
}
printf("\n");
//causing the warning:
char storagepkt[] = {0x0A, 0x00, 0x01, 0x40, 0x79, 0xEA, 0x60, 0x1D, 0x6B, 0x3E};
buf = storagepkt;
len = sizeof(storagepkt);
return pSend(s, buf, len, flags);
}
UPDATE
int (WINAPI *pSend)(SOCKET s, const char* buf, int len, int flags) = send;
int WINAPI MySend(SOCKET s, const char* buf, int len, int flags);
UPDATE
As suggested I tried memcpy:
memcpy((char*) buf, storagepkt, sizeof(storagepkt));
UPDATE
unsigned char storagepkt[] = {0x0A, 0x00, 0x01, 0x40, 0x79, 0xEA, 0x60, 0x1D, 0x6B, 0x3E};
Fixed it.
You're initializing a buffer of char
which is signed. Anything over 0x7f
is beyond what it can handle and will be converted to a negative number. The actual data is probably OK and you can ignore the warning, although it would be better to make it unsigned char
.
As for why it's only sending 4 bytes, that sounds suspiciously like the size of a pointer. Are you sure the code is exactly as you've represented it, using an array, rather than a pointer passed to a function? A function doesn't know the size of an array even when you declare the parameter as an array - you need to pass the size of the array into the function.
I can reproduce this warning with the following code:
char aa = 0xff;
the warning is solved with
unsigned char aa = 0xff;
(as Mark Ransom already pointed out, I just added a minimal sample code to reproduce the warning)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With