Skip to content

Commit

Permalink
pstring: Improve error checking in char_to_wchar.
Browse files Browse the repository at this point in the history
Make the existing error checking clearer and also check `errno` to handle
another possible failure scenario.

Signed-off-by: James O. D. Hunt <[email protected]>
  • Loading branch information
jamesodhunt committed Jan 1, 2025
1 parent 2809268 commit 0e31aac
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/pstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "pstring.h"

#include <assert.h>
#include <errno.h>

extern wchar_t wide_indent_char;

Expand All @@ -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);
Expand Down

0 comments on commit 0e31aac

Please sign in to comment.