[libc-commits] [libc] [libc] Fixed mbtowc functions (PR #150118)
via libc-commits
libc-commits at lists.llvm.org
Tue Jul 22 14:48:58 PDT 2025
https://github.com/sribee8 created https://github.com/llvm/llvm-project/pull/150118
mbrtowc was not handling null destination correctly
>From 4a5a6a6e57d8656cac666b271edcc810d037876f Mon Sep 17 00:00:00 2001
From: Sriya Pratipati <sriyap at google.com>
Date: Tue, 22 Jul 2025 21:48:06 +0000
Subject: [PATCH] [libc] Fixed mbtowc functions
mbrtowc was not handling null destination correctly
---
libc/src/__support/wchar/mbrtowc.cpp | 3 ++-
libc/src/wchar/mbtowc.cpp | 5 +----
2 files changed, 3 insertions(+), 5 deletions(-)
diff --git a/libc/src/__support/wchar/mbrtowc.cpp b/libc/src/__support/wchar/mbrtowc.cpp
index 90ba934c42b69..0f730d6e6dbec 100644
--- a/libc/src/__support/wchar/mbrtowc.cpp
+++ b/libc/src/__support/wchar/mbrtowc.cpp
@@ -37,7 +37,8 @@ ErrorOr<size_t> mbrtowc(wchar_t *__restrict pwc, const char *__restrict s,
}
auto wc = char_conv.pop_utf32();
if (wc.has_value()) {
- *pwc = wc.value();
+ if (pwc != nullptr)
+ *pwc = wc.value();
// null terminator -> return 0
if (wc.value() == L'\0')
return 0;
diff --git a/libc/src/wchar/mbtowc.cpp b/libc/src/wchar/mbtowc.cpp
index eae39ba6081f3..6d099d43da5fa 100644
--- a/libc/src/wchar/mbtowc.cpp
+++ b/libc/src/wchar/mbtowc.cpp
@@ -25,10 +25,7 @@ LLVM_LIBC_FUNCTION(int, mbtowc,
if (s == nullptr)
return 0;
internal::mbstate internal_mbstate;
- // temp ptr to use if pwc is nullptr
- wchar_t buf[1];
- auto ret =
- internal::mbrtowc(pwc == nullptr ? buf : pwc, s, n, &internal_mbstate);
+ auto ret = internal::mbrtowc(pwc, s, n, &internal_mbstate);
if (!ret.has_value() || static_cast<int>(ret.value()) == -2) {
// Encoding failure
libc_errno = EILSEQ;
More information about the libc-commits
mailing list