From 0e31aacccde14766a25f5ffb5bc5147fcabe1956 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Wed, 1 Jan 2025 12:45:19 +0000 Subject: [PATCH] pstring: Improve error checking in char_to_wchar. Make the existing error checking clearer and also check `errno` to handle another possible failure scenario. Signed-off-by: James O. D. Hunt --- src/pstring.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/pstring.c b/src/pstring.c index f6edbc7..dcffa06 100644 --- a/src/pstring.c +++ b/src/pstring.c @@ -8,6 +8,7 @@ #include "pstring.h" #include +#include extern wchar_t wide_indent_char; @@ -25,9 +26,22 @@ char_to_wchar (const char *str) memset(&ps, 0, sizeof (ps)); + errno = 0; len = mbsrtowcs (NULL, &str, 0, &ps); - if (len <= 0) + if (len == 0) { return NULL; + } + + if (len == (size_t)-1 || errno == EILSEQ) { + return NULL; + } + + /* Another possible error state. + * See mbsrtowcs(3). + */ + if (str == NULL) { + return NULL; + } /* include space for terminator */ bytes = (1 + len) * sizeof (wchar_t);