Skip to contentSkip to conversation

json: Update jsmn library to upstream commit 053d3cd

PATCH In review Patrick Steinhardt 2 participants 6 messages Apr 16, 2020 1 message not in the archive

lore
Share & tools
lore
#1 lore raw

PATCH 1/2 json: Update jsmn library to upstream commit 053d3cd

In reply to a message not in the archive

Update our embedded version of the jsmn library to upstream commit 053d3cd (Merge pull request #175 from pks-t/pks/struct-type, 2020-04-02).

---
 grub-core/lib/json/jsmn.h | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
grub-core/lib/json/jsmn.h +5 -2
index b95368a20..3178dcc97 100644
@@ -66,7 +66,7 @@ enum jsmnerr {
  * start	start position in JSON data string
  * end		end position in JSON data string
  */
-typedef struct {
+typedef struct jsmntok {
   jsmntype_t type;
   int start;
   int end;
@@ -80,7 +80,7 @@ typedef struct {
  * JSON parser. Contains an array of token blocks available. Also stores
  * the string being parsed now and current position in that string.
  */
-typedef struct {
+typedef struct jsmn_parser {
   unsigned int pos;     /* offset in the JSON string */
   unsigned int toknext; /* next token to allocate */
   int toksuper;         /* superior token node, e.g. parent object or array */
@@ -154,6 +154,9 @@ static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
     case ']':
     case '}':
       goto found;
+    default:
+                   /* to quiet a warning from gcc*/
+      break;
     }
     if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
       parser->pos = start;
Signature · 1 line
2.26.1
#5 lore raw

PATCH 2/2 json: Get rid of casts for `jsmntok_t`

In reply to a message not in the archive

With the upstream change having landed that adds a name to the previously anonymous jsmntok typedef, we can now add a forward declaration for that struct in our code. As a result, we no longer have to store the tokens member of struct grub_json as a void pointer but can instead use the forward declaration, allowing us to get rid of casts of that field.

---
 grub-core/lib/json/json.c | 8 ++++----
 grub-core/lib/json/json.h | 4 +++-
 2 files changed, 7 insertions(+), 5 deletions(-)
grub-core/lib/json/json.c +4 -4
index 15c0d9949..694af4f3a 100644
@@ -95,7 +95,7 @@ grub_json_getsize (grub_size_t *out, const grub_json_t *json)
 {
   int size;
 
-  size = ((jsmntok_t *)json->tokens)[json->idx].size;
+  size = json->tokens[json->idx].size;
   if (size < 0)
     return GRUB_ERR_OUT_OF_RANGE;
 
@@ -106,7 +106,7 @@ grub_json_getsize (grub_size_t *out, const grub_json_t *json)
 grub_err_t
 grub_json_gettype (grub_json_type_t *out, const grub_json_t *json)
 {
-  switch (((jsmntok_t *)json->tokens)[json->idx].type)
+  switch (json->tokens[json->idx].type)
     {
     case JSMN_OBJECT:
       *out = GRUB_JSON_OBJECT;
@@ -142,7 +142,7 @@ grub_json_getchild (grub_json_t *out, const grub_json_t *parent, grub_size_t n)
    * array), as well. We thus add the children's size to n on
    * each iteration.
    */
-  p = &((jsmntok_t *)parent->tokens)[parent->idx];
+  p = &parent->tokens[parent->idx];
   while (n--)
     n += p[offset++].size;
 
@@ -197,7 +197,7 @@ get_value (grub_json_type_t *out_type, const char **out_string, const grub_json_
       p = &child;
     }
 
-  tok = &((jsmntok_t *) p->tokens)[p->idx];
+  tok = &p->tokens[p->idx];
   p->string[tok->end] = '\0';
 
   *out_string = p->string + tok->start;
grub-core/lib/json/json.h +3 -1
index 358e4bca3..d9f99454d 100644
@@ -36,9 +36,11 @@ enum grub_json_type
 };
 typedef enum grub_json_type grub_json_type_t;
 
+typedef struct jsmntok jsmntok_t;
+
 struct grub_json
 {
-  void	      *tokens;
+  jsmntok_t   *tokens;
   char	      *string;
   grub_size_t idx;
 };
Signature · 1 line
2.26.1
#2 lore raw
In reply to Patrick Steinhardt
Quoted 3 lines · Patrick Steinhardt

Update our embedded version of the jsmn library to upstream commit 053d3cd (Merge pull request #175 from pks-t/pks/struct-type, 2020-04-02).

Does this commit contain all changes listed below? No more no less? If yes Reviewed-by: Daniel Kiper <[email protected]>

Daniel

Quoted 39 lines
---
 grub-core/lib/json/jsmn.h | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
grub-core/lib/json/jsmn.h +5 -3
index b95368a20..3178dcc97 100644
@@ -66,7 +66,7 @@ enum jsmnerr {
  * start	start position in JSON data string
  * end		end position in JSON data string
  */
-typedef struct {
+typedef struct jsmntok {
   jsmntype_t type;
   int start;
   int end;
@@ -80,7 +80,7 @@ typedef struct {
  * JSON parser. Contains an array of token blocks available. Also stores
  * the string being parsed now and current position in that string.
  */
-typedef struct {
+typedef struct jsmn_parser {
   unsigned int pos;     /* offset in the JSON string */
   unsigned int toknext; /* next token to allocate */
   int toksuper;         /* superior token node, e.g. parent object or array */
@@ -154,6 +154,9 @@ static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
     case ']':
     case '}':
       goto found;
+    default:
+                   /* to quiet a warning from gcc*/
+      break;
     }
     if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
       parser->pos = start;
--

2.26.1

Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] json: Get rid of casts for `jsmntok_t`

In reply to Patrick Steinhardt
Quoted 8 lines · Patrick Steinhardt

With the upstream change having landed that adds a name to the previously anonymous jsmntok typedef, we can now add a forward declaration for that struct in our code. As a result, we no longer have to store the tokens member of struct grub_json as a void pointer but can instead use the forward declaration, allowing us to get rid of casts of that field.

Daniel

#3 lore raw
In reply to Daniel Kiper
Quoted 9 lines · Daniel Kiper
Quoted 3 lines · Patrick Steinhardt

Update our embedded version of the jsmn library to upstream commit 053d3cd (Merge pull request #175 from pks-t/pks/struct-type, 2020-04-02).

Does this commit contain all changes listed below? No more no less? If yes Reviewed-by: Daniel Kiper <[email protected]>

Daniel

Yup. I've just copied "jsmn.h" from upstream commit 053d3cd verbosely.

Patrick

#4 lore raw
Quoted 6 lines · Patrick Steinhardt
Quoted 5 lines · Daniel Kiper
Quoted 3 lines · Patrick Steinhardt

Update our embedded version of the jsmn library to upstream commit 053d3cd (Merge pull request #175 from pks-t/pks/struct-type, 2020-04-02).

Quoted 6 lines
Quoted 4 lines

Does this commit contain all changes listed below? No more no less? If yes Reviewed-by: Daniel Kiper <[email protected]>

Daniel

Yup. I've just copied "jsmn.h" from upstream commit 053d3cd verbosely.

Great! So, if there are no objections I will take this patch next week.

Thanks,

Daniel