Skip to content

Commit

Permalink
Merge pull request #788 from xexyl/jnamval-jval-num-error
Browse files Browse the repository at this point in the history
Make jval/jnamval exit 7 on number conv error
  • Loading branch information
lcn2 authored Jul 22, 2023
2 parents 717c72e + 76c774f commit e22cdf1
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 22 deletions.
19 changes: 19 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# Major changes to the IOCCC entry toolkit

## Release 1.0.36 2023-07-21

Fix bug in `vjson_fprint()` where numbers that were converted and parsed were
not printed as the check accidentally was using the wrong macro that checks if
converted is true and parsed is false. It should be it checks for both are true
and then the next check in the else if checks if parsed is true and converted is
false.

Move `converted` bool in `struct json_` structs below the `parsed` bool.

Make `jval` and `jnamval` exit with 7 if a number cannot be represented by a C
type. Updated man pages and usage messages. For `jnamval` this means what used
to be 7 is now 8: no matches found.

For number conversions in `jval` and `jnamval` use the macros to check for
converted/parsed booleans.



## Release 1.0.35 2023-07-19

Add initial version of man pages of `jfmt(1)`, `jval(1)` and `jnamval(1)` to
Expand Down
3 changes: 2 additions & 1 deletion jparse/jnamval.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ static const char * const usage_msg1 =
" 4\tfile does not exist, not a file, or unable to read the file\n"
" 5\tfile contents is not valid JSON\n"
" 6\ttest mode failed\n"
" 7\tno matches found\n"
" 7\tunable to represent a number\n"
" 8\tno matches found\n"
" >=10\tinternal error\n"
"\n"
"JSON parser version: %s\n"
Expand Down
10 changes: 7 additions & 3 deletions jparse/jnamval_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ jnamval_parse_cmp_op(struct jnamval *jnamval, const char *option, char *optarg,
if (cmp->string == NULL) {
err(38, __func__, "failed to convert string: <%s> for -%s: cmp->string is NULL", optarg + 3, option);
not_reached();
} else if (!cmp->string->converted && !cmp->string->parsed) {
} else if (!CONVERTED_PARSED_JSON_NODE(cmp->string)) {
err(39, __func__, "failed to convert or parse string: <%s> for option -%s but string pointer not NULL!",
optarg + 3, option);
not_reached();
Expand All @@ -1223,11 +1223,15 @@ jnamval_parse_cmp_op(struct jnamval *jnamval, const char *option, char *optarg,
not_reached();
} else {
cmp->number = &(item->item.number);
if (!cmp->number->converted && !cmp->number->parsed) {
err(41, __func__, "failed to convert or parse number: <%s> for option -%s but number pointer not NULL!",
if (!CONVERTED_PARSED_JSON_NODE(cmp->number)) {
err(7, __func__, "failed to convert or parse number: <%s> for option -%s but number pointer not NULL!",/*ooo*/
optarg + 3, option);
not_reached();
} else if (PARSED_JSON_NODE(cmp->number)) {
err(7, __func__, "failed to convert number: <%s> for option -%s", optarg +3 , option); /*ooo*/
not_reached();
}

/* TODO - add debug call if converted / parsed ? - TODO */
}
}
Expand Down
4 changes: 2 additions & 2 deletions jparse/jparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
/*
* official jparse version
*/
#define JPARSE_VERSION "1.1.0 2023-07-15" /* format: major.minor YYYY-MM-DD */
#define JPARSE_VERSION "1.1.1 2023-07-21" /* format: major.minor YYYY-MM-DD */

/*
* definitions
Expand All @@ -77,7 +77,7 @@
/*
* official JSON parser version
*/
#define JSON_PARSER_VERSION "1.1.0 2023-07-15" /* library version format: major.minor YYYY-MM-DD */
#define JSON_PARSER_VERSION "1.1.0 2023-07-21" /* library version format: major.minor YYYY-MM-DD */


/*
Expand Down
16 changes: 8 additions & 8 deletions jparse/json_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ struct encode
*/
struct json_number
{
bool converted; /* true ==> able to convert JSON number string to some form of C value */
bool parsed; /* true ==> able to parse correctly */
bool converted; /* true ==> able to convert JSON number string to some form of C value */

char *as_str; /* allocated copy of the original allocated JSON number, NUL terminated */
char *first; /* first whitespace character */
Expand Down Expand Up @@ -212,8 +212,8 @@ struct json_number
*/
struct json_string
{
bool converted; /* true ==> able to decode JSON string, false ==> str is invalid or not decoded */
bool parsed; /* true ==> able to parse correctly */
bool converted; /* true ==> able to decode JSON string, false ==> str is invalid or not decoded */

char *as_str; /* allocated non-decoded JSON string, NUL terminated (perhaps sans JSON '"'s) */
char *str; /* allocated decoded JSON string, NUL terminated */
Expand Down Expand Up @@ -246,8 +246,8 @@ struct json_string
*/
struct json_boolean
{
bool converted; /* true ==> able to decode JSON boolean, false ==> as_str is invalid or not decoded */
bool parsed; /* true ==> able to parse correctly */
bool converted; /* true ==> able to decode JSON boolean, false ==> as_str is invalid or not decoded */

char *as_str; /* allocated JSON boolean string, NUL terminated */
size_t as_str_len; /* length of as_str */
Expand All @@ -268,8 +268,8 @@ struct json_boolean
*/
struct json_null
{
bool converted; /* true ==> able to decode JSON null, false ==> as_str is invalid or not decoded */
bool parsed; /* true ==> able to parse correctly */
bool converted; /* true ==> able to decode JSON null, false ==> as_str is invalid or not decoded */

char *as_str; /* allocated JSON null string, NUL terminated */
size_t as_str_len; /* length of as_str */
Expand Down Expand Up @@ -311,8 +311,8 @@ struct json_null
*/
struct json_member
{
bool converted; /* true ==> able to decode JSON member */
bool parsed; /* true ==> able to parse correctly */
bool converted; /* true ==> able to decode JSON member */

char *name_as_str; /* name string as non-decoded JSON string - will not be NULL */
char *name_str; /* name string as decoded JSON string - will not be NULL */
Expand Down Expand Up @@ -342,8 +342,8 @@ struct json_member
*/
struct json_object
{
bool converted; /* true ==> able to decode JSON object */
bool parsed; /* true ==> able to parse correctly */
bool converted; /* true ==> able to decode JSON object */

intmax_t len; /* number of JSON members in the object, 0 ==> empty object */
struct json **set; /* set of JSON members belonging to the object */
Expand Down Expand Up @@ -372,8 +372,8 @@ struct json_object
*/
struct json_array
{
bool converted; /* true ==> able to decode JSON array */
bool parsed; /* true ==> able to parse correctly */
bool converted; /* true ==> able to decode JSON array */

intmax_t len; /* number of JSON values in the JSON array, 0 ==> empty array */
struct json **set; /* set of JSON values belonging to the JSON array */
Expand All @@ -399,8 +399,8 @@ struct json_array
*/
struct json_elements
{
bool converted; /* true ==> able to decode JSON array */
bool parsed; /* true ==> able to parse correctly */
bool converted; /* true ==> able to decode JSON array */

intmax_t len; /* number of JSON values in the JSON elements, 0 ==> empty array */
struct json **set; /* set of JSON values belonging to the JSON elements */
Expand Down
2 changes: 1 addition & 1 deletion jparse/json_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,7 @@ vjson_fprint(struct json *node, unsigned int depth, va_list ap)
/*
* case: converted number
*/
if (CONVERTED_JSON_NODE(item)) {
if (CONVERTED_PARSED_JSON_NODE(item)) {

/*
* case: converted negative number
Expand Down
2 changes: 1 addition & 1 deletion jparse/jval.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ static const char * const usage_msg0 =
"\t\t\tIf lvl is a number followed by : (e.g. '-l 3:'), level must be >= number.\n"
"\t\t\tIf lvl is a : followed by a number (e.g. '-l :3'), level must be <= number.\n"
"\t\t\tIf lvl is num:num (e.g. '-l 3:5'), level must be inclusively in the range.\n"

"\t-Q\t\tPrint JSON strings surrounded by double quotes (def: do not)\n"
"\t-D\t\tPrint JSON strings as decoded strings (def: print JSON strings as encoded strings)\n"
"\t-d\t\tMatch the JSON decoded values (def: match as given in the JSON document)\n"
Expand Down Expand Up @@ -121,6 +120,7 @@ static const char * const usage_msg0 =
" 4\tfile does not exist, not a file, or unable to read the file\n"
" 5\tfile contents is not valid JSON\n"
" 6\ttest mode failed\n"
" 7\tunable to represent a number\n"
" >=10\tinternal error\n"
"\n"
"JSON parser version: %s\n"
Expand Down
11 changes: 7 additions & 4 deletions jparse/jval_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ jval_parse_cmp_op(struct jval *jval, const char *option, char *optarg, struct jv
if (cmp->string == NULL) {
err(37, __func__, "failed to convert string: <%s> for -%s: cmp->string is NULL", optarg + 3, option);
not_reached();
} else if (!cmp->string->converted && !cmp->string->parsed) {
} else if (!CONVERTED_PARSED_JSON_NODE(cmp->string)) {
err(38, __func__, "failed to convert or parse string: <%s> for option -%s but string pointer not NULL!",
optarg + 3, option);
not_reached();
Expand All @@ -908,12 +908,15 @@ jval_parse_cmp_op(struct jval *jval, const char *option, char *optarg, struct jv
not_reached();
} else {
cmp->number = &(item->item.number);
if (!cmp->number->converted && !cmp->number->parsed) {
err(40, __func__, "failed to convert or parse number: <%s> for option -%s but number pointer not NULL!",
if (!CONVERTED_PARSED_JSON_NODE(cmp->number)) {
err(7, __func__, "failed to convert or parse number: <%s> for option -%s but number pointer not NULL!",/*ooo*/
optarg + 3, option);
not_reached();
} else if (PARSED_JSON_NODE(cmp->number)) {
err(7, __func__, "failed to convert number: <%s> for option -%s", optarg +3 , option); /*ooo*/
not_reached();
}
/* TODO - add debug call if converted / parsed ? - TODO */
json_dbg(JSON_DBG_NONE, __func__, "number to compare: <%s>", cmp->number->as_str);
}
}
}
5 changes: 4 additions & 1 deletion jparse/man/man1/jnamval.1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
.\" "Share and Enjoy!"
.\" -- Sirius Cybernetics Corporation Complaints Division, JSON spec department. :-)
.\"
.TH jnamval 1 "19 July 2023" "jnamval" "IOCCC tools"
.TH jnamval 1 "21 July 2023" "jnamval" "IOCCC tools"
.SH NAME
.B jnamval
\- IOCCC JSON printer
Expand Down Expand Up @@ -392,6 +392,9 @@ file contents is not valid JSON
test mode failed
.TQ
7
unable to represent a number
.TQ
8
no matches found
.TQ
>=10
Expand Down
5 changes: 4 additions & 1 deletion jparse/man/man1/jval.1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
.\" "Share and Enjoy!"
.\" -- Sirius Cybernetics Corporation Complaints Division, JSON spec department. :-)
.\"
.TH jval 1 "19 July 2023" "jval" "IOCCC tools"
.TH jval 1 "21 July 2023" "jval" "IOCCC tools"
.SH NAME
.B jval
\- IOCCC JSON printer
Expand Down Expand Up @@ -324,6 +324,9 @@ file contents is not valid JSON
6
test mode failed
.TQ
7
unable to represent a number
.TQ
>=10
internal error
.SH NOTES
Expand Down

0 comments on commit e22cdf1

Please sign in to comment.