Skip to content
Snippets Groups Projects
Commit 8d34afd2 authored by Scott Snyder's avatar Scott Snyder Committed by Adam Edward Barton
Browse files

CaloCondBlobObjs: Fix unaligned writes.

CaloCondBlobObjs: Fix unaligned writes.

Fix potential unaligned writes of 64-bit quantities, using 
CxxUtils::set_unaligned.
Fixes UB sanitizer warnings seen in the dbg build.
parent aa134b5e
No related branches found
No related tags found
No related merge requests found
......@@ -12,7 +12,7 @@ atlas_add_library( CaloCondBlobObjs
src/*.cxx
PUBLIC_HEADERS CaloCondBlobObjs
INCLUDE_DIRS ${CORAL_INCLUDE_DIRS}
LINK_LIBRARIES ${CORAL_LIBRARIES} GaudiKernel
LINK_LIBRARIES ${CORAL_LIBRARIES} GaudiKernel CxxUtils
PRIVATE_LINK_LIBRARIES AthenaKernel )
atlas_add_dictionary( CaloCondBlobObjsDict
......
/*
Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
*/
#ifndef CALOCONDBLOBOBJS_CALOCONDBLOBBASE_H
......@@ -38,6 +38,7 @@
#include "CoralBase/Blob.h"
#include "CaloCondBlobObjs/Exception.h"
#include "CaloCondBlobObjs/CaloCondType.h"
#include "CxxUtils/get_unaligned.h"
class CaloCondBlobBase{
public:
......@@ -283,8 +284,9 @@ inline uint64_t
CaloCondBlobBase::getTimeStamp() const
{
if(!getCommentSizeUint32()) return 0;
return *(reinterpret_cast<const uint64_t*>(getBlobStart()+getHdrSize() +
getNObjs()*getObjSizeUint32()));
auto p = reinterpret_cast<const uint8_t*>(getBlobStart()+getHdrSize() +
getNObjs()*getObjSizeUint32());
return CxxUtils::get_unaligned64 (p);
}
#endif
/*
Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
*/
#include "CaloCondBlobObjs/CaloCondBlobBase.h"
#include "CaloCondBlobObjs/CaloCondType.h"
#include "CxxUtils/set_unaligned.h"
#include <algorithm>
#include <ctime>
......@@ -121,9 +122,8 @@ CaloCondBlobBase::createBlob(uint16_t objType,
//==== fill comment fields
if(commentSizeChar){
if(!timeStamp) timeStamp = ::time(nullptr);
uint64_t* pTimeStamp = reinterpret_cast<uint64_t*>(getBlobStart()+dataSizeByte/sizeof(uint32_t));
pTimeStamp[0] = timeStamp;
char* pChar = reinterpret_cast<char*>(++pTimeStamp);
uint8_t* pChar = reinterpret_cast<uint8_t*> (getBlobStart()+dataSizeByte/sizeof(uint32_t));
CxxUtils::set_unaligned<uint64_t> (pChar, timeStamp);
std::string::const_iterator iStr = author.begin();
for(; iStr!=author.end(); ++iStr){ *pChar = *iStr; ++pChar; }
*pChar = 0;
......
/*
Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
*/
#define BOOST_TEST_DYN_LINK
......@@ -11,6 +11,7 @@ namespace utf = boost::unit_test;
#include "CaloCondBlobObjs/CaloCondBlobBase.h"
#include "CoralBase/Blob.h"
#include "CxxUtils/set_unaligned.h"
#include <cstdint>
#include <sstream>
#include <iostream>
......@@ -117,13 +118,12 @@ BOOST_AUTO_TEST_CASE(PublicMethods, * utf::expected_failures(1)){
pDestination[2] = num;
pDestination[3] = 0x80000000 | (gain << 24) | nChans;
pDestination[4] = comment.size() / wordSize;
uint64_t* pTimeStamp = reinterpret_cast<uint64_t*>(pDestination+dataSizeWord);
uint8_t* pChar = reinterpret_cast<uint8_t*>(pDestination+dataSizeWord);
uint64_t timeStamp{0};
pTimeStamp[0] = timeStamp;
char* pChar = reinterpret_cast<char*>(++pTimeStamp);
strcpy(pChar, author.c_str());
CxxUtils::set_unaligned<uint64_t> (pChar, timeStamp);
strcpy(reinterpret_cast<char*>(pChar), author.c_str());
pChar+=(1+author.size());
strcpy(pChar, comment.c_str());
strcpy(reinterpret_cast<char*>(pChar), comment.c_str());
//instantiate the class using this blob
CaloCondBlobStub blobStub(b);
//checking...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment