lib: json: Alignment should be considered when calculating struct size
This was causing an unaligned pointer read on some architectures,
leading to crashes. This could be alternatively solved by rounding
the size to the nearest power of 2, but this wouldn't work with
packed structs.
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
diff --git a/lib/json/json.c b/lib/json/json.c
index 411a708..576e55f 100644
--- a/lib/json/json.c
+++ b/lib/json/json.c
@@ -9,10 +9,11 @@
#include <errno.h>
#include <limits.h>
#include <misc/printk.h>
+#include <misc/util.h>
#include <stdbool.h>
-#include <zephyr/types.h>
#include <stdlib.h>
#include <string.h>
+#include <zephyr/types.h>
#include "json.h"
@@ -475,6 +476,8 @@
static ptrdiff_t get_elem_size(const struct json_obj_descr *descr)
{
+ assert(descr->alignment);
+
switch (descr->type) {
case JSON_TOK_NUMBER:
return sizeof(s32_t);
@@ -489,8 +492,10 @@
ptrdiff_t total = 0;
size_t i;
- for (i = 0; i < descr->sub_descr_len; i++) {
- total += get_elem_size(&descr->object.sub_descr[i]);
+ for (i = 0; i < descr->object.sub_descr_len; i++) {
+ ptrdiff_t s = get_elem_size(&descr->object.sub_descr[i]);
+
+ total += ROUND_UP(s, descr->alignment);
}
return total;