Subject: bug#16614: FYI-patch: maint: use to_uchar function rather than explicit casts

From: Jim Meyering <[email-masked]>
Date: Sat, 1 Feb 2014 08:50:45 -0800

From c5cb52ecb97af4bf052e1c1366b8eb93a54ba6a0 Mon Sep 17 00:00:00 2001
From: Jim Meyering <[email-masked]>
Date: Thu, 30 Jan 2014 13:03:26 -0800
Subject: [PATCH] maint: use to_uchar function rather than explicit casts

* src/system.h (to_uchar): Define function.
* src/kwsearch.c (Fexecute): Use to_uchar twice in place of casts.
* src/dfasearch.c (EGexecute): Likewise.
* src/main.c (prepend_args): Likewise.
* src/kwset.c (U): Define in terms of to_uchar.
* src/dfa.c (match_mb_charset): Use to_uchar, not an explicit cast.
---
 src/dfa.c       | 2 +-
 src/dfasearch.c | 4 ++--
 src/kwsearch.c  | 4 ++--
 src/kwset.c     | 2 +-
 src/main.c      | 4 ++--
 src/system.h    | 9 +++++++++
 6 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/src/dfa.c b/src/dfa.c
index b79c604..f7453c7 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -3003,7 +3003,7 @@ match_mb_charset (struct dfa *d, state_num s, position pos, size_t idx)

   /* Match in range 0-255?  */
   if (wc < NOTCHAR && work_mbc->cset != -1
-      && tstbit ((unsigned char) wc, d->charclasses[work_mbc->cset]))
+      && tstbit (to_uchar (wc), d->charclasses[work_mbc->cset]))
     goto charset_matched;

   /* match with a character class?  */
diff --git a/src/dfasearch.c b/src/dfasearch.c
index 37b6acb..0b56960 100644
--- a/src/dfasearch.c
+++ b/src/dfasearch.c
@@ -325,9 +325,9 @@ EGexecute (char const *buf, size_t size, size_t *match_size,
                 while (match <= best_match)
                   {
                     regoff_t shorter_len = 0;
-                    if ((match == buf || !WCHAR ((unsigned char) match[-1]))
+                    if ((match == buf || !WCHAR (to_uchar (match[-1])))
                         && (start + len == end - buf - 1
-                            || !WCHAR ((unsigned char) match[len])))
+                            || !WCHAR (to_uchar (match[len]))))
                       goto assess_pattern_match;
                     if (len > 0)
                       {
diff --git a/src/kwsearch.c b/src/kwsearch.c
index 5d57639..643c6d8 100644
--- a/src/kwsearch.c
+++ b/src/kwsearch.c
@@ -130,9 +130,9 @@ Fexecute (char const *buf, size_t size, size_t *match_size,
       else if (match_words)
         for (try = beg; ; )
           {
-            if (try > buf && WCHAR((unsigned char) try[-1]))
+            if (try > buf && WCHAR(to_uchar (try[-1])))
               break;
-            if (try + len < buf + size && WCHAR((unsigned char) try[len]))
+            if (try + len < buf + size && WCHAR(to_uchar (try[len])))
               {
                 if (!len)
                   break;
diff --git a/src/kwset.c b/src/kwset.c
index 901e122..410e046 100644
--- a/src/kwset.c
+++ b/src/kwset.c
@@ -47,7 +47,7 @@
 #define obstack_chunk_alloc malloc
 #define obstack_chunk_free free

-#define U(c) ((unsigned char) (c))
+#define U(c) (to_uchar (c))

 /* Balanced tree of edges and labels leaving a given trie node. */
 struct tree
diff --git a/src/main.c b/src/main.c
index 3f16061..42f9ff3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1700,7 +1700,7 @@ prepend_args (char const *options, char *buf, char **argv)

   for (;;)
     {
-      while (c_isspace ((unsigned char) *o))
+      while (c_isspace (to_uchar (*o)))
         o++;
       if (!*o)
         return n;
@@ -1711,7 +1711,7 @@ prepend_args (char const *options, char *buf, char **argv)
       do
         if ((*b++ = *o++) == '\\' && *o)
           b[-1] = *o++;
-      while (*o && ! c_isspace ((unsigned char) *o));
+      while (*o && ! c_isspace (to_uchar (*o)));

       *b++ = '\0';
     }
diff --git a/src/system.h b/src/system.h
index b07471b..4c85409 100644
--- a/src/system.h
+++ b/src/system.h
@@ -55,4 +55,13 @@ enum { EXIT_TROUBLE = 2 };

 #define STREQ(a, b) (strcmp (a, b) == 0)

+/* Convert a possibly-signed character to an unsigned character.  This is
+   a bit safer than casting to unsigned char, since it catches some type
+   errors that the cast doesn't.  */
+static inline unsigned char
+to_uchar (char ch)
+{
+  return ch;
+}
+
 #endif
-- 
1.8.5.3.321.g14598b9