diff mbox series

[2/2] BUG: JSON parser can't handle a string "offset"

Message ID 20231009152836.416267-2-stefano.babic@swupdate.org
State Accepted
Delegated to: Stefano Babic
Headers show
Series [1/2] parselib: function to check if a field is numeric | expand

Commit Message

Stefano Babic Oct. 9, 2023, 3:28 p.m. UTC
get_field(p, elem, "offset", &offset) in case of JSON does not return 0
if field is a string, causing a wrong offset to be evaluated. This bug
does not happen if libconfig is the parser.

Call is_field_numeric() in advance to check if offset is set as number
or string.

Signed-off-by: Stefano Babic <stefano.babic@swupdate.org>
Reported-by: Einar Jón <tolvupostur@gmail.com>
---
 include/parselib.h | 2 ++
 parser/parser.c    | 8 ++++----
 2 files changed, 6 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/include/parselib.h b/include/parselib.h
index 387ddc67..a027d53a 100644
--- a/include/parselib.h
+++ b/include/parselib.h
@@ -54,6 +54,7 @@  void *get_node_libconfig(config_t *cfg, const char **nodes);
 #define get_field_cfg(e, path, dest)
 #define find_root_libconfig(cfg, nodes, depth)		(NULL)
 #define get_node_libconfig(cfg, nodes)		(NULL)
+#define is_field_numeric_cfg(e, path)	(false)
 #endif
 
 #ifdef CONFIG_JSON
@@ -85,6 +86,7 @@  void *get_node_json(json_object *root, const char **nodes);
 #define json_object_array_length(a)	(0)
 #define find_root_json(root, nodes, depth)	(NULL)
 #define get_node_json(root, nodes)	(NULL)
+#define is_field_numeric_json(e, path)	(false)
 #endif
 
 bool is_field_numeric(parsertype p, void *e, const char *path);
diff --git a/parser/parser.c b/parser/parser.c
index 4c0a8f2f..52661384 100644
--- a/parser/parser.c
+++ b/parser/parser.c
@@ -398,8 +398,6 @@  static int parse_common_attributes(parsertype p, void *elem, struct img_type *im
 	GET_FIELD_STRING(p, elem, "mtdname", image->mtdname);
 	GET_FIELD_STRING(p, elem, "filesystem", image->filesystem);
 	GET_FIELD_STRING(p, elem, "type", image->type);
-	get_field(p, elem, "offset", &offset);
-	GET_FIELD_STRING(p, elem, "offset", seek_str);
 	GET_FIELD_STRING(p, elem, "data", image->type_data);
 	get_hash_value(p, elem, image->sha256);
 
@@ -407,9 +405,11 @@  static int parse_common_attributes(parsertype p, void *elem, struct img_type *im
 	 * offset can be set as number or string. As string,
 	 * multiplier suffixes are allowed
 	 */
-	if (offset)
+	if (is_field_numeric(p, elem, "offset")) {
+		get_field(p, elem, "offset", &offset);
 		image->seek = offset;
-	else {
+	} else {
+		GET_FIELD_STRING(p, elem, "offset", seek_str);
 		/* convert the offset handling multiplicative suffixes */
 		image->seek = ustrtoull(seek_str, NULL, 0);
 		if (errno){