Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introducing binary safe type cast utility. #1073

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Sources/Core/Math.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
#include <algorithm> // std::max / std::min
#include <cstdint> // uint32_t --> msvc
#include <cmath>
#include <cstring>
#include <random>
#include <string>
#include <vector>
#include <functional>
#include <type_traits>
#include <mutex>

namespace spades {
Expand Down Expand Up @@ -973,6 +975,20 @@ namespace spades {
std::string TrimSpaces(const std::string &);

float SmoothStep(float);

/*
* binary safe cast, unlike traditional c++ cast operators, it needs to operate on a matching type size
*
* if the code updates to c++ 20 it can be dropped
*/
template<bool dest, class src> using EnableIf = typename std::enable_if<dest, src>::type;
template<class dest, class src>
EnableIf<sizeof(dest) == sizeof(src) && std::is_trivially_copyable<dest>::value && std::is_trivially_copyable<src>::value, dest>
SafeCast(const src& s) {
dest d;
std::memcpy(&d, &s, sizeof(dest));
return d;
}
}

namespace std {
Expand Down
8 changes: 4 additions & 4 deletions Sources/Core/OpusAudioStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ namespace spades {
switch (whence) {
case SEEK_CUR:
self.stream->SetPosition(
static_cast<std::uint64_t>(offset + self.stream->GetPosition()));
SafeCast<std::uint64_t>(offset + self.stream->GetPosition()));
break;
case SEEK_SET: self.stream->SetPosition(static_cast<std::uint64_t>(offset)); break;
case SEEK_END:
self.stream->SetPosition(
static_cast<std::uint64_t>(offset + self.stream->GetLength()));
SafeCast<std::uint64_t>(offset + self.stream->GetLength()));
break;
}
return 0;
};
opusCallback->tell = [](void *stream) -> opus_int64 {
auto &self = *reinterpret_cast<OpusAudioStream *>(stream);
return static_cast<opus_int64>(self.stream->GetPosition());
return SafeCast<opus_int64>(self.stream->GetPosition());
};
opusCallback->close = nullptr;

Expand Down Expand Up @@ -220,7 +220,7 @@ namespace spades {
void OpusAudioStream::SetPosition(uint64_t pos) {
pos = std::min(pos, GetLength());

op_pcm_seek(opusFile, static_cast<opus_int64>(pos / (channels * 4)));
op_pcm_seek(opusFile, SafeCast<opus_int64>(pos / (channels * 4)));

subsamplePosition = static_cast<int>(pos % (channels * 4));
if (subsamplePosition) {
Expand Down
5 changes: 3 additions & 2 deletions Sources/Core/SdlFileStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "SdlFileStream.h"
#include "Debug.h"
#include "Exception.h"
#include "Math.h"

namespace spades {
SdlFileStream::SdlFileStream(SDL_RWops *f, bool ac) : ops(f), autoClose(ac) {
Expand Down Expand Up @@ -76,7 +77,7 @@ namespace spades {
if (pos == -1) {
SPRaise("This stream doesn't support seeking.");
}
return static_cast<uint64_t>(pos);
return SafeCast<uint64_t>(pos);
}

void SdlFileStream::SetPosition(uint64_t pos) {
Expand All @@ -86,7 +87,7 @@ namespace spades {
SPRaise("Currently SdlFileStream doesn't support 64-bit offset.");
}

if (SDL_RWseek(ops, static_cast<Sint64>(pos), RW_SEEK_SET) == -1) {
if (SDL_RWseek(ops, SafeCast<Sint64>(pos), RW_SEEK_SET) == -1) {
SPRaise("This stream doesn't support seeking.");
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Draw/SWImageRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ namespace spades {
}
if (numSteps == 0)
numSteps = 1;
b.divisor = static_cast<unsigned int>(numSteps);
b.divisor = SafeCast<unsigned int>(numSteps);
b.dividend = 0;
b.largePos = start;
b.step = static_cast<unsigned int>(distance);
b.step = SafeCast<unsigned int>(distance);
} else {
mode = 0;
if (numSteps == 0) {
Expand Down