Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undefined behaviour #426

Merged
merged 2 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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