Skip to content

Commit 52a035d

Browse files
authored
Merge pull request #25 from NordSecurity/safer_enum_converter
Enum converter: write enums based on variant, instead of casting to uint
2 parents ae1d9cd + 41da0b1 commit 52a035d

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Scaffolding: Decorate public functions with `__declspec(dllexport)` under Windows and `__attribute__((visibility("default")))` on other platforms
66
- Core: Make complex function arguments be passed by `const&` for non-callback functions
7+
- Core: Write enums based on variant instead of casting to uint during conversion
78

89
#### v0.4.0+v0.25.0
910

bindgen/src/bindings/cpp/templates/enum_tmpl.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@ RustBuffer {{ ffi_converter_name }}::lower(const {{ type_name }} &val) {
3232
}
3333

3434
void {{ ffi_converter_name }}::write(RustStream &stream, const {{ type_name }} &val) {
35-
stream << static_cast<int32_t>(val);
35+
switch (val) {
36+
{% for variant in e.variants() %}
37+
case {{ type_name }}::{{ variant|variant_name }}:
38+
stream << static_cast<int32_t>({{ loop.index }});
39+
break;
40+
{% endfor %}
41+
default:
42+
throw std::runtime_error("No matching {{ type_name }} variant");
43+
}
3644
}
3745

3846
int32_t {{ ffi_converter_name }}::allocation_size(const {{ type_name|class_name }} &) {
@@ -122,4 +130,4 @@ int32_t {{ ffi_converter_name }}::allocation_size(const {{ type_name|class_name
122130

123131
return size;
124132
}
125-
{%- endif %}
133+
{%- endif %}

0 commit comments

Comments
 (0)