Skip to content

Commit e048db7

Browse files
authored
Bail when doing wit_component::decode on a component (#1473)
* Bail when doing wit-component::decode on a component Signed-off-by: Ryan Levick <ryan.levick@fermyon.com> * Add test for component-new-existing-component Signed-off-by: Ryan Levick <ryan.levick@fermyon.com> --------- Signed-off-by: Ryan Levick <ryan.levick@fermyon.com>
1 parent 8b11dbd commit e048db7

3 files changed

Lines changed: 9 additions & 1 deletion

File tree

crates/wit-component/src/metadata.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use wasm_encoder::{
5050
ComponentBuilder, ComponentExportKind, ComponentType, ComponentTypeRef, CustomSection,
5151
};
5252
use wasm_metadata::Producers;
53-
use wasmparser::{BinaryReader, Parser, Payload};
53+
use wasmparser::{BinaryReader, Encoding, Parser, Payload};
5454
use wit_parser::{Package, PackageName, Resolve, World, WorldId, WorldItem};
5555

5656
const CURRENT_VERSION: u8 = 0x04;
@@ -127,6 +127,8 @@ pub struct ModuleMetadata {
127127
/// The extraction here provides the metadata necessary to continue the process
128128
/// later on.
129129
///
130+
/// This will return an error if `wasm` is not a valid WebAssembly module.
131+
///
130132
/// Note that a "stripped" binary where `component-type` sections are removed
131133
/// is returned as well to embed within a component.
132134
pub fn decode(wasm: &[u8]) -> Result<(Vec<u8>, Bindgen)> {
@@ -142,6 +144,9 @@ pub fn decode(wasm: &[u8]) -> Result<(Vec<u8>, Bindgen)> {
142144
ret.merge(data)
143145
.with_context(|| format!("updating metadata for section {}", cs.name()))?;
144146
}
147+
wasmparser::Payload::Version { encoding, .. } if encoding != Encoding::Module => {
148+
bail!("decoding a component is not supported")
149+
}
145150
_ => {
146151
if let Some((id, range)) = payload.as_section() {
147152
new_module.section(&wasm_encoder::RawSection {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
;; FAIL: component new % -o component.wasm
2+
(component)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
error: decoding a component is not supported

0 commit comments

Comments
 (0)