diff options
author | iphydf <iphydf@users.noreply.github.com> | 2019-01-07 19:46:15 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2019-01-10 19:37:28 +0000 |
commit | 3dd31b1fd06a39a460e259ad525449b7f64e4a70 (patch) | |
tree | 0539b4443527bec976a5b26afac877219fc3279a /toxcore/network.c | |
parent | 86935beb2fc04f4cefc47b77c85fb1ab02f0e05d (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/network.c')
-rw-r--r-- | toxcore/network.c | 21 |
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 | ||
406 | static uint32_t data_0(uint16_t buflen, const uint8_t *buffer) | 406 | static 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 | } |
414 | static uint32_t data_1(uint16_t buflen, const uint8_t *buffer) | 416 | static 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 | ||
420 | static void loglogdata(const Logger *log, const char *message, const uint8_t *buffer, | 427 | static void loglogdata(const Logger *log, const char *message, const uint8_t *buffer, |