From 9fddb12566049e6e89d06b94a28d60ff5409b27c Mon Sep 17 00:00:00 2001 From: Mattias Ellert Date: Mon, 4 Dec 2023 13:00:07 +0100 Subject: [PATCH] Avoid bus errors Do not dereference unaligned pointers. --- src/XProtocol/XProtocol.cc | 4 ++-- src/XrdCl/XrdClXRootDMsgHandler.cc | 4 ++-- src/XrdZip/XrdZipCDFH.hh | 32 +++++++++++++++--------------- src/XrdZip/XrdZipEOCD.hh | 14 ++++++------- src/XrdZip/XrdZipZIP64EOCD.hh | 18 ++++++++--------- src/XrdZip/XrdZipZIP64EOCDL.hh | 6 +++--- 6 files changed, 39 insertions(+), 39 deletions(-) diff --git a/src/XProtocol/XProtocol.cc b/src/XProtocol/XProtocol.cc index e5a7df3dd..da9e74613 100644 --- a/src/XProtocol/XProtocol.cc +++ b/src/XProtocol/XProtocol.cc @@ -204,7 +204,7 @@ char* ClientFattrRequest::VVecInsert( const char *value, char *buffer ) // char* ClientFattrRequest::NVecRead( char* buffer, kXR_unt16 &rc ) { - rc = *reinterpret_cast( buffer ); + memcpy(&rc, buffer, sizeof(kXR_unt16)); rc = htons( rc ); buffer += sizeof( kXR_unt16 ); return buffer; @@ -223,7 +223,7 @@ char* ClientFattrRequest::NVecRead( char* buffer, char *&name ) // char* ClientFattrRequest::VVecRead( char* buffer, kXR_int32 &len ) { - len = *reinterpret_cast( buffer ); + memcpy(&len, buffer, sizeof(kXR_int32)); len = htonl( len ); buffer += sizeof( kXR_int32 ); return buffer; diff --git a/src/XrdCl/XrdClXRootDMsgHandler.cc b/src/XrdCl/XrdClXRootDMsgHandler.cc index 81e41d21b..da6513872 100644 --- a/src/XrdCl/XrdClXRootDMsgHandler.cc +++ b/src/XrdCl/XrdClXRootDMsgHandler.cc @@ -2445,10 +2445,10 @@ namespace XrdCl { if( sizeof( T ) > buflen ) return Status( stError, errDataError ); - result = *reinterpret_cast( buffer ); + memcpy(&result, buffer, sizeof(T)); buffer += sizeof( T ); - buflen -= sizeof( T ); + buflen -= sizeof( T ); return Status(); } diff --git a/src/XrdZip/XrdZipCDFH.hh b/src/XrdZip/XrdZipCDFH.hh index 96851ea35..e6dbb36f2 100644 --- a/src/XrdZip/XrdZipCDFH.hh +++ b/src/XrdZip/XrdZipCDFH.hh @@ -194,22 +194,22 @@ namespace XrdZip //------------------------------------------------------------------------- CDFH( const char *buffer, const uint32_t maxSize = 0 ) { - zipVersion = *reinterpret_cast( buffer + 4 ); - minZipVersion = *reinterpret_cast( buffer + 6 ); - generalBitFlag = *reinterpret_cast( buffer + 8 ); - compressionMethod = *reinterpret_cast( buffer + 10 ); - timestmp.time = *reinterpret_cast( buffer + 12 ); - timestmp.date = *reinterpret_cast( buffer + 14 ); - ZCRC32 = *reinterpret_cast( buffer + 16 ); - compressedSize = *reinterpret_cast( buffer + 20 ); - uncompressedSize = *reinterpret_cast( buffer + 24 ); - filenameLength = *reinterpret_cast( buffer + 28 ); - extraLength = *reinterpret_cast( buffer + 30 ); - commentLength = *reinterpret_cast( buffer + 32 ); - nbDisk = *reinterpret_cast( buffer + 34 ); - internAttr = *reinterpret_cast( buffer + 36 ); - externAttr = *reinterpret_cast( buffer + 38 ); - offset = *reinterpret_cast( buffer + 42 ); + zipVersion = to(buffer + 4); + minZipVersion = to(buffer + 6); + generalBitFlag = to(buffer + 8); + compressionMethod = to(buffer + 10); + timestmp.time = to(buffer + 12); + timestmp.date = to(buffer + 14); + ZCRC32 = to(buffer + 16); + compressedSize = to(buffer + 20); + uncompressedSize = to(buffer + 24); + filenameLength = to(buffer + 28); + extraLength = to(buffer + 30); + commentLength = to(buffer + 32); + nbDisk = to(buffer + 34); + internAttr = to(buffer + 36); + externAttr = to(buffer + 38); + offset = to(buffer + 42); if(maxSize > 0 && (uint32_t)(cdfhBaseSize+filenameLength + extraLength + commentLength) > maxSize){ throw bad_data(); } diff --git a/src/XrdZip/XrdZipEOCD.hh b/src/XrdZip/XrdZipEOCD.hh index d38a6a1c8..575a300bc 100644 --- a/src/XrdZip/XrdZipEOCD.hh +++ b/src/XrdZip/XrdZipEOCD.hh @@ -53,13 +53,13 @@ namespace XrdZip //------------------------------------------------------------------------- EOCD( const char *buffer, uint32_t maxSize = 0 ) { - nbDisk = *reinterpret_cast( buffer + 4 ); - nbDiskCd = *reinterpret_cast( buffer + 6 ); - nbCdRecD = *reinterpret_cast( buffer + 8 ); - nbCdRec = *reinterpret_cast( buffer + 10 ); - cdSize = *reinterpret_cast( buffer + 12 ); - cdOffset = *reinterpret_cast( buffer + 16 ); - commentLength = *reinterpret_cast( buffer + 20 ); + nbDisk = to(buffer + 4); + nbDiskCd = to(buffer + 6); + nbCdRecD = to(buffer + 8); + nbCdRec = to(buffer + 10); + cdSize = to(buffer + 12); + cdOffset = to(buffer + 16); + commentLength = to(buffer + 20); if(maxSize > 0 && (uint32_t)(eocdBaseSize + commentLength) > maxSize) throw bad_data(); comment = std::string( buffer + 22, commentLength ); diff --git a/src/XrdZip/XrdZipZIP64EOCD.hh b/src/XrdZip/XrdZipZIP64EOCD.hh index c7ab0a3bb..09e5ecc4f 100644 --- a/src/XrdZip/XrdZipZIP64EOCD.hh +++ b/src/XrdZip/XrdZipZIP64EOCD.hh @@ -28,15 +28,15 @@ namespace XrdZip ZIP64_EOCD( const char* buffer ): extensibleDataLength( 0 ) { - zip64EocdSize = *reinterpret_cast( buffer + 4 ); - zipVersion = *reinterpret_cast( buffer + 12 ); - minZipVersion = *reinterpret_cast( buffer + 14 ); - nbDisk = *reinterpret_cast( buffer + 16 ); - nbDiskCd = *reinterpret_cast( buffer + 20 ); - nbCdRecD = *reinterpret_cast( buffer + 24 ); - nbCdRec = *reinterpret_cast( buffer + 32 ); - cdSize = *reinterpret_cast( buffer + 40 ); - cdOffset = *reinterpret_cast( buffer + 48 ); + zip64EocdSize = to(buffer + 4); + zipVersion = to(buffer + 12); + minZipVersion = to(buffer + 14); + nbDisk = to(buffer + 16); + nbDiskCd = to(buffer + 20); + nbCdRecD = to(buffer + 24); + nbCdRec = to(buffer + 32); + cdSize = to(buffer + 40); + cdOffset = to(buffer + 48); zip64EocdTotalSize = zip64EocdBaseSize + extensibleDataLength; } diff --git a/src/XrdZip/XrdZipZIP64EOCDL.hh b/src/XrdZip/XrdZipZIP64EOCDL.hh index d2cea2edc..cceca756a 100644 --- a/src/XrdZip/XrdZipZIP64EOCDL.hh +++ b/src/XrdZip/XrdZipZIP64EOCDL.hh @@ -26,9 +26,9 @@ namespace XrdZip //------------------------------------------------------------------------- ZIP64_EOCDL( const char *buffer ) { - nbDiskZip64Eocd = *reinterpret_cast( buffer + 4 ); - zip64EocdOffset = *reinterpret_cast( buffer + 8 ); - totalNbDisks = *reinterpret_cast( buffer + 16 ); + nbDiskZip64Eocd = to(buffer + 4); + zip64EocdOffset = to(buffer + 8); + totalNbDisks = to(buffer + 16); } //------------------------------------------------------------------------- -- 2.43.0