[llvm] 17c93d6 - Improvements to TextEncodingConverter (#142476)

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 25 06:35:30 PDT 2025


Author: Abhina Sree
Date: 2025-09-25T09:35:26-04:00
New Revision: 17c93d6e082c3cb3e3780c53e4d51ed9dc1fa5b5

URL: https://github.com/llvm/llvm-project/commit/17c93d6e082c3cb3e3780c53e4d51ed9dc1fa5b5
DIFF: https://github.com/llvm/llvm-project/commit/17c93d6e082c3cb3e3780c53e4d51ed9dc1fa5b5.diff

LOG: Improvements to TextEncodingConverter (#142476)

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

Added: 
    

Modified: 
    llvm/lib/Support/TextEncoding.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/TextEncoding.cpp b/llvm/lib/Support/TextEncoding.cpp
index b4ee0f8ee8bfd..804ff07f6e9a8 100644
--- a/llvm/lib/Support/TextEncoding.cpp
+++ b/llvm/lib/Support/TextEncoding.cpp
@@ -161,7 +161,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,
@@ -172,8 +172,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());
@@ -268,10 +270,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 = const_cast<char *>(InputLength ? Source.data() : "");
+    char *Input = const_cast<char *>(Source.data());
     Ret = iconv(ConvDesc, &Input, &InputLength, &Output, &OutputLength);
     if (Ret != 0) {
       if (auto EC = HandleError(Ret))


        


More information about the llvm-commits mailing list