[llvm] Improvements to TextEncodingConverter (PR #142476)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 2 13:08:43 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Abhina Sree (abhina-sree)

<details>
<summary>Changes</summary>

This patch addresses the follow-up comments on PR https://github.com/llvm/llvm-project/pull/138893

---
Full diff: https://github.com/llvm/llvm-project/pull/142476.diff


1 Files Affected:

- (modified) llvm/lib/Support/TextEncoding.cpp (+5-5) 


``````````diff
diff --git a/llvm/lib/Support/TextEncoding.cpp b/llvm/lib/Support/TextEncoding.cpp
index 969dd419ede72..72a011c85e0eb 100644
--- a/llvm/lib/Support/TextEncoding.cpp
+++ b/llvm/lib/Support/TextEncoding.cpp
@@ -164,7 +164,7 @@ TextEncodingConverterICU::convertString(StringRef Source,
     EC = U_ZERO_ERROR;
     const char *Input = In;
 
-    Output = InputLength ? static_cast<char *>(Result.data()) : nullptr;
+    Output = static_cast<char *>(Result.data());
     ucnv_convertEx(&*ToConvDesc, &*FromConvDesc, &Output, Result.end(), &Input,
                    In + InputLength, /*pivotStart=*/NULL,
                    /*pivotSource=*/NULL, /*pivotTarget=*/NULL,
@@ -175,8 +175,10 @@ TextEncodingConverterICU::convertString(StringRef Source,
         if (Capacity < Result.max_size()) {
           HandleOverflow(Capacity, Output, OutputLength, Result);
           continue;
-        } else
+        } else {
+          Result.resize(Output - Result.data());
           return std::error_code(E2BIG, std::generic_category());
+        }
       }
       // Some other error occured.
       Result.resize(Output - Result.data());
@@ -271,10 +273,8 @@ TextEncodingConverterIconv::convertString(StringRef Source,
   };
 
   do {
-    // Setup the input. Use nullptr to reset iconv state if input length is
-    // zero.
     size_t InputLength = Source.size();
-    char *Input = InputLength ? const_cast<char *>(Source.data()) : "";
+    char *Input = const_cast<char *>(Source.data());
     Ret = iconv(ConvDesc, &Input, &InputLength, &Output, &OutputLength);
     if (Ret != 0) {
       if (auto EC = HandleError(Ret))

``````````

</details>


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


More information about the llvm-commits mailing list