Subject: bug#17136: [PATCH] dfa: speed up memory allocation, port to IRIX

From: Paul Eggert <[email-masked]>
Date: Fri, 18 Apr 2014 23:22:14 -0700

Norihiro Tanaka wrote:
> Perhaps, I may run accross it in order to use old GCC (4.1.2 on CentOS
> 5.10).

Thanks, it's lucky you did, so that we won't be inundated by other 
people reporting similar problems.  I see now that we were passing 
-Wno-pointer-sign to GCC, and that this suppressed useful diagnostics on 
newer GCC instances (but not on your older one).  I installed the 
attached patch to fix both the signedness problem, and the 
diagnostic-suppression problem.

From 5f7911e016a31a8d9de260e86103b1c2d286b3fa Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Fri, 18 Apr 2014 23:17:41 -0700
Subject: [PATCH] dfa: fix pointer type conversion bug

The code converted between size_t * and ptrdiff_t *, which wasn't
diagnosed by modern x86-64 GCC but isn't portable.  Problem
reported by Norihiro Tanaka in <http://bugs.gnu.org/17136#31>.
* configure.ac (WERROR_CFLAGS): Don't add -Wno-pointer-sign.
We want GCC to diagnose pointer signedness problems, as they
violate the C standard and other compilers no doubt complain too.
* src/dfa.c (struct dfa): Change type of salloc to size_t.
(realloc_trans_if_necessary): Convert signed value to size_t before
passing its address to x2nrealloc.  Changing the type of tralloc
to size_t might have led to problems elsewhere.
---
 configure.ac |  1 -
 src/dfa.c    | 10 +++++-----
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/configure.ac b/configure.ac
index 886449b..17a8920 100644
--- a/configure.ac
+++ b/configure.ac
@@ -145,7 +145,6 @@ if test "$gl_gcc_warnings" = yes; then
   done
   gl_WARN_ADD([-Wno-missing-field-initializers]) # We need this one
   gl_WARN_ADD([-Wno-sign-compare])     # Too many warnings for now
-  gl_WARN_ADD([-Wno-pointer-sign])     # Too many warnings for now
   gl_WARN_ADD([-Wno-unused-parameter]) # Too many warnings for now
 
   # In spite of excluding -Wlogical-op above, it is enabled, as of
diff --git a/src/dfa.c b/src/dfa.c
index eeca257..90cf4a9 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -378,7 +378,7 @@ struct dfa
   /* Fields filled by the state builder.  */
   dfa_state *states;            /* States of the dfa.  */
   state_num sindex;             /* Index for adding new states.  */
-  state_num salloc;             /* Number of states currently allocated.  */
+  size_t salloc;		/* Number of states currently allocated.  */
 
   /* Fields filled by the parse tree->NFA conversion.  */
   position_set *follows;        /* Array of follow sets, indexed by position
@@ -2780,12 +2780,12 @@ realloc_trans_if_necessary (struct dfa *d, state_num new_state)
   if (oldalloc <= new_state)
     {
       state_num **realtrans = d->trans ? d->trans - 1 : NULL;
-      size_t newalloc;
-      d->tralloc = new_state + 1;
-      realtrans = x2nrealloc (realtrans, &d->tralloc, sizeof *realtrans);
+      size_t newalloc, newalloc1;
+      newalloc1 = new_state + 1;
+      realtrans = x2nrealloc (realtrans, &newalloc1, sizeof *realtrans);
       realtrans[0] = NULL;
       d->trans = realtrans + 1;
-      newalloc = --d->tralloc;
+      d->tralloc = newalloc = newalloc1 - 1;
       d->fails = xnrealloc (d->fails, newalloc, sizeof *d->fails);
       d->success = xnrealloc (d->success, newalloc, sizeof *d->success);
       d->newlines = xnrealloc (d->newlines, newalloc, sizeof *d->newlines);
-- 
1.9.0