Skip to content

Commit 8761aa0

Browse files
authored
Fix 30 bugs found during code review (#618)
1 parent 31c056c commit 8761aa0

45 files changed

Lines changed: 1667 additions & 130 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/windows.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ jobs:
5151
cmake -S . -B build -DCMAKE_CXX_STANDARD=20 -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_JSON=OFF -DREFLECTCPP_${{ matrix.format }}=ON -DCMAKE_BUILD_TYPE=Release
5252
cmake --build build --config Release -j4
5353
- name: Run tests
54-
if: matrix.format != 'benchmarks'
54+
if: matrix.format != 'benchmarks'
5555
run: |
56-
ctest --test-dir build --output-on-failure
56+
ctest --test-dir build -C Release --output-on-failure
5757
- name: Run benchmarks
5858
if: matrix.format == 'benchmarks'
5959
run: |
@@ -106,6 +106,6 @@ jobs:
106106
cmake --build build --config Release -j4
107107
- name: Run tests
108108
run: |
109-
ctest --test-dir build --output-on-failure
109+
ctest --test-dir build -C Release --output-on-failure
110110
111111

MR_DESCRIPTION.md

Lines changed: 385 additions & 0 deletions
Large diffs are not rendered by default.

include/rfl/Field.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct Field {
3434
Field(const Field<_name, U>& _field) : value_(_field.get()) {}
3535

3636
template <class U>
37-
Field(Field<_name, U>&& _field) : value_(_field.get()) {}
37+
Field(Field<_name, U>&& _field) : value_(std::move(_field.get())) {}
3838

3939
template <class U>
4040
requires std::is_convertible_v<U, Type>

include/rfl/Flatten.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ struct Flatten {
2929
Flatten(const Flatten<U>& _f) : value_(_f.get()) {}
3030

3131
template <class U>
32-
Flatten(Flatten<U>&& _f) : value_(_f.get()) {}
32+
Flatten(Flatten<U>&& _f) : value_(std::move(_f.get())) {}
3333

3434
template <class U>
3535
requires std::is_convertible_v<U, Type>
3636
Flatten(const U& _value) : value_(_value) {}
3737

3838
template <class U>
3939
requires std::is_convertible_v<U, Type>
40-
Flatten(U&& _value) : value_(_value) {}
40+
Flatten(U&& _value) : value_(std::forward<U>(_value)) {}
4141

4242
~Flatten() = default;
4343

@@ -101,7 +101,7 @@ struct Flatten {
101101
/// Assigns the underlying object.
102102
template <class U>
103103
Flatten& operator=(Flatten<U>&& _f) {
104-
value_ = std::forward<U>(_f);
104+
value_ = std::move(_f.get());
105105
return *this;
106106
}
107107

include/rfl/Generic.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef RFL_GENERIC_HPP_
22
#define RFL_GENERIC_HPP_
33

4+
#include <limits>
45
#include <optional>
56
#include <ostream>
67
#include <string>
@@ -177,6 +178,11 @@ class RFL_API Generic {
177178
[](auto _v) -> Result<int> {
178179
using V = std::remove_cvref_t<decltype(_v)>;
179180
if constexpr (std::is_same_v<V, int64_t>) {
181+
if (_v < static_cast<int64_t>(std::numeric_limits<int>::min()) ||
182+
_v > static_cast<int64_t>(std::numeric_limits<int>::max())) {
183+
return error(
184+
"rfl::Generic: int64_t value out of range for int.");
185+
}
180186
return static_cast<int>(_v);
181187
} else {
182188
return error(

include/rfl/Object.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Object {
2626
using mapped_type = T;
2727
using value_type = std::pair<std::string, T>;
2828
using size_type = typename DataType::size_type;
29-
using difference_type = typename DataType::size_type;
29+
using difference_type = typename DataType::difference_type;
3030
using reference = value_type&;
3131
using const_reference = const value_type&;
3232
using pointer = typename DataType::pointer;

include/rfl/Result.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ class Result {
211211
requires std::is_convertible_v<U, T>
212212
auto& operator=(const Result<U>& _other) {
213213
const auto to_t = [](const U& _u) -> T { return _u; };
214-
t_or_err_ = _other.transform(to_t).t_or_err_;
214+
auto temp = _other.transform(to_t);
215+
destroy();
216+
success_ = temp.success_;
217+
move_from_other(temp);
215218
return *this;
216219
}
217220

@@ -292,7 +295,7 @@ class Result {
292295
}
293296

294297
/// Returns the value or a default.
295-
T&& value_or(T&& _default) && noexcept {
298+
T value_or(T&& _default) && noexcept {
296299
if (success_) {
297300
return std::move(*this).get_t();
298301
} else {
@@ -332,7 +335,7 @@ class Result {
332335

333336
bool has_value() const noexcept { return success_; }
334337

335-
Error& error() && {
338+
Error&& error() && {
336339
if (success_) throw std::runtime_error("Expected does not contain value");
337340
return std::move(*this).get_err();
338341
}

include/rfl/TaggedUnion.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace rfl {
1212
// https://serde.rs/enum-representations.html
1313
template <internal::StringLiteral _discriminator, class... Ts>
1414
struct TaggedUnion {
15-
static constexpr internal::StringLiteral discrimininator_ = _discriminator;
15+
static constexpr internal::StringLiteral discriminator_ = _discriminator;
1616

1717
/// The type of the underlying variant.
1818
using VariantType = rfl::Variant<Ts...>;

include/rfl/Tuple.hpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,19 @@ class Tuple {
102102
static_assert(sizeof...(Types) == sizeof...(OtherTypes),
103103
"The size of the two tuples must be the same.");
104104

105-
const auto compare = [&]<int _i>(std::strong_ordering* _ordering,
106-
std::integral_constant<int, _i>) {
107-
if (*_ordering != std::strong_ordering::equivalent &&
108-
this->get<_i>() != _other.template get<_i>()) {
109-
*_ordering = (this->get<_i>() <=> _other.template get<_i>());
110-
}
105+
using OrderingType = std::common_comparison_category_t<
106+
decltype(std::declval<Types>() <=> std::declval<OtherTypes>())...>;
107+
108+
auto ordering = OrderingType::equivalent;
109+
110+
const auto compare = [&]<int _i>(std::integral_constant<int, _i>) {
111+
ordering = static_cast<OrderingType>(
112+
this->get<_i>() <=> _other.template get<_i>());
113+
return ordering != 0;
111114
};
112115

113116
return [&]<int... _is>(std::integer_sequence<int, _is...>) {
114-
auto ordering = std::strong_ordering::equivalent;
115-
(compare(&ordering, std::integral_constant<int, _is>{}), ...);
117+
(compare(std::integral_constant<int, _is>{}) || ...);
116118
return ordering;
117119
}(std::make_integer_sequence<int, sizeof...(Types)>());
118120
}

include/rfl/bson/Reader.hpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,24 @@ struct Reader {
124124
return value.v_bool;
125125

126126
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
127-
if (btype != BSON_TYPE_DOUBLE) {
128-
return error(
129-
"Could not cast to numeric value. The type must be double, "
130-
"int32, int64 or date_time.");
127+
switch (btype) {
128+
case BSON_TYPE_DOUBLE:
129+
return static_cast<T>(value.v_double);
130+
131+
case BSON_TYPE_INT32:
132+
return static_cast<T>(value.v_int32);
133+
134+
case BSON_TYPE_INT64:
135+
return static_cast<T>(value.v_int64);
136+
137+
case BSON_TYPE_DATE_TIME:
138+
return static_cast<T>(value.v_datetime);
139+
140+
default:
141+
return error(
142+
"Could not cast to numeric value. The type must be double, "
143+
"int32, int64 or date_time.");
131144
}
132-
return static_cast<T>(value.v_double);
133145

134146
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
135147
switch (btype) {

0 commit comments

Comments
 (0)