Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

cli: reject zero for the --http-outgoing-body-* options (#14448)

Xia Chaobe0feb356d13
diff --git a/crates/cli-flags/src/lib.rs b/crates/cli-flags/src/lib.rs
index 562f948e01..4542f65703 100644
--- a/crates/cli-flags/src/lib.rs
+++ b/crates/cli-flags/src/lib.rs
@@ -562,10 +562,10 @@ wasmtime_option_group! {
/// Number of distinct write calls to the outgoing body's output-stream
/// that the implementation will buffer.
/// Default: 1.
- pub http_outgoing_body_buffer_chunks: Option<usize>,
+ pub http_outgoing_body_buffer_chunks: Option<NonZeroUsize>,
/// Maximum size allowed in a write call to the outgoing body's output-stream.
/// Default: 1024 * 1024.
- pub http_outgoing_body_chunk_size: Option<usize>,
+ pub http_outgoing_body_chunk_size: Option<NonZeroUsize>,
/// Enable support for WASI config imports (experimental)
pub config: Option<bool>,
/// Enable support for WASI key-value imports (experimental)
diff --git a/crates/cli-flags/src/opt.rs b/crates/cli-flags/src/opt.rs
index a472062837..bfebb1beaf 100644
--- a/crates/cli-flags/src/opt.rs
+++ b/crates/cli-flags/src/opt.rs
@@ -13,7 +13,7 @@ use clap::error::{Error, ErrorKind};
#[cfg(feature = "serde")]
use serde::de::{self, Visitor};
use std::fmt;
-use std::num::NonZeroU32;
+use std::num::{NonZeroU32, NonZeroUsize};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::time::Duration;
@@ -447,6 +447,19 @@ impl WasmtimeOptionValue for NonZeroU32 {
}
}
+impl WasmtimeOptionValue for NonZeroUsize {
+ const VAL_HELP: &'static str = "=N";
+
+ fn parse(val: Option<&str>) -> Result<Self> {
+ let n = <usize as WasmtimeOptionValue>::parse(val)?;
+ NonZeroUsize::new(n).ok_or_else(|| format_err!("value must be non-zero"))
+ }
+
+ fn display(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "{self}")
+ }
+}
+
impl WasmtimeOptionValue for u64 {
const VAL_HELP: &'static str = "=N";
fn parse(val: Option<&str>) -> Result<Self> {
diff --git a/src/common.rs b/src/common.rs
index 958ea21ab1..7433b14db9 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -399,12 +399,14 @@ impl RunCommon {
.common
.wasi
.http_outgoing_body_buffer_chunks
- .unwrap_or_else(|| wasmtime_wasi_http::p2::DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS),
+ .map(|n| n.get())
+ .unwrap_or(wasmtime_wasi_http::p2::DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS),
p2_outgoing_body_chunk_size: self
.common
.wasi
.http_outgoing_body_chunk_size
- .unwrap_or_else(|| wasmtime_wasi_http::p2::DEFAULT_OUTGOING_BODY_CHUNK_SIZE),
+ .map(|n| n.get())
+ .unwrap_or(wasmtime_wasi_http::p2::DEFAULT_OUTGOING_BODY_CHUNK_SIZE),
}
}
diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rs
index 92de02f629..bb7d4c23e2 100644
--- a/tests/all/cli_tests.rs
+++ b/tests/all/cli_tests.rs
@@ -1782,6 +1782,31 @@ mod test_programs {
Ok(())
}
+ #[test]
+ fn p2_cli_reject_zero_outgoing_body_options() -> Result<()> {
+ // The outgoing body writer requires at least one buffered chunk and a
+ // non-zero write budget, so a zero here is a configuration error.
+ for option in [
+ "-Shttp-outgoing-body-buffer-chunks=0",
+ "-Shttp-outgoing-body-chunk-size=0",
+ ] {
+ let err = run_wasmtime(&[
+ "run",
+ "-Shttp=y",
+ option,
+ P2_CLI_RUN_OUTGOING_BODY_CHUNK_SIZE_COMPONENT,
+ ])
+ .unwrap_err();
+ let err = format!("{err:?}");
+ assert!(
+ err.contains("value must be non-zero"),
+ "expected `{option}` to be rejected, got: {err}"
+ );
+ }
+
+ Ok(())
+ }
+
#[test]
fn p2_cli_run_outgoing_body_chunk_size() -> Result<()> {
// Without the option an outgoing body keeps its 1 MiB default budget.