summaryrefslogtreecommitdiff
path: root/nrf-softdevice-gen
diff options
context:
space:
mode:
authorDario Nieuwenhuis <dirbaio@dirbaio.net>2020-09-09 00:16:52 +0200
committerDario Nieuwenhuis <dirbaio@dirbaio.net>2020-09-09 00:56:38 +0200
commitb14ea37486ef1d515b852469f9a0f699c8790c7d (patch)
tree98a15c777ed4382650532dc3d56957c8ea38d6d1 /nrf-softdevice-gen
parentf7c21b467a1b4661b1ec87c74c5da7483a14158b (diff)
downloadnrf-softdevice-b14ea37486ef1d515b852469f9a0f699c8790c7d.zip
Fix UB regarding bindgen unions and dynamically-sized array members.
Diffstat (limited to 'nrf-softdevice-gen')
-rw-r--r--nrf-softdevice-gen/src/main.rs7
1 files changed, 7 insertions, 0 deletions
diff --git a/nrf-softdevice-gen/src/main.rs b/nrf-softdevice-gen/src/main.rs
index fd30102..4ee2bf0 100644
--- a/nrf-softdevice-gen/src/main.rs
+++ b/nrf-softdevice-gen/src/main.rs
@@ -85,6 +85,13 @@ fn main() {
}
gen_bindings(&tmp_dir, &src_dir, &tmp_bindings_path, |data| {
+ // Change all "dynamically-sized" arrays from length 1 to length 0.
+ // This avoids UB when creating Rust references to structs when the length is 0.
+ //
+ // We can't use a "real" flexible array ([] instead of [0]) because
+ // they're used inside enums :(
+ let data = data.replace("[1];", "[0];");
+
let re = Regex::new(r"SVCALL\((?P<svc>[A-Za-z0-9_]+),\s*(?P<ret>[A-Za-z0-9_]+),\s*(?P<name>[A-Za-z0-9_]+)\((?P<args>.*)\)\);").unwrap();
re.replace_all(&data, "$ret $name($args);").into()
});