Skip to content

Commit

Permalink
fix CodeQL diagnostics (#568)
Browse files Browse the repository at this point in the history
* try CodeQL using custom build script

* try CodeQL using custom build script

* try CodeQL using custom build script

* try CodeQL using custom build script

* Update codeql.yml

* try CodeQL using custom build script

* try CodeQL using custom build script

* avoid "suspicious sizeof()" from CodeQL
  • Loading branch information
J. Daniel Smith authored Jun 29, 2022
1 parent 32293a5 commit 87e43b4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
20 changes: 12 additions & 8 deletions six/modules/c++/cphd/source/ByteSwap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ class ByteSwapRunnable : public sys::Runnable
const size_t mNumElements;
};

inline const std::byte* calc_offset(const void* input_, size_t offset)
{
auto input = static_cast<const std::byte*>(input_);
return input + offset;
}

template <typename InT>
class ByteSwapAndPromoteRunnable : public sys::Runnable
{
Expand All @@ -84,8 +90,7 @@ class ByteSwapAndPromoteRunnable : public sys::Runnable
size_t numRows,
size_t numCols,
std::complex<float>* output) :
mInput(static_cast<const std::byte*>(input) +
startRow * numCols * sizeof(std::complex<InT>)),
mInput(calc_offset(input, startRow * numCols * sizeof(std::complex<InT>))),
mDims(numRows, numCols),
mOutput(output + startRow * numCols)
{
Expand All @@ -105,9 +110,9 @@ class ByteSwapAndPromoteRunnable : public sys::Runnable
// Have to be careful here - can't treat mInput as a
// std::complex<InT> directly in case InT is a float (see
// explanation in byteSwap() comments)
const std::byte* const input = mInput + inIdx;
const auto input = calc_offset(mInput, inIdx);
byteSwap(input, real);
byteSwap(input + sizeof(InT), imag);
byteSwap(calc_offset(input, sizeof(InT)), imag);

mOutput[outIdx] = std::complex<float>(real,
imag);
Expand All @@ -132,8 +137,7 @@ class ByteSwapAndScaleRunnable : public sys::Runnable
size_t numCols,
const double* scaleFactors,
std::complex<float>* output) :
mInput(static_cast<const std::byte*>(input) +
startRow * numCols * sizeof(std::complex<InT>)),
mInput(calc_offset(input, startRow * numCols * sizeof(std::complex<InT>))),
mDims(numRows, numCols),
mScaleFactors(scaleFactors + startRow),
mOutput(output + startRow * numCols)
Expand All @@ -156,9 +160,9 @@ class ByteSwapAndScaleRunnable : public sys::Runnable
// Have to be careful here - can't treat mInput as a
// std::complex<InT> directly in case InT is a float (see
// explanation in byteSwap() comments)
const std::byte* const input = mInput + inIdx;
const auto input = calc_offset(mInput, inIdx);
byteSwap(input, real);
byteSwap(input + sizeof(InT), imag);
byteSwap(calc_offset(input, sizeof(InT)), imag);

mOutput[outIdx] = std::complex<float>(
static_cast<float>(real * scaleFactor),
Expand Down
34 changes: 25 additions & 9 deletions six/modules/c++/cphd/source/PVPBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,33 @@ template <typename T> inline void setData(const sys::byte* data,
{
setData(reinterpret_cast<const unsigned char*>(data), dest);
}
template <typename T> inline void setData(const double* data_,
T& dest)
{
const void* pData_ = data_;
auto data = static_cast<const unsigned char*>(pData_);
setData(data, dest);
}

inline void setData(const std::byte* data,
inline void setData(const std::byte* data_,
cphd::Vector3& dest)
{
setData(data, dest[0]);
setData(data + sizeof(double), dest[1]);
setData(data + 2*sizeof(double), dest[2]);
const void* pData_ = data_;
auto data = static_cast<const double*>(pData_);
setData(&(data[0]), dest[0]);
setData(&(data[1]), dest[1]);
setData(&(data[2]), dest[2]);
}

// Get data from data struct and put into data block
template <typename T> inline void getData(std::byte* dest,
T value)
{
memcpy(dest, &value, sizeof(T));
memcpy(dest, &value, sizeof(value));
}
inline void getData(double* dest, double value)
{
memcpy(dest, &value, sizeof(value));
}

template <typename T> inline void getData(std::byte* dest,
Expand All @@ -79,12 +92,15 @@ template <typename T> inline void getData(std::byte* dest,
memcpy(dest, value, size);
}

inline void getData(std::byte* dest,
inline void getData(std::byte* dest_,
const cphd::Vector3& value)
{
getData(dest, value[0]);
getData(dest + sizeof(double), value[1]);
getData(dest + 2*sizeof(double), value[2]);
void* pDest_ = dest_;
auto dest = static_cast<double*>(pDest_);

getData(&(dest[0]), value[0]);
getData(&(dest[1]), value[1]);
getData(&(dest[2]), value[2]);
}
}

Expand Down

0 comments on commit 87e43b4

Please sign in to comment.