summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2019-01-07 19:46:15 +0000
committeriphydf <iphydf@users.noreply.github.com>2019-01-10 19:37:28 +0000
commit3dd31b1fd06a39a460e259ad525449b7f64e4a70 (patch)
tree0539b4443527bec976a5b26afac877219fc3279a /toxcore
parent86935beb2fc04f4cefc47b77c85fb1ab02f0e05d (diff)
Fix misaligned 4-byte access in trace logging.
This code is undefined behaviour as is, and breaks on various platforms requiring strict alignment (many microcontrollers).
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/network.c21
1 files changed, 14 insertions, 7 deletions
diff --git a/toxcore/network.c b/toxcore/network.c
index 917556ac..ca363efa 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -405,16 +405,23 @@ bool set_socket_dualstack(Socket sock)
405 405
406static uint32_t data_0(uint16_t buflen, const uint8_t *buffer) 406static uint32_t data_0(uint16_t buflen, const uint8_t *buffer)
407{ 407{
408 // TODO(iphydf): Do this differently. Right now this is most likely a 408 uint32_t data = 0;
409 // misaligned memory access in reality, and definitely undefined behaviour 409
410 // in terms of C standard. 410 if (buflen > 4) {
411 const uint8_t *const start = buffer + 1; 411 net_unpack_u32(buffer + 1, &data);
412 return buflen > 4 ? net_ntohl(*(const uint32_t *)start) : 0; 412 }
413
414 return data;
413} 415}
414static uint32_t data_1(uint16_t buflen, const uint8_t *buffer) 416static uint32_t data_1(uint16_t buflen, const uint8_t *buffer)
415{ 417{
416 const uint8_t *const start = buffer + 5; 418 uint32_t data = 0;
417 return buflen > 7 ? net_ntohl(*(const uint32_t *)start) : 0; 419
420 if (buflen > 7) {
421 net_unpack_u32(buffer + 5, &data);
422 }
423
424 return data;
418} 425}
419 426
420static void loglogdata(const Logger *log, const char *message, const uint8_t *buffer, 427static void loglogdata(const Logger *log, const char *message, const uint8_t *buffer,