diff --git a/src/qgcunittest/MockMavlinkFileServer.cc b/src/qgcunittest/MockMavlinkFileServer.cc index f67592735ce2380a11e8c42ea9d1089f45d3a4ab..b4945b3b015cf72aab58221f9b87dc9042934e00 100644 --- a/src/qgcunittest/MockMavlinkFileServer.cc +++ b/src/qgcunittest/MockMavlinkFileServer.cc @@ -139,7 +139,7 @@ void MockMavlinkFileServer::_openCommand(QGCUASFileManager::Request* request, ui // Data contains file length response.hdr.size = sizeof(uint32_t); - *((uint32_t*)response.data) = _readFileLength; + response.openFileLength = _readFileLength; _emitResponse(&response, outgoingSeqNumber); } diff --git a/src/uas/QGCUASFileManager.cc b/src/uas/QGCUASFileManager.cc index 65fd98ee5270b2bbbd067249111bbefdde31c88e..483bccbc38fcf6884009055102430d9443a753c7 100644 --- a/src/uas/QGCUASFileManager.cc +++ b/src/uas/QGCUASFileManager.cc @@ -107,8 +107,7 @@ void QGCUASFileManager::_openAckResponse(Request* openAck) // File length comes back in data Q_ASSERT(openAck->hdr.size == sizeof(uint32_t)); - uint32_t fileLength = *((uint32_t*)&openAck->data[0]); - emit openFileLength(fileLength); + emit openFileLength(openAck->openFileLength); _readOffset = 0; // Start reading at beginning of file _readFileAccumulator.clear(); // Start with an empty file diff --git a/src/uas/QGCUASFileManager.h b/src/uas/QGCUASFileManager.h index e63dcc69496d33b74a4b18a51b2a5fc50b81f924..57a54255e09f1c30b20616bdbb4d8a331ae57be4 100644 --- a/src/uas/QGCUASFileManager.h +++ b/src/uas/QGCUASFileManager.h @@ -71,9 +71,16 @@ protected: struct Request { struct RequestHeader hdr; - // The entire Request must fit into the data member of the mavlink_encapsulated_data_t structure. We use as many leftover bytes - // after we use up space for the RequestHeader for the data portion of the Request. - uint8_t data[sizeof(((mavlink_encapsulated_data_t*)0)->data) - sizeof(RequestHeader)]; + + // We use a union here instead of just casting (uint32_t)&data[0] to not break strict aliasing rules + union { + // The entire Request must fit into the data member of the mavlink_encapsulated_data_t structure. We use as many leftover bytes + // after we use up space for the RequestHeader for the data portion of the Request. + uint8_t data[sizeof(((mavlink_encapsulated_data_t*)0)->data) - sizeof(RequestHeader)]; + + // File length returned by Open command + uint32_t openFileLength; + }; }; enum Opcode