From 6e319a818ed7b15b452ed2baab2f6a38d42fd1fe Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 14 Sep 2014 14:52:56 -0700 Subject: grep: improve performance for older glibc glibc has a bug where mbrlen and mbrtowc mishandle length-0 inputs. Working around it in gnulib slows grep down, so disable the tests for it and make sure grep works even if the bug is present. * bootstrap.conf (avoided_gnulib_modules): Add mbrtowc-tests. * configure.ac (gl_cv_func_mbrtowc_empty_input): Assume yes. * src/searchutils.c (mb_next_wc): Don't invoke mbrtowc on empty input. --- bootstrap.conf | 1 + configure.ac | 5 +++++ src/searchutils.c | 3 ++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/bootstrap.conf b/bootstrap.conf index d8171f5c..50c0aab1 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -17,6 +17,7 @@ avoided_gnulib_modules=' --avoid=lock-tests + --avoid=mbrtowc-tests ' # gnulib modules used by this package. diff --git a/configure.ac b/configure.ac index 3315855d..4d069b87 100644 --- a/configure.ac +++ b/configure.ac @@ -83,6 +83,11 @@ AC_PROG_CC gl_EARLY AC_PROG_RANLIB +# grep never invokes mbrtowc or mbrlen on empty input, +# so don't worry about this common bug, +# as working around it would merely slow grep down. +gl_cv_func_mbrtowc_empty_input='assume yes' + dnl Checks for typedefs, structures, and compiler characteristics. AC_TYPE_SIZE_T AC_C_CONST diff --git a/src/searchutils.c b/src/searchutils.c index 5eb9a12f..18dd584e 100644 --- a/src/searchutils.c +++ b/src/searchutils.c @@ -285,5 +285,6 @@ mb_next_wc (char const *cur, char const *end) { wchar_t wc; mbstate_t mbs = { 0 }; - return mbrtowc (&wc, cur, end - cur, &mbs) < (size_t) -2 ? wc : WEOF; + return (end - cur != 0 && mbrtowc (&wc, cur, end - cur, &mbs) < (size_t) -2 + ? wc : WEOF); } -- cgit v1.2.1