From f561d16334862bead540f993e172777586cba435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Wed, 10 Jan 2018 20:45:45 +0100 Subject: [PATCH] fix param_export: off-by-one buffer size In rare circumstances, the buffer size was too small by 1 byte, leading to a param export failure. And leading to a reset of all params! This could only happen when the last saved param was a float. Also the realloc_ok initialization is not needed. --- src/modules/systemlib/param/param.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/modules/systemlib/param/param.c b/src/modules/systemlib/param/param.c index dff0bc74e0..2a1bb5b51d 100644 --- a/src/modules/systemlib/param/param.c +++ b/src/modules/systemlib/param/param.c @@ -1035,7 +1035,6 @@ param_export(int fd, bool only_unsaved) int result = -1; struct bson_encoder_s encoder; - encoder.realloc_ok = 0; int shutdown_lock_ret = px4_shutdown_lock(); @@ -1072,9 +1071,9 @@ param_export(int fd, bool only_unsaved) const size_t size = param_size(s->param); // check remaining buffer size and commit to disk - // total size = name + param size + bson header + bson end + // total size = strlen(name) + 1 (null char) + param size + 1 (bson header) + 1 (bson end) // size is doubled (floats saved as doubles) - const size_t total_size = strlen(name) + 2 * size + 2; + const size_t total_size = strlen(name) + 2 * size + 3; if (encoder.bufpos > encoder.bufsize - total_size) { // write buffer to disk and continue