Skip to content

Commit

Permalink
Fix error check in nvlist_print_json_string
Browse files Browse the repository at this point in the history
Move check for errors from mbrtowc() into the loop.  The error values
are not actually negative, so we don't break out of the loop when they
are encountered.

Reviewed-by: Tony Nguyen <tony.nguyen@delphix.com>
Reviewed-by: John Kennedy <john.kennedy@delphix.com>
Signed-off-by: Ryan Moeller <ryan@iXsystems.com>
Closes openzfs#12175
Closes openzfs#12176
  • Loading branch information
Ryan Moeller authored and behlendorf committed Jun 8, 2021
1 parent 05dc937 commit bd806f8
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/libnvpair/libnvpair_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ nvlist_print_json_string(FILE *fp, const char *input)

FPRINTF(fp, "\"");
while ((sz = mbrtowc(&c, input, MB_CUR_MAX, &mbr)) > 0) {
if (sz == (size_t)-1 || sz == (size_t)-2) {
/*
* We last read an invalid multibyte character sequence,
* so return an error.
*/
return (-1);
}
switch (c) {
case '"':
FPRINTF(fp, "\\\"");
Expand Down Expand Up @@ -97,14 +104,6 @@ nvlist_print_json_string(FILE *fp, const char *input)
input += sz;
}

if (sz == (size_t)-1 || sz == (size_t)-2) {
/*
* We last read an invalid multibyte character sequence,
* so return an error.
*/
return (-1);
}

FPRINTF(fp, "\"");
return (0);
}
Expand Down

0 comments on commit bd806f8

Please sign in to comment.