From a080645844d2c761639707a58e51d400543eabce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCller?= <649785+chmllr@users.noreply.github.com> Date: Thu, 26 May 2022 18:09:45 +0200 Subject: [PATCH] do not append on empty buffer (#268) Co-authored-by: Linwei Shang --- e2e-tests/canisters/reverse.rs | 4 ++++ e2e-tests/tests/e2e.rs | 5 +++++ src/ic-cdk/CHANGELOG.md | 1 + src/ic-cdk/src/api/call.rs | 4 +++- 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/e2e-tests/canisters/reverse.rs b/e2e-tests/canisters/reverse.rs index f8f16c8d4..5afa9ce12 100644 --- a/e2e-tests/canisters/reverse.rs +++ b/e2e-tests/canisters/reverse.rs @@ -6,5 +6,9 @@ fn reverse() { assert_eq!(arg_bytes.len(), arg_data_raw_size()); reply_raw(arg_bytes.into_iter().rev().collect::>().as_ref()); } +#[export_name = "canister_update empty_call"] +fn empty_call() { + reply_raw(&[]); +} fn main() {} diff --git a/e2e-tests/tests/e2e.rs b/e2e-tests/tests/e2e.rs index bc9574f5e..6512d09b7 100644 --- a/e2e-tests/tests/e2e.rs +++ b/e2e-tests/tests/e2e.rs @@ -126,6 +126,11 @@ fn test_raw_api() { let result = env.query(canister_id, "reverse", vec![1, 2, 3, 4]).unwrap(); assert_eq!(result, WasmResult::Reply(vec![4, 3, 2, 1])); + + let result = env + .execute_ingress(canister_id, "empty_call", Default::default()) + .unwrap(); + assert_eq!(result, WasmResult::Reply(Default::default())); } #[test] diff --git a/src/ic-cdk/CHANGELOG.md b/src/ic-cdk/CHANGELOG.md index 75ace612e..1d4d779c4 100644 --- a/src/ic-cdk/CHANGELOG.md +++ b/src/ic-cdk/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Use explicitly type u8 in vector initialization (#264) +- Make `reply_raw` avoid writing empty replies - Uses new format for candid environment variables in import macros. Requires DFX >=0.9.2 (#270) ## [0.5.1] - 2022-05-16 diff --git a/src/ic-cdk/src/api/call.rs b/src/ic-cdk/src/api/call.rs index 04c1888f2..b3bc5a419 100644 --- a/src/ic-cdk/src/api/call.rs +++ b/src/ic-cdk/src/api/call.rs @@ -573,7 +573,9 @@ pub fn arg_data_raw_size() -> usize { /// Replies with the bytes passed pub fn reply_raw(buf: &[u8]) { unsafe { - ic0::msg_reply_data_append(buf.as_ptr() as i32, buf.len() as i32); + if !buf.is_empty() { + ic0::msg_reply_data_append(buf.as_ptr() as i32, buf.len() as i32) + }; ic0::msg_reply(); } }