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>
This commit is contained in:
Leandro Pereira 2017-09-05 16:32:27 -07:00 committed by Anas Nashif
commit 7a72ecd314
2 changed files with 21 additions and 3 deletions

View file

@ -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 int decode_value(struct json_obj *obj,
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 @@ static ptrdiff_t get_elem_size(const struct json_obj_descr *descr)
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;