bug-grep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] build: avoid compiler warnings


From: Jim Meyering
Subject: [PATCH] build: avoid compiler warnings
Date: Fri, 12 Mar 2010 20:42:24 +0100

Thanks again for all of that work, Paolo.
However, with your 10..17 patches, gcc printed several warnings
when I configured with --enable-gcc-warnings on F12.

The changes below fix all but one.
Would you please insert each fix in the change-set
that introduces the warning, so that we get a warning-free
build with --enable-gcc-warnings after each change set?

The one remaining warning indicates a real problem:

    search.c: In function 'mbtolower':
    search.c:107: error: comparison of unsigned expression < 0 is always false 
[-Wtype-limits]

That's complaining, rightfully, about this code:

  size_t n_free;

  if (*n > outlen)
    {
      /* Get some additional room since we're resizing.  */
      outlen = *n * 2 + MB_CUR_MAX + 1;
      out = xrealloc (out, outlen);
    }

  memset (&is, 0, sizeof (is));
  memset (&os, 0, sizeof (os));
  end = beg + *n;
  p = out;
  n_free = outlen - MB_CUR_MAX;
  while (beg < end)
    {
      wchar_t wc;
      size_t mbclen = mbrtowc(&wc, beg, end - beg, &is);
      if (n_free < 0)
        {
          n_free += outlen;
          outlen *= 2;
          out = xrealloc (out, outlen);
        }

Note how n_free is of type size_t,
and hence can never be smaller than zero.

While you're addressing that, you might want to use
x2nrealloc rather than blindly multiplying by 2,
which is subject to a theoretical overflow bug.
Using x2nrealloc is safer, and has a more moderate growth curve.


>From d9f05ea1da9e63489e4f6bd59e3391c20b549983 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Fri, 12 Mar 2010 20:34:55 +0100
Subject: [PATCH] build: avoid compiler warnings

* src/search.c (mbtolower): s/free/n_free/ to avoid shadowing.
(is_mb_middle): Remove declarations of now-unused vars: i, wc.
* src/dfa.c: Include assert.h, now that it's used again.
(fetch_wc): Remove now-unused, empty function.
(lex): Remove declaration of unused vars: c1, invert.
(check_utf8): Declare to be static.
* src/grep.c (do_execute): Declare to be static.
---
 src/dfa.c    |   16 +++++-----------
 src/grep.c   |    3 ++-
 src/search.c |   12 +++++-------
 3 files changed, 12 insertions(+), 19 deletions(-)

diff --git a/src/dfa.c b/src/dfa.c
index 0558902..2e30bc1 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -23,6 +23,7 @@
 #include <config.h>
 #include <ctype.h>
 #include <stdio.h>
+#include <assert.h>
 #include <sys/types.h>
 #include <stddef.h>
 #include <stdlib.h>
@@ -296,7 +297,7 @@ static unsigned char const *buf_end;        /* reference to 
end in dfaexec().  */
    assume in a multibyte encoding. */
 static int using_utf8;

-void
+static void
 check_utf8 (void)
 {
 #ifdef HAVE_LANGINFO_CODESET
@@ -305,7 +306,7 @@ check_utf8 (void)
 #endif
 }
 #else
-void
+static void
 check_utf8 (void)
 {
 }
@@ -346,13 +347,6 @@ check_utf8 (void)
     wint_t _wc;                                        \
     FETCH_WC(c, _wc, eoferr);                  \
   } while(0)
-
-/* This function fetch a wide character, and update cur_mb_len,
-   used only if the current locale is a multibyte environment.  */
-static wint_t
-fetch_wc (char const *eoferr)
-{
-}
 #else
 /* Note that characters become unsigned here. */
 # define FETCH(c, eoferr)            \
@@ -737,8 +731,8 @@ parse_bracket_exp (void)
 static token
 lex (void)
 {
-  unsigned c, c1, c2;
-  int backslash = 0, invert;
+  unsigned c, c2;
+  int backslash = 0;
   charclass ccl;
   int i;

diff --git a/src/grep.c b/src/grep.c
index 1f73c70..6989b83 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -1025,7 +1025,8 @@ prtext (char const *beg, char const *lim, int *nlinesp)
   used = 1;
 }

-EXECUTE_RET do_execute EXECUTE_ARGS
+static EXECUTE_RET
+do_execute EXECUTE_ARGS
 {
   const char *line_buf, *line_end, *line_next;
   size_t result = (size_t) -1;
diff --git a/src/search.c b/src/search.c
index a87e33a..15b7b70 100644
--- a/src/search.c
+++ b/src/search.c
@@ -86,7 +86,7 @@ mbtolower (const char *beg, size_t *n)
   mbstate_t is, os;
   const char *end;
   char *p;
-  size_t free;
+  size_t n_free;

   if (*n > outlen)
     {
@@ -99,14 +99,14 @@ mbtolower (const char *beg, size_t *n)
   memset (&os, 0, sizeof (os));
   end = beg + *n;
   p = out;
-  free = outlen - MB_CUR_MAX;
+  n_free = outlen - MB_CUR_MAX;
   while (beg < end)
     {
       wchar_t wc;
       size_t mbclen = mbrtowc(&wc, beg, end - beg, &is);
-      if (free < 0)
+      if (n_free < 0)
         {
-          free += outlen;
+          n_free += outlen;
           outlen *= 2;
           out = xrealloc (out, outlen);
         }
@@ -124,7 +124,7 @@ mbtolower (const char *beg, size_t *n)
           beg += mbclen;
           mbclen = wcrtomb (p, towlower ((wint_t) wc), &os);
           p += mbclen;
-          free -= mbclen;
+          n_free -= mbclen;
         }
     }

@@ -224,8 +224,6 @@ is_mb_middle(const char **good, const char *buf, const char 
*end)
 {
   const char *p = *good, *prev = p;
   mbstate_t cur_state;
-  wchar_t wc;
-  int i;

   /* TODO: can be optimized for UTF-8.  */
   memset(&cur_state, 0, sizeof(mbstate_t));
--
1.7.0.2.393.gfb6b




reply via email to

[Prev in Thread] Current Thread [Next in Thread]