Subject: bug#24535: [PATCH] sed: fix incorrect match for title case

From: Norihiro Tanaka <[email-masked]>
Date: Sun, 25 Sep 2016 14:49:11 +0900

On Sat, 24 Sep 2016 20:43:01 -0700
Jim Meyering <[email-masked]> wrote:

> Thank you for that patch.
> Quick feedback:
> 
> Did you intend to use the new function, require_el_iso88597_locale_?
> 
> Please replace this line from your new test (I prefer to avoid double
> quoting strings intended to contain backslashes, because each must be
> doubled, and that impairs readability/maintainability):
> 
>   printf "/\\(\\)\\\\1$chr/Ip" >prog || fail=1
> 
> with this:
> 
>   printf '/\(\)\\1'"$chr"/Ip >prog || fail=1
> 
> Is that a stray "-f -n" in the test script? This cannot work as intended:
> 
>   $ sed -f -n -f prog in >out
>   sed: couldn't open file -n: No such file or directory

Thanks for quick feedback.  Sorry, I seem that I extracted the patch
from old repository.  I fixed it, and ru-run all tests and they are
passed.
From c4d18fe3ea6b5386a0e81d662d05f7b42f309eae Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email-masked]>
Date: Sun, 25 Sep 2016 11:05:53 +0900
Subject: [PATCH] sed: fix incorrect match for title case

* sed/regexp.c (compile_regex_1): Avoid building fastmap for case
insensitive matching.
* testsuite/mb-title-case.sh: Add new test for this fix.
* testsuite/local.mk (T): Call the test and sort alphabetically.
* init.cfg: Add require_el_iso88597_locale_.
---
 init.cfg                   |    9 +++++++++
 sed/regexp.c               |    7 ++++---
 testsuite/local.mk         |    3 ++-
 testsuite/mb-title-case.sh |   36 ++++++++++++++++++++++++++++++++++++
 4 files changed, 51 insertions(+), 4 deletions(-)
 create mode 100755 testsuite/mb-title-case.sh

diff --git a/init.cfg b/init.cfg
index 39f009c..55aeed6 100644
--- a/init.cfg
+++ b/init.cfg
@@ -52,6 +52,15 @@ require_en_utf8_locale_()
   esac
 }
 
+require_el_iso88597_locale_()
+{
+  path_prepend_ ./testsuite
+  case $(get-mb-cur-max el_GR.iso88597) in
+    1) ;;
+    *) skip_ 'el_GR.iso88597 locale not found' ;;
+  esac
+}
+
 # Some tests would fail without this particular locale.
 # If the locale is not available, just skip the test.
 # The exact spelling differs between operating systems
diff --git a/sed/regexp.c b/sed/regexp.c
index 1eecd73..cf4f8a0 100644
--- a/sed/regexp.c
+++ b/sed/regexp.c
@@ -93,14 +93,15 @@ compile_regex_1 (struct regex *new_regex, int needed_sub)
     }
 
 #ifdef RE_ICASE
-  syntax |= (new_regex->flags & REG_ICASE) ? RE_ICASE : 0;
+  if (new_regex->flags & REG_ICASE)
+    syntax |= RE_ICASE;
+  else
 #endif
+    new_regex->pattern.fastmap = malloc (1 << (sizeof (char) * 8));
 #ifdef RE_NO_SUB
   syntax |= needed_sub ? 0 : RE_NO_SUB;
 #endif
 
-  new_regex->pattern.fastmap = malloc (1 << (sizeof (char) * 8));
-
   /* If REG_NEWLINE is set, newlines are treated differently.  */
   if (new_regex->flags & REG_NEWLINE)
     {
diff --git a/testsuite/local.mk b/testsuite/local.mk
index 6ae11e4..b30d44a 100644
--- a/testsuite/local.mk
+++ b/testsuite/local.mk
@@ -34,9 +34,10 @@ T =					\
   testsuite/in-place-hyphen.sh		\
   testsuite/in-place-suffix-backup.sh	\
   testsuite/invalid-mb-seq-UMR.sh	\
+  testsuite/mb-bad-delim.sh		\
   testsuite/mb-charclass-non-utf8.sh	\
   testsuite/mb-match-slash.sh		\
-  testsuite/mb-bad-delim.sh		\
+  testsuite/mb-title-case.sh		\
   testsuite/mb-y-translate.sh		\
   testsuite/normalize-text.sh		\
   testsuite/nulldata.sh			\
diff --git a/testsuite/mb-title-case.sh b/testsuite/mb-title-case.sh
new file mode 100755
index 0000000..962929b
--- /dev/null
+++ b/testsuite/mb-title-case.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+# Test case insensitive matching for titlecase and similarly odd chars.
+
+# Copyright (C) 2016 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+. "${srcdir=.}/testsuite/init.sh"; path_prepend_ ./sed
+print_ver_ sed
+
+require_el_iso88597_locale_
+
+fail=0
+
+a='\323' # SIGMA
+b='\362' # stigma
+c='\363' # sigma
+
+printf "$a\\n$b\\n$c\\n" >in || framework_failure_
+for chr in "$a" "$b" "$c"; do
+   printf '/\(\)\\1'"$chr"/Ip >prog || fail=1
+   LC_ALL=el_GR.iso88597 sed -n -f prog in >out || fail=1
+   compare_ in out || fail=1
+done
+
+Exit $fail
-- 
1.7.1