Skip to content

Commit

Permalink
Add extra sanity checks to jstrencode/jstrdecode
Browse files Browse the repository at this point in the history
The function free_jstring_list() now takes a 'struct jstring
**jstring_list' and if not NULL it will set *jstring_list to NULL, after
freeing the list, in case the caller does something silly. Even so, the
two tools now set the list to NULL after calling the free function.
  • Loading branch information
xexyl committed Nov 1, 2024
1 parent 3588744 commit d511af7
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ generated code, perhaps in a torture chamber or something like that, or for
those who want hallucinations or nightmares :-) or simply those who are really
curious what flex does.

Add extra sanity checks to `jstrencode(1)` and `jstrdecode(1)` when freeing the
lists. The function `free_jstring_list()` now takes a `struct jstring
**jstring_list` and if not NULL it will set `*jstring_list` to NULL, after
freeing the list, in case the caller does something silly. Even so, the two
tools now set the list to NULL after calling the free function.


## Release 2.0.0 2024-10-31

Expand Down
15 changes: 11 additions & 4 deletions jstr_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,28 +155,35 @@ free_jstring(struct jstring **jstr)
*
* given:
*
* jstring_list pointer to list to free
* jstring_list pointer to pointer to list to free
*
* This function takes a struct jstring *, a pointer to a linked list of struct
* This function takes a struct jstring **, a pointer to a linked list of struct
* jstring *.
*
* NOTE: it is ASSUMED that the string in each struct jstring * is allocated on
* the stack due to how the encoding/decoding works. If this is not the case
* then expect errors.
*
* NOTE: if jstring_list is NULL then nothing is done.
*/
void
free_jstring_list(struct jstring *jstring_list)
free_jstring_list(struct jstring **jstring_list)
{
struct jstring *jstr = NULL; /* current in list */
struct jstring *jstr_next = NULL; /* next in list */

for (jstr = jstring_list; jstr != NULL; jstr = jstr_next) {

for (jstr = jstring_list != NULL ? *jstring_list : NULL; jstr != NULL; jstr = jstr_next) {
jstr_next = jstr->next; /* get next in list before we free the current */

/* free current json string */
free_jstring(&jstr);
jstr = NULL;
}

if (jstring_list != NULL) {
*jstring_list = NULL;
}

jstring_list = NULL;
}
2 changes: 1 addition & 1 deletion jstr_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ struct jstring
extern struct jstring *alloc_jstr(char *string, size_t bufsiz);
extern int parse_entertainment(char const *optarg);
extern void free_jstring(struct jstring **jstr);
extern void free_jstring_list(struct jstring *jstring_list);
extern void free_jstring_list(struct jstring **jstring_list);

#endif /* INCLUDE_JSTR_UTIL_H */
3 changes: 2 additions & 1 deletion jstrdecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,8 @@ main(int argc, char **argv)
/*
* free list of decoded strings
*/
free_jstring_list(json_decoded_strings);
free_jstring_list(&json_decoded_strings);
json_decoded_strings = NULL;

/*
* All Done!!! All Done!!! -- Jessica Noll, Age 2
Expand Down
2 changes: 1 addition & 1 deletion jstrdecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
/*
* official jstrdecode version
*/
#define JSTRDECODE_VERSION "2.0.0 2024-10-31" /* format: major.minor YYYY-MM-DD */
#define JSTRDECODE_VERSION "2.0.1 2024-11-01" /* format: major.minor YYYY-MM-DD */


/*
Expand Down
3 changes: 2 additions & 1 deletion jstrencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,8 @@ main(int argc, char **argv)
/*
* free list of encoded strings
*/
free_jstring_list(json_encoded_strings);
free_jstring_list(&json_encoded_strings);
json_encoded_strings = NULL;

/*
* All Done!!! All Done!!! -- Jessica Noll, Age 2
Expand Down
2 changes: 1 addition & 1 deletion jstrencode.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
/*
* official jstrencode version
*/
#define JSTRENCODE_VERSION "2.0.0 2024-10-31" /* format: major.minor YYYY-MM-DD */
#define JSTRENCODE_VERSION "2.0.1 2024-11-01" /* format: major.minor YYYY-MM-DD */


/*
Expand Down

0 comments on commit d511af7

Please sign in to comment.