Avoid undefined behaviour const_casting std::string c_str()
Use non-const data() instead See: #224
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
|
||||
#include "../../stdafx.h"
|
||||
#include "../../string_func.h"
|
||||
#include "../../string_func_extra.h"
|
||||
#include "../../command_type.h"
|
||||
|
||||
#include "packet.h"
|
||||
@@ -337,7 +338,7 @@ void Packet::Recv_string(std::string &buffer, StringValidationSettings settings)
|
||||
size_t length = ttd_strnlen((const char *)(this->buffer + this->pos), this->size - this->pos - 1);
|
||||
buffer.assign((const char *)(this->buffer + this->pos), length);
|
||||
this->pos += length + 1;
|
||||
str_validate(const_cast<char *>(buffer.c_str()), buffer.c_str() + buffer.size(), settings);
|
||||
str_validate_inplace(buffer, settings);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -946,7 +946,7 @@ void NetworkGameLoop()
|
||||
cp->text.resize(MAX_CMD_TEXT_LENGTH);
|
||||
static_assert(MAX_CMD_TEXT_LENGTH > 8192);
|
||||
int ret = sscanf(p, "date{%x; %x; %x}; company: %x; tile: %x (%*u x %*u); p1: %x; p2: %x; p3: " OTTD_PRINTFHEX64 "; cmd: %x; \"%8192[^\"]\"",
|
||||
&next_date, &next_date_fract, &next_tick_skip_counter, &company, &cp->tile, &cp->p1, &cp->p2, &cp->p3, &cp->cmd, const_cast<char *>(cp->text.c_str()));
|
||||
&next_date, &next_date_fract, &next_tick_skip_counter, &company, &cp->tile, &cp->p1, &cp->p2, &cp->p3, &cp->cmd, cp->text.data());
|
||||
/* There are 10 pieces of data to read, however the last is a
|
||||
* string that might or might not exist. Ignore it if that
|
||||
* string misses because in 99% of the time it's not used. */
|
||||
|
@@ -1163,7 +1163,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_DESYNC_LOG(Pack
|
||||
{
|
||||
uint size = p->Recv_uint16();
|
||||
this->server_desync_log.resize(this->server_desync_log.size() + size);
|
||||
p->Recv_binary(const_cast<char *>(this->server_desync_log.data() + this->server_desync_log.size() - size), size);
|
||||
p->Recv_binary(this->server_desync_log.data() + this->server_desync_log.size() - size, size);
|
||||
DEBUG(net, 2, "Received %u bytes of server desync log", size);
|
||||
return NETWORK_RECV_STATUS_OKAY;
|
||||
}
|
||||
|
@@ -1280,7 +1280,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_DESYNC_LOG(Pack
|
||||
{
|
||||
uint size = p->Recv_uint16();
|
||||
this->desync_log.resize(this->desync_log.size() + size);
|
||||
p->Recv_binary(const_cast<char *>(this->desync_log.data() + this->desync_log.size() - size), size);
|
||||
p->Recv_binary(this->desync_log.data() + this->desync_log.size() - size, size);
|
||||
DEBUG(net, 2, "Received %u bytes of client desync log", size);
|
||||
this->receive_limit += p->size;
|
||||
return NETWORK_RECV_STATUS_OKAY;
|
||||
|
Reference in New Issue
Block a user