bug-gnulib
[Top][All Lists]
Advanced

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

several trim bugs


From: Colin Watson
Subject: several trim bugs
Date: Wed, 17 Oct 2007 23:14:33 +0100
User-agent: Mutt/1.5.13 (2006-08-11)

Firstly, the trim module fails to add its files to lib_SOURCES, so they
don't get built.

Secondly, after fixing that, it tries to include "mbuiter.h" without
depending on that module. On inspection, I found that it claims to be
including it for MB_CUR_MAX, but that's actually in <stdlib.h> (the only
system header mbuiter.h includes over and above mbiter.h) so including
"mbuiter.h" for it seems odd and redundant. I added a check for
defined(MB_CUR_MAX) because I don't know offhand if C89 <stdlib.h>
guarantees that; if it does, please remove this check again.

Thirdly, trim.c uses isspace unconditionally so it should also include
<ctype.h> unconditionally.

Fourthly, gcc complains:

  trim.c: In function ‘trim2’:
  trim.c:67: warning: ‘r’ may be used uninitialized in this function

... and it may be that the state machine in fact renders it impossible
for this to be used uninitialised but this wasn't obvious to me. Some
more sanity-checking seems to be called for here.

The following patch fixes all of these problems in the obvious ways.

diff --git a/lib/trim.c b/lib/trim.c
index b917549..b7dbd70 100644
--- a/lib/trim.c
+++ b/lib/trim.c
@@ -20,13 +20,13 @@
 # include <config.h>
 #endif
 
+#include <stdlib.h>            /* for MB_CUR_MAX */
+#include <ctype.h>
+
 #if HAVE_MBRTOWC 
 # include <stddef.h>
 # include "mbchar.h"
 # include "mbiter.h"
-# include "mbuiter.h"          /* FIXME: for MB_CUR_MAX */
-#else
-# include <ctype.h>
 #endif
 
 #include "xalloc.h"
@@ -42,7 +42,7 @@ trim2(const char *s, int how)
   if (!d)
     xalloc_die();
   
-#if HAVE_MBRTOWC
+#if HAVE_MBRTOWC && defined(MB_CUR_MAX)
   if (MB_CUR_MAX > 1)
     {
       mbi_iterator_t i;
@@ -62,7 +62,7 @@ trim2(const char *s, int how)
       if (how != TRIM_LEADING) 
        {
          int state = 0;
-         char *r;
+         char *r = NULL;
          
          mbi_init (i, d, strlen (d));
 
@@ -101,7 +101,7 @@ trim2(const char *s, int how)
                }
            }
          
-         if (state == 2) 
+         if (state == 2 && r)
            *r = '\0';
        }
     }
diff --git a/modules/trim b/modules/trim
index 27cdefc..7cd1a7c 100644
--- a/modules/trim
+++ b/modules/trim
@@ -12,6 +12,7 @@ mbiter
 configure.ac:
 
 Makefile.am:
+lib_SOURCES += trim.h trim.c
 
 Include:
 #include "trim.h"

Thanks,

-- 
Colin Watson                                       address@hidden




reply via email to

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