aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Tatham <anakin@pobox.com>2018-06-21 18:54:08 +0100
committerSimon Tatham <anakin@pobox.com>2018-06-21 18:54:08 +0100
commit3618f6a07f71336ad5741836afb6aeb6cae3ff99 (patch)
tree4ecd6edb0ba939e6acdbf434b84b86c022088e38
parent506b07352ae225c74e79f6c6e7872bc2005bdb53 (diff)
downloadpuzzles-3618f6a07f71336ad5741836afb6aeb6cae3ff99.zip
puzzles-3618f6a07f71336ad5741836afb6aeb6cae3ff99.tar.gz
puzzles-3618f6a07f71336ad5741836afb6aeb6cae3ff99.tar.bz2
puzzles-3618f6a07f71336ad5741836afb6aeb6cae3ff99.tar.xz
Fix NUL-termination bug in saving from Javascript.
The JS code that retrieves the save-file data from emcc.c doesn't receive a separate length value, but instead expects the data to be in the form of a NUL-terminated string. But emcc.c wasn't NUL-terminating it, so the save data could come out with random cruft on the end.
-rw-r--r--emcc.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/emcc.c b/emcc.c
index 563fbe2..1d16d20 100644
--- a/emcc.c
+++ b/emcc.c
@@ -806,11 +806,15 @@ char *get_save_file(void)
midend_serialise(me, savefile_write, &ctx);
size = ctx.pos;
- /* Second pass, to actually write out the data */
- ctx.buffer = snewn(size, char);
+ /* Second pass, to actually write out the data. We have to put a
+ * terminating \0 on the end (which we expect never to show up in
+ * the actual serialisation format - it's text, not binary) so
+ * that the Javascript side can easily find out the length. */
+ ctx.buffer = snewn(size+1, char);
ctx.pos = 0;
midend_serialise(me, savefile_write, &ctx);
assert(ctx.pos == size);
+ ctx.buffer[ctx.pos] = '\0';
return ctx.buffer;
}