Qrzysztof commited on
Commit
0ad0a6e
·
verified ·
1 Parent(s): 6a8f747

fix output read: scan for NUL terminator instead of using rc as length

Browse files
Files changed (1) hide show
  1. index.html +5 -2
index.html CHANGED
@@ -75,8 +75,11 @@ async function run() {
75
  const qP = enc(q);
76
  const cap = 1 << 20, outP = mod._malloc(cap);
77
  const rc = mod._needle_complete(qP, 512, outP, cap);
78
- const bytes = mod.HEAPU8.subarray(outP, outP + Math.min(rc, cap));
79
- let end = bytes.indexOf(0); if (end < 0) end = bytes.length;
 
 
 
80
  const response = new TextDecoder().decode(bytes.subarray(0, end));
81
 
82
  out.textContent = response;
 
75
  const qP = enc(q);
76
  const cap = 1 << 20, outP = mod._malloc(cap);
77
  const rc = mod._needle_complete(qP, 512, outP, cap);
78
+ if (rc < 0) throw new Error("needle_complete returned " + rc);
79
+ // rc is NOT the output length: the engine writes a NUL-terminated string,
80
+ // so scan the whole buffer for the first NUL terminator.
81
+ const bytes = mod.HEAPU8.subarray(outP, outP + cap);
82
+ let end = bytes.indexOf(0); if (end < 0) end = cap;
83
  const response = new TextDecoder().decode(bytes.subarray(0, end));
84
 
85
  out.textContent = response;