Skip to content

Improvements to TextEncodingConverter #142476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

abhina-sree
Copy link
Contributor

This patch addresses the follow-up comments on PR #138893

@abhina-sree abhina-sree changed the title Improvments to TextEncodingConverter Improvements to TextEncodingConverter Jun 2, 2025
@abhina-sree abhina-sree self-assigned this Jun 2, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 2, 2025

@llvm/pr-subscribers-llvm-support

Author: Abhina Sree (abhina-sree)

Changes

This patch addresses the follow-up comments on PR #138893


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

1 Files Affected:

  • (modified) llvm/lib/Support/TextEncoding.cpp (+5-5)
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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants