aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-12-26 18:56:34 +0100
committerbptato <nincsnevem662@gmail.com>2024-12-26 18:56:34 +0100
commitf2d3040da5c686fd4a3a78dbcca68b75512ea28e (patch)
treef8647dc78da10db4fb2608140d94d0c1b0a72b77 /src
parent2471a1165a37ad2d25355f0f31b1830cabb26c35 (diff)
downloadchawan-f2d3040da5c686fd4a3a78dbcca68b75512ea28e.tar.gz
jsencoding: fix nil deref on empty encode input
Diffstat (limited to 'src')
-rw-r--r--src/html/jsencoding.nim18
1 files changed, 9 insertions, 9 deletions
diff --git a/src/html/jsencoding.nim b/src/html/jsencoding.nim
index 7aae5eb2..c552724f 100644
--- a/src/html/jsencoding.nim
+++ b/src/html/jsencoding.nim
@@ -74,19 +74,19 @@ func jencoding(this: JSTextEncoder): string {.jsfget: "encoding".} =
return "utf-8"
proc dealloc_wrap(rt: JSRuntime; opaque, p: pointer) {.cdecl.} =
- dealloc(p)
+ if p != nil:
+ dealloc(p)
proc encode(this: JSTextEncoder; input = ""): JSUint8Array {.jsfunc.} =
# we have to validate input first :/
#TODO it is possible to do less copies here...
- var input = input.toValidUTF8()
- let buf = cast[ptr UncheckedArray[uint8]](alloc(input.len))
- copyMem(buf, addr input[0], input.len)
- let abuf = JSArrayBuffer(
- p: buf,
- len: csize_t(input.len),
- dealloc: dealloc_wrap
- )
+ let input = input.toValidUTF8()
+ let abuf = if input.len > 0:
+ let buf = cast[ptr UncheckedArray[uint8]](alloc(input.len))
+ copyMem(buf, unsafeAddr input[0], input.len)
+ JSArrayBuffer(p: buf, len: csize_t(input.len), dealloc: dealloc_wrap)
+ else:
+ JSArrayBuffer(p: nil, len: 0, dealloc: dealloc_wrap)
return JSUint8Array(
abuf: abuf,
offset: 0,