History

Add & use HMACAuth method that calculates hash atomically

To avoid issues with race conditions involving separate addData() and result() calls use a method that does both in the lock.

Simon Walton committed 8 years ago

Commit 4fa11f11 parent e25e15d3 files · +28 −9Browse files

Showing the changes to libraries/networking/src/NLPacket.cpp (1 of 3 files). Show all

Unified · Split

libraries/networking/src/NLPacket.cpp+4−3
@@ -157,9 +157,10 @@ QByteArray NLPacket::hashForPacketAndHMAC(const udt::Packet& packet, HMACAuth& h · 156 unmodified lines
157 + NUM_BYTES_LOCALID + NUM_BYTES_MD5_HASH;157 + NUM_BYTES_LOCALID + NUM_BYTES_MD5_HASH;
158 158
159 // add the packet payload and the connection UUID159 // add the packet payload and the connection UUID
160 QMutexLocker hashLock(&hash.getLock());160 HMACAuth::HMACHash hashResult;
161 hash.addData(packet.getData() + offset, packet.getDataSize() - offset);161 if (!hash.calculateHash(hashResult, packet.getData() + offset, packet.getDataSize() - offset)) {
162 auto hashResult { hash.result() };162 return QByteArray();
163 }
163 return QByteArray((const char*) hashResult.data(), (int) hashResult.size());164 return QByteArray((const char*) hashResult.data(), (int) hashResult.size());
164}165}
165 166