Fix error check in nvlist_print_json_string

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 #12175
Closes #12176
This commit is contained in:
Ryan Moeller 2021-06-04 15:53:44 -04:00 committed by Brian Behlendorf
parent c650ceb64d
commit e298695809
1 changed files with 7 additions and 8 deletions

View File

@ -54,6 +54,13 @@ nvlist_print_json_string(FILE *fp, const char *input)
FPRINTF(fp, "\""); FPRINTF(fp, "\"");
while ((sz = mbrtowc(&c, input, MB_CUR_MAX, &mbr)) > 0) { 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) { switch (c) {
case '"': case '"':
FPRINTF(fp, "\\\""); FPRINTF(fp, "\\\"");
@ -97,14 +104,6 @@ nvlist_print_json_string(FILE *fp, const char *input)
input += sz; 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, "\""); FPRINTF(fp, "\"");
return (0); return (0);
} }