Skip to content

Commit 8d1bdbc

Browse files
authored
Merge pull request #13 from extism/handle-errors
handle allocation errors
2 parents d364019 + a8f7a79 commit 8d1bdbc

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

http/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ use extism_pdk::*;
44
pub fn http_get(Json(input): Json<HttpRequest>) -> FnResult<Memory> {
55
let res = http::request::<()>(&input, None)?;
66
let res = res.to_memory()?;
7+
8+
if res.offset() == 0 {
9+
return Err(WithReturnCode::new(Error::msg("Failed to allocate memory"), 1));
10+
}
11+
712
Ok(res)
813
}
914

@@ -18,5 +23,10 @@ struct HttpRequestWithBody {
1823
pub fn http_post(Json(input): Json<HttpRequestWithBody>) -> FnResult<Memory> {
1924
let res = http::request::<&str>(&input.req, Some(&input.data))?;
2025
let res = res.into_memory();
26+
27+
if res.offset() == 0 {
28+
return Err(WithReturnCode::new(Error::msg("Failed to allocate memory"), 1));
29+
}
30+
2131
Ok(res)
2232
}

memory/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@ struct AllocRequest {
88

99
#[plugin_fn]
1010
pub unsafe fn alloc_memory(Json(input): Json<AllocRequest>) -> FnResult<()> {
11-
_ = extism::alloc(input.bytes);
11+
let offs = extism::alloc(input.bytes);
12+
if offs == 0 {
13+
return Err(WithReturnCode::new(Error::msg("Failed to allocate memory"), 1));
14+
}
1215

1316
Ok(())
1417
}
1518

1619
#[plugin_fn]
1720
pub unsafe fn alloc_var(Json(input): Json<AllocRequest>) -> FnResult<()> {
1821
let buffer = vec![0u8; input.bytes as usize];
19-
_ = var::set("buffer", buffer);
22+
23+
if let Err(e) = var::set("buffer", buffer) {
24+
return Err(WithReturnCode::from(e));
25+
}
2026

2127
Ok(())
2228
}

0 commit comments

Comments
 (0)