[libc-commits] [libc] [libc] Added isValidState to CharacterConverter class to ensure a provided mbstate is valid (PR #145564)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Wed Jun 25 10:45:02 PDT 2025


================
@@ -40,6 +43,17 @@ bool CharacterConverter::isFull() {
 
 bool CharacterConverter::isEmpty() { return state->bytes_stored == 0; }
 
+bool CharacterConverter::isValidState() {
+  if (state->total_bytes > MAX_UTF8_LENGTH)
+    return false;
+
+  const char32_t max_utf32_value =
+      state->total_bytes == 0 ? 0
+                              : MAX_VALUE_PER_UTF8_LEN[state->total_bytes - 1];
+  return state->bytes_stored <= state->total_bytes &&
+         state->bytes_stored >= 0 && state->partial <= max_utf32_value;
----------------
michaelrj-google wrote:

bytes stored is unsigned, so it will always be at least 0.

https://github.com/llvm/llvm-project/pull/145564


More information about the libc-commits mailing list