Skip to content

Commit

Permalink
Merge pull request #426 from Wargus/tim/402-undefined-behaviour
Browse files Browse the repository at this point in the history
Fix undefined behaviour
  • Loading branch information
timfel authored Jan 10, 2023
2 parents e5ca6aa + 6e69241 commit d701d7a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
26 changes: 17 additions & 9 deletions src/network/net_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,32 @@
size_t serialize32(unsigned char *buf, uint32_t data)
{
if (buf) {
*reinterpret_cast<uint32_t *>(buf) = htonl(data);
uint32_t val = htonl(data);
memcpy(buf, &val, sizeof(val));
}
return sizeof(data);
}
size_t serialize32(unsigned char *buf, int32_t data)
{
if (buf) {
*reinterpret_cast<int32_t *>(buf) = htonl(data);
int32_t val = htonl(data);
memcpy(buf, &val, sizeof(val));
}
return sizeof(data);
}
size_t serialize16(unsigned char *buf, uint16_t data)
{
if (buf) {
*reinterpret_cast<uint16_t *>(buf) = htons(data);
uint16_t val = htons(data);
memcpy(buf, &val, sizeof(val));
}
return sizeof(data);
}
size_t serialize16(unsigned char *buf, int16_t data)
{
if (buf) {
*reinterpret_cast<int16_t *>(buf) = htons(data);
int16_t val = htons(data);
memcpy(buf, &val, sizeof(val));
}
return sizeof(data);
}
Expand Down Expand Up @@ -136,22 +140,26 @@ size_t serialize(unsigned char *buf, const std::vector<unsigned char> &data)

size_t deserialize32(const unsigned char *buf, uint32_t *data)
{
*data = ntohl(*reinterpret_cast<const uint32_t *>(buf));
memcpy(data, buf, sizeof(*data));
*data = ntohl(*data);
return sizeof(*data);
}
size_t deserialize32(const unsigned char *buf, int32_t *data)
{
*data = ntohl(*reinterpret_cast<const int32_t *>(buf));
memcpy(data, buf, sizeof(*data));
*data = ntohl(*data);
return sizeof(*data);
}
size_t deserialize16(const unsigned char *buf, uint16_t *data)
{
*data = ntohs(*reinterpret_cast<const uint16_t *>(buf));
memcpy(data, buf, sizeof(*data));
*data = ntohs(*data);
return sizeof(*data);
}
size_t deserialize16(const unsigned char *buf, int16_t *data)
{
*data = ntohs(*reinterpret_cast<const int16_t *>(buf));
memcpy(data, buf, sizeof(*data));
*data = ntohs(*data);
return sizeof(*data);
}
size_t deserialize8(const unsigned char *buf, uint8_t *data)
Expand Down Expand Up @@ -285,7 +293,7 @@ size_t CServerSetup::Deserialize(const unsigned char *p)
p += deserialize8(p, reinterpret_cast<int8_t*>(&this->ServerGameSettings.Resources));
p += deserialize8(p, reinterpret_cast<int8_t*>(&this->ServerGameSettings.RevealMap));
// The bitfield contains Inside and NoFogOfWar, as well as game-defined settings
p += deserialize32(p, reinterpret_cast<uint32_t*>(&this->ServerGameSettings._Bitfield));
p += deserialize32(p, &this->ServerGameSettings._Bitfield);

for (int i = 0; i < PlayerMax; ++i) {
p += deserialize8(p, reinterpret_cast<int8_t*>(&this->ServerGameSettings.Presets[i].Race));
Expand Down
18 changes: 9 additions & 9 deletions src/network/online_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,20 +356,19 @@ class BNCSOutputStream {

void serialize32(uint32_t data) {
ensureSpace(sizeof(data));
uint32_t *view = reinterpret_cast<uint32_t *>(buf + pos);
*view = htonl(data);
uint32_t val = htonl(data);
memcpy(buf + pos, &val, sizeof(val));
pos += sizeof(data);
};
void serialize32NativeByteOrder(uint32_t data) {
ensureSpace(sizeof(data));
uint32_t *view = reinterpret_cast<uint32_t *>(buf + pos);
*view = data;
memcpy(buf + pos, &data, sizeof(data));
pos += sizeof(data);
};
void serialize16(uint16_t data) {
ensureSpace(sizeof(data));
uint16_t *view = reinterpret_cast<uint16_t *>(buf + pos);
*view = htons(data);
uint16_t val = htons(data);
memcpy(buf + pos, &val, sizeof(val));
pos += sizeof(data);
};
void serialize8(uint8_t data) {
Expand Down Expand Up @@ -404,8 +403,8 @@ class BNCSOutputStream {
uint8_t *getBuffer() {
// if needed, insert length to make it a valid buffer
if (length_pos >= 0) {
uint16_t *view = reinterpret_cast<uint16_t *>(buf + length_pos);
*view = pos;
uint16_t val = pos;
memcpy(buf + length_pos, &val, sizeof(val));
}
return buf;
};
Expand Down Expand Up @@ -1399,7 +1398,8 @@ class Context : public OnlineContext {
// (UINT32) 0x05 0x00 0x00 0x00
// (UINT32) UDP Code
{
const uint32_t udpCode = reinterpret_cast<const uint32_t*>(buffer)[1];
uint32_t udpCode;
memcpy(&udpCode, buffer + (sizeof(uint32_t) / sizeof(uint8_t)), sizeof(udpCode));
BNCSOutputStream udppingresponse(0x14);
udppingresponse.serialize32(udpCode);
udppingresponse.flush(getTCPSocket());
Expand Down
13 changes: 13 additions & 0 deletions src/pathfinder/astar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@
// 02111-1307, USA.
//

// timfel: How to debug the pathfinder.
// In debug builds, there is a lua function "DumpNextAStar". The way I use this
// is to enter a game with stdout redirected to a file (that's the default on
// Windows), make sure nothing is moving, then hit "Return/Enter" to write a
// cheat code, and then type "eval DumpNextAStar()". Then send a unit somewhere.
// Next, open the stdout file using "less -R -z64 stdout.txt". Replace the "64"
// with the height of your map. The entire map will have been dumped using RGB colors
// to stdout. I use the arrow keys to position the first line of the first A*
// iteration at the top of my terminal, and then hit "Space" repeatedly. If the
// "-z64" argument used the correct map height and the terminal is wide enough,
// each time I hit space, it will scroll exactly far enough to have the next A*
// iteration first line at the top of my terminal, giving a sort of poor animation.

//@{

/*----------------------------------------------------------------------------
Expand Down

0 comments on commit d701d7a

Please sign in to comment.