Skip to content

Commit a080645

Browse files
chmllrlwshang
andauthored
do not append on empty buffer (#268)
Co-authored-by: Linwei Shang <[email protected]>
1 parent 468cc63 commit a080645

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

e2e-tests/canisters/reverse.rs

+4
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ fn reverse() {
66
assert_eq!(arg_bytes.len(), arg_data_raw_size());
77
reply_raw(arg_bytes.into_iter().rev().collect::<Vec<_>>().as_ref());
88
}
9+
#[export_name = "canister_update empty_call"]
10+
fn empty_call() {
11+
reply_raw(&[]);
12+
}
913

1014
fn main() {}

e2e-tests/tests/e2e.rs

+5
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ fn test_raw_api() {
126126

127127
let result = env.query(canister_id, "reverse", vec![1, 2, 3, 4]).unwrap();
128128
assert_eq!(result, WasmResult::Reply(vec![4, 3, 2, 1]));
129+
130+
let result = env
131+
.execute_ingress(canister_id, "empty_call", Default::default())
132+
.unwrap();
133+
assert_eq!(result, WasmResult::Reply(Default::default()));
129134
}
130135

131136
#[test]

src/ic-cdk/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010

1111
### Fixed
1212
- Use explicitly type u8 in vector initialization (#264)
13+
- Make `reply_raw` avoid writing empty replies
1314
- Uses new format for candid environment variables in import macros. Requires DFX >=0.9.2 (#270)
1415

1516
## [0.5.1] - 2022-05-16

src/ic-cdk/src/api/call.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,9 @@ pub fn arg_data_raw_size() -> usize {
573573
/// Replies with the bytes passed
574574
pub fn reply_raw(buf: &[u8]) {
575575
unsafe {
576-
ic0::msg_reply_data_append(buf.as_ptr() as i32, buf.len() as i32);
576+
if !buf.is_empty() {
577+
ic0::msg_reply_data_append(buf.as_ptr() as i32, buf.len() as i32)
578+
};
577579
ic0::msg_reply();
578580
}
579581
}

0 commit comments

Comments
 (0)