Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Solving the mystery of hanging character set conversions in glibc's iconv utility

April 23, 2021
Arjun Shankar
Related topics:
C, C#, C++LinuxOpen source
Related products:
Developer Toolset

    Website visitors don't typically consider character encoding and conversion when accessing digital content. However, engineers have been dealing with conversion issues since users started transferring strings from one computer to another. Today, when more than 95 percent of all web pages use UTF-8, converting data between character sets might seem less relevant. However, it continues to be useful when dealing with legacy data and systems, or with languages where multiple character sets are comparably popular.

    For example, using EUC-KR is not unusual on Korean websites because it is more efficient for Korean text. A search engine that indexes Korean sites needs to be able to convert between EUC-KR and UTF-8 to perform the relevant string comparisons.

    The iconv programming interface provided by POSIX, implemented by its namesake utility program, converts textual data from one character encoding to another. As an engineer on the Red Hat Platform Tools team, I was tasked with finding and fixing bugs reported in the GNU C Library (glibc) iconv utility. In this article, I discuss my experiences with fuzzing iconv and fixing bugs in the iconv front end.

    Error handling in iconv

    A typical iconv program invocation looks like so:

    $ iconv -f ISO-8859-7 -t UTF-8

    In this case, it is expected to read Latin and Greek text (encoded as ISO/IEC 8859-7) from standard input and write its UTF-8 equivalent to standard output.

    While POSIX allows the iconv utility to exit with an error when given invalid input, the glibc iconv implementation has an extension that is more tolerant of erroneous input. The user can add a suffix to the destination character set specification to enable iconv features that can:

    • Ignore invalid input characters while continuing to process further input.
    • Transliterate input characters without an equivalent in the output character set to a sufficiently similar combination of output characters.

    For example, if we wanted to convert UTF-8 encoded Czech text, which uses both diacritics and standard Latin letters, to ASCII, the following invocation would do the trick:

    $ iconv -f UTF-8 -t ASCII//TRANSLIT

    In this case, iconv transliterates the letters with diacritics into their diacritic-free equivalents.

    String manipulation in iconv_open

    This extension is probably implemented as a string suffix because the underlying iconv interface is a POSIX standard, and its signature doesn't allow for flags or options. Calls to iconv are made with a handler obtained by calling iconv_open, and with the appropriate conversion source and destination character encoding names. The iconv_open function has the following signature:

    iconv_t iconv_open (const char *tocode, const char *fromcode);

    Thus, the only meaningful way to extend its functionality without introducing a new interface is to pass option information in one of the string arguments.

    The glibc iconv implementation recognizes and uses conversion source and destination encodings in the form of a slash-separated triplet:

    • The first component of the triplet might be an encoding form, which implies a character set.
    • Alternatively, it might be a character set where a second component is ideally an encoding form for the underlying character set. If the encoding form is left unspecified, iconv uses a default.
    • The third component of the triplet is the optional suffix that enables GNU extensions. This means UCS-2// and ISO-10646/UCS-2/ are equivalent.

    However, because most of the common iconv invocations simply use encoding forms, the second component of the slash-separated specification was forgotten when documenting the interface, and the manual page ended up with the following fragment:

    Furthermore the GNU C library and the GNU libiconv library support the following 
    two suffixes:
    
           //TRANSLIT
                  When the string "//TRANSLIT" is appended to tocode,
                  transliteration is activated.  This means that when a
                  character cannot be represented in the target character
                  set, it can be approximated through one or several
                  similarly looking characters.
    
           //IGNORE
                  When the string "//IGNORE" is appended to tocode,
                  characters that cannot be represented in the target
                  character set will be silently discarded.

    There was no clear explanation of the triplet scheme and how multiple suffixes could be passed, which led to the implementation allowing calls such as:

    $ iconv -f ISO-8859-7 -t UTF-8//TRANSLIT//IGNORE

    Note that the missing second component and the resulting // were misunderstood as a suffix separator. Based on my reading, this misunderstanding seems to have touched the program code as well.

    Debugging the iconv utility

    Ideally, the iconv utility program would have avoided supporting options in the form of string suffixes. Instead, it might have supported them only by way of these options, but not in this case. It accepts the same suffixes as the underlying function.

    In 2016, a bug was reported against the iconv utility, uncovering a hang when the TRANSLIT and IGNORE suffixes were used in conjunction with bad input and the -c option. This combination is meant to cause iconv to omit invalid characters in the input from the output, similar to IGNORE, as follows:

    $ echo -en '\x80' | iconv -f us-ascii -t us-ascii//translit//ignore -c

    The report gathered comments including hints at the possible causes and other similar hangs. I didn't make much progress on the report until 2019, when I started looking at it in depth.

    Not knowing much about the internals of the iconv interface or utility, I decided to start with a bit of testing. I first wanted to check whether there were hangs in the iconv program other than the one reported. A "back of the envelope" calculation led me to conclude that it would take mere days to test every single two-byte input combination for all combinations of program switches and conversions—days that I wouldn't have to spend analyzing or debugging iconv code.

    One bash script and roughly a week later, I presented the results. There were hangs while converting 165 character sets. Almost all, with the exception of four, occurred when using the option suffixes (TRANSLIT and IGNORE) and the -c switch, which silenced error messages upon encountering invalid characters. It seemed clear that the problem was general and did not correspond to a bug in a specific character set's converter.

    After some debugging, I realized that the option suffixes were handled in string form during the program, instead of being used once to set corresponding flags that are subsequently used exclusively. Bugs in the code led to all but the first option being dropped. This led to hangs when the IGNORE option was passed after TRANSLIT.

    Fixing this involved a somewhat significant change in iconv's option parsing logic, but once that was fixed, the hangs vanished.

    Conclusion

    Eventually, I fixed the remaining known hangs in iconv. I am finally beginning to feel a bit more comfortable with the codebase. There is still some work to do in glibc's iconv implementation—among other things, improving the manual page. A new interface would also be useful.

    For more information on C and C++, please visit Red Hat's topic page.

    Last updated: February 5, 2024

    Recent Posts

    • MCP servers vs. skills: Choosing the right context for your AI

    • How to route external and local LLMs with Models-as-a-Service

    • Protect data offloaded to GPU-accelerated environments with OpenShift sandboxed containers

    • Case study: Measuring energy efficiency on the x64 platform

    • How to prevent AI inference stack silent failures

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.