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

(ubsan) decode integer #22

Merged
merged 1 commit into from
Dec 7, 2023
Merged
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
62 changes: 0 additions & 62 deletions include/scale/detail/fixed_width_integer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,66 +39,4 @@ namespace scale::detail {
out << buf.data()[i];
}
}

/**
* @brief decodeInteger function decodes integer from stream
* @tparam T integer type
* @param stream source stream
* @return decoded value or error
*/
template <class T,
class S,
typename I = std::decay_t<T>,
typename = std::enable_if_t<std::is_integral_v<I>>>
I decodeInteger(S &stream) {
constexpr size_t size = sizeof(I);
static_assert(size <= 8);

// sign bit = 2^(num_bits - 1)
static constexpr auto sign_bit = [](size_t num_bytes) -> uint64_t {
return 0x80ul << (num_bytes * 8);
};

static constexpr auto multiplier = [](size_t num_bytes) -> uint64_t {
return 0x1ul << (num_bytes * 8);
};

if (!stream.hasMore(size)) {
raise(DecodeError::NOT_ENOUGH_DATA);
UNREACHABLE
}

// get integer as 8 bytes from little-endian stream
// and represent it as native-endian unsigned integer
uint64_t v = 0u;

for (size_t i = 0; i < size; ++i) {
v += multiplier(i) * static_cast<uint64_t>(stream.nextByte());
}
// now we have an uint64 native-endian value
// which can store either a signed or an unsigned value

// if the value is actually unsigned, we know that is not greater than
// the max value for type T, so static_cast<T>(v) is safe

// if it is signed and positive, it is also ok
// we can be sure that it is less than max_value<T>/2.
// To check if it is negative we check if the sign bit is present
// in the unsigned representation. (which is true when the value is greater
// than 2^(size_in_bits-1)
bool is_positive_signed = v < sign_bit(size - 1);
if (std::is_unsigned<I>() || is_positive_signed) {
return static_cast<I>(v);
}

// T is a signed integer type and the value v is negative.
// A value is negative, which means that (-x),
// where (-x) is positive, is smaller than sign_bits[size-1].
// Find this x, safely cast to a positive signed and negate the result.
// the bitwise negation operation affects higher bits as well,
// but it doesn't spoil the result.
// static_cast to a smaller type cuts these higher bits off.
I sv = -static_cast<I>((~v) + 1);
return sv;
}
} // namespace scale::detail
11 changes: 8 additions & 3 deletions include/scale/scale_decoder_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace scale {
static constexpr auto is_decoder_stream = true;

explicit ScaleDecoderStream(ConstSpanOfBytes data)
: span_{data}, current_iterator_{span_.begin()}, current_index_{0} {}
: span_{data}, current_index_{0} {}

template <typename T>
T decodeCompact() {
Expand Down Expand Up @@ -144,7 +144,13 @@ namespace scale {
return *this;
}
// decode any other integer
v = detail::decodeInteger<I>(*this);
if (not hasMore(sizeof(T))) {
raise(DecodeError::NOT_ENOUGH_DATA);
}
v = boost::endian::
endian_load<T, sizeof(T), boost::endian::order::little>(
&span_[current_index_]);
current_index_ += sizeof(T);
return *this;
}

Expand Down Expand Up @@ -373,7 +379,6 @@ namespace scale {
}

ByteSpan span_;
SpanIterator current_iterator_;
SizeType current_index_;
};

Expand Down
3 changes: 1 addition & 2 deletions src/scale_decoder_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ namespace scale {
if (not hasMore(1)) {
raise(DecodeError::NOT_ENOUGH_DATA);
}
++current_index_;
return *current_iterator_++;
return span_[current_index_++];
}
} // namespace scale
Loading