[BZ # 14672] Fix iconv -c

Andreas Jaeger aj@suse.com
Fri Nov 16 11:03:00 GMT 2012


Pasting the description from Bugzilla:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

While studying the source code of the iconv(1) program, I noticed a 
mistake in
the code handling the '-c' switch (which orders iconv to ignore any invalid
characters in the input stream). The mistake causes the program not to
recognise some of the encoding names in the presence of the '-c' flag, 
but the
conditions for observing the bug are relatively exotic, so it is rather
probable that nobody had ever stumbled upon the problem.

This is the relevant snippet of the code from main() in iconv/iconv_prog.c:

156:      if (*errhand == '/')
157:    {
158:      --nslash;
159:      errhand = strchrnul (errhand, '/');
160:
161:      if (*errhand == '/')
162:        {
163:          --nslash;
164:          errhand = strchr (errhand, '\0');
165:        }
166:    }

Notice that line 159 has no effect at all, since it searches for the first
occurrence of '/' in a string that starts with a '/' (as tested in line 
156).
The program should instead be calling strchrnul(errhand+1, '/'), since 
we are
trying to find the *next* slash in the string.

The consequence is that a single slash gets counted twice and the code that
follows (not shown above) later fails to append one even if it should. 
The bug
only shows when encoding names of the form ABC/DEF are used, i.e., a call to

iconv -c -f LATIN1 -t ISO/TR_11548-1

fails because the target encoding name gets rewritten into
"ISO/TR_11548-1,IGNORE" instead of "ISO/TR_11548-1/IGNORE" and iconv no 
longer
recognises this as a valid encoding name.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I've tested the appended patch on Linux/x86-64 and it handles now this
case.

Ok to commit?

Andreas


2012-11-16  Andrej Lajovic  <natrij@gmail.com>

	[BZ #14672]
	* iconv/iconv_prog.c (main): Fix -c handling of '/'.

diff --git a/iconv/iconv_prog.c b/iconv/iconv_prog.c
index 62c6794..a2ce396 100644
--- a/iconv/iconv_prog.c
+++ b/iconv/iconv_prog.c
@@ -1,5 +1,5 @@
  /* Convert text in given files from the specified from-set to the to-set.
-   Copyright (C) 1998-2011, 2012 Free Software Foundation, Inc.
+   Copyright (C) 1998-2012 Free Software Foundation, Inc.
     This file is part of the GNU C Library.
     Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.

@@ -155,7 +155,7 @@ main (int argc, char *argv[])
        if (*errhand == '/')
  	{
  	  --nslash;
-	  errhand = strchrnul (errhand, '/');
+	  errhand = strchrnul (errhand + 1, '/');

  	  if (*errhand == '/')
  	    {

-- 
  Andreas Jaeger aj@{suse.com,opensuse.org} Twitter/Identica: jaegerandi
   SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
    GF: Jeff Hawn,Jennifer Guild,Felix Imendörffer,HRB16746 (AG Nürnberg)
     GPG fingerprint = 93A3 365E CE47 B889 DF7F  FED1 389A 563C C272 A126



More information about the Libc-alpha mailing list