This is the mail archive of the libc-hacker@sourceware.org mailing list for the glibc project.

Note that libc-hacker is a closed list. You may look at the archives of this list, but subscription and posting are not open.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix iconv from SHIFT-JIS


Hi!

As can be seen on the https://bugzilla.redhat.com/show_bug.cgi?id=497267
testcase, sjis.c handles invalid 2 byte inputs incorrectly, inptr += 2
shouldn't be done before STANDARD_FROM_LOOP_ERR_HANDLER (2).
The code does:
if (something)
  STANDARD_FROM_LOOP_ERR_HANDLER (1);
else
  {
    if (...)
      ch = ...
    else...
      ch = ...
    inptr += 2;
  }
if (ch == 0)
  STANDARD_FROM_LOOP_ERR_HANDLER (2);

As STANDARD_FROM_LOOP_ERR_HANDLER never falls through (either does break
or continue), we can move the second S_F_L_E_H into the else and it really
has to be done before the inptr += 2, otherwise when stopping on errors
we move over the invalid 2 byte sequence and when ignoring errors
skip over 4 bytes instead of 2.
Also, I believe we should skip over 2 bytes instead of 1 when ignoring
errors for say \xea\xaf, instead of trying to convert the second byte as
start of a character.

2009-04-24  Jakub Jelinek  <jakub@redhat.com>

	* iconvdata/sjis.c (BODY): Don't advance inptr before
	STANDARD_FROM_LOOP_ERR_HANDLER (2) for 2 byte invalid input.
	Use STANDARD_FROM_LOOP_ERR_HANDLER with 2 instead of 1 for
	two byte chars.

--- libc/iconvdata/sjis.c.jj	2002-12-02 23:07:56.000000000 +0100
+++ libc/iconvdata/sjis.c	2009-04-24 18:09:01.000000000 +0200
@@ -1,5 +1,5 @@
 /* Mapping tables for SJIS handling.
-   Copyright (C) 1997-2001, 2002 Free Software Foundation, Inc.
+   Copyright (C) 1997-2001, 2002, 2009 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
 
@@ -4379,7 +4379,7 @@ static const char from_ucs4_extra[0x100]
 	    || __builtin_expect (idx > 0xeaa4, 0))			      \
 	  {								      \
 	    /* This is illegal.  */					      \
-	    STANDARD_FROM_LOOP_ERR_HANDLER (1);				      \
+	    STANDARD_FROM_LOOP_ERR_HANDLER (2);				      \
 	  }								      \
 	else								      \
 	  {								      \
@@ -4395,14 +4395,15 @@ static const char from_ucs4_extra[0x100]
 	    else							      \
 	      ch = cjk_block4[(ch - 0xe0) * 192 + ch2 - 0x40];		      \
 									      \
+	    if (__builtin_expect (ch == 0, 0))				      \
+	      {								      \
+		/* This is an illegal character.  */			      \
+		STANDARD_FROM_LOOP_ERR_HANDLER (2);			      \
+	      }								      \
+									      \
 	    inptr += 2;							      \
 	  }								      \
 									      \
-	if (__builtin_expect (ch == 0, 0))				      \
-	  {								      \
-	    /* This is an illegal character.  */			      \
-	    STANDARD_FROM_LOOP_ERR_HANDLER (2);				      \
-	  }								      \
       }									      \
 									      \
     put32 (outptr, ch);							      \

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]