Skip to content

Commit 621a04a

Browse files
committed
Merge #120: proxy-types.h: add static_assert to detect int/enum size mismatch
110349f test: Add coverage for enum/int conversions (Ryan Ofsky) bbc80ab proxy-types.h: add static_assert to detect when an int fields is too small to hold an enum value (Ryan Ofsky) Pull request description: Add static_assert to detect when an int field is too small to hold an enum value This catches the bug TheCharlatan pointed out in bitcoin/bitcoin#29409 (comment) Top commit has no ACKs. Tree-SHA512: cc0adc905e6a6fbd89dd970e5b6675279a514c4e47579b2ef6617c76f0cb18a50dc56539dac300dd89939d5b65cae2e1967c5fae2926b25fe0d36609e86ecb86
2 parents 350067f + 110349f commit 621a04a

File tree

3 files changed

+7
-0
lines changed

3 files changed

+7
-0
lines changed

include/mp/proxy-types.h

+3
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,9 @@ LocalType BuildPrimitive(InvokeContext& invoke_context,
963963
TypeList<LocalType>,
964964
typename std::enable_if<std::is_enum<Value>::value>::type* enable = nullptr)
965965
{
966+
using E = std::make_unsigned_t<std::underlying_type_t<Value>>;
967+
using T = std::make_unsigned_t<LocalType>;
968+
static_assert(std::numeric_limits<T>::max() >= std::numeric_limits<E>::max(), "mismatched integral/enum types");
966969
return static_cast<LocalType>(value);
967970
}
968971

test/mp/test/foo.capnp

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface FooInterface $Proxy.wrap("mp::test::FooImplementation") {
2727
passEmpty @12 (arg :FooEmpty) -> (result :FooEmpty);
2828
passMessage @13 (arg :FooMessage) -> (result :FooMessage);
2929
passMutable @14 (arg :FooMutable) -> (arg :FooMutable);
30+
passEnum @15 (arg :Int32) -> (result :Int32);
3031
}
3132

3233
interface FooCallback $Proxy.wrap("mp::test::FooCallback") {

test/mp/test/foo.h

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ struct FooStruct
2121
std::vector<bool> vbool;
2222
};
2323

24+
enum class FooEnum : int { ONE = 1, TWO = 2, };
25+
2426
struct FooCustom
2527
{
2628
std::string v1;
@@ -72,6 +74,7 @@ class FooImplementation
7274
FooEmpty passEmpty(FooEmpty foo) { return foo; }
7375
FooMessage passMessage(FooMessage foo) { foo.message += " call"; return foo; }
7476
void passMutable(FooMutable& foo) { foo.message += " call"; }
77+
FooEnum passEnum(FooEnum foo) { return foo; }
7578
std::shared_ptr<FooCallback> m_callback;
7679
};
7780

0 commit comments

Comments
 (0)