Skip to content

Commit

Permalink
Merge #1688
Browse files Browse the repository at this point in the history
1688: Fix sentinel value in wasm_limits_t for memory in wasm_c_api r=MarkMcCaskey a=MarkMcCaskey

FIxes issue mentioned in #1631 (comment)


Co-authored-by: Mark McCaskey <mark@wasmer.io>
  • Loading branch information
bors[bot] and Mark McCaskey authored Oct 7, 2020
2 parents aaf90cd + f7d5a11 commit 70baded
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/c-api/src/wasm_c_api/types/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub struct wasm_limits_t {
#[no_mangle]
pub unsafe extern "C" fn wasm_memorytype_new(limits: &wasm_limits_t) -> Box<wasm_memorytype_t> {
let min_pages = Pages(limits.min as _);
// TODO: investigate if `0` is in fact a sentinel value here
let max_pages = if limits.max == 0 {
// u32::max_value() is a sentinel value for no max specified
let max_pages = if limits.max == u32::max_value() {
None
} else {
Some(Pages(limits.max as _))
Expand Down
20 changes: 18 additions & 2 deletions lib/c-api/tests/wasm_c_api/test-memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@ int main(int argc, const char *argv[]) {
own wasm_engine_t* engine = wasm_engine_new();
own wasm_store_t* store = wasm_store_new(engine);

// =====================
wasm_limits_t limits1 = {
.min = 0,
.max = wasm_limits_max_default,
.max = 0x7FFFFFFF,
};
own wasm_memorytype_t* memtype1 = wasm_memorytype_new(&limits1);
own wasm_memory_t* memory1 = wasm_memory_new(store, memtype1);
assert(memory1 == NULL);
char* error = get_wasmer_error();
printf("Found error string: %s\n", error);
assert(0 == strcmp("The maximum requested memory (4294967295 pages) is greater than the maximum allowed memory (65536 pages)", error));
assert(0 == strcmp("The maximum requested memory (2147483647 pages) is greater than the maximum allowed memory (65536 pages)", error));
free(error);

wasm_memorytype_delete(memtype1);

// =====================
wasm_limits_t limits2 = {
.min = 15,
.max = 25,
Expand All @@ -46,6 +48,20 @@ int main(int argc, const char *argv[]) {
wasm_memorytype_delete(memtype2);
wasm_memory_delete(memory2);

// =====================
wasm_limits_t limits3 = {
.min = 15,
.max = wasm_limits_max_default,
};
own wasm_memorytype_t* memtype3 = wasm_memorytype_new(&limits3);
own wasm_memory_t* memory3 = wasm_memory_new(store, memtype3);
assert(memory3 != NULL);
int size = wasm_memory_size(memory3);
printf("memory size: %d\n", size);

wasm_memorytype_delete(memtype3);
wasm_memory_delete(memory3);

printf("Shutting down...\n");
wasm_store_delete(store);
wasm_engine_delete(engine);
Expand Down

0 comments on commit 70baded

Please sign in to comment.