Skip to content

Commit

Permalink
Update fromCString to avoid allocation errors (#54)
Browse files Browse the repository at this point in the history
This commit reverts the implementation of `fromCString` to the one
before revision 22b5230 in order to
avoid allocation errors when getting environment variables and command
line arguments.

The underlying issue with its implementation seems to be related to
passing the maximum integer value instead of the actual size, resulting
in an allocation overflow at runtime.

Another potential solution for this could be using the safe mechanisms
provided by AssemblyScript (`changetype` and `decode`, but there seem
to be some intermittent GC-related issues when using them, and this
requires some more investigation).

Signed-off-by: Radu M <[email protected]>
Co-authored-by: Matt Butcher <[email protected]>

Co-authored-by: Matt Butcher <[email protected]>
  • Loading branch information
radu-matei and technosophos authored Jul 17, 2020
1 parent ab9700c commit 4d63b96
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion assembly/as-wasi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,11 @@ class StringUtils {
*/
@inline
static fromCString(cstring: usize): string {
return String.UTF8.decodeUnsafe(cstring, i32.MAX_VALUE, true);
let size = 0;
while (load<u8>(cstring + size) !== 0) {
size++;
}
return String.UTF8.decodeUnsafe(cstring, size);
}
}

Expand Down

0 comments on commit 4d63b96

Please sign in to comment.