After clarify-memory-allocation, fail in the regression tests and occur
the two warnings in compilation.  Fix them.

Norihiro
From 7982fbc2bae3e23047680efb7f77c1aa548ce970 Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email protected]>
Date: Fri, 18 Apr 2014 22:44:02 +0900
Subject: [PATCH] grep: fix for clarify-memory-allocation

* src/dfa.c (typedef token): Change ptrdiff_t into size_t.
(typedef state_num): Change ptrdiff_t into size_t.
(struct mb_char_classes): Change ptrdiff_t into size_t.
(dfa_charclass_index): Fix new size to maybe_realloc().
(parse_bracket_exp): Fix new sizes to maybe_realloc().
(addtok): Cast negative integer to size_t.
(insert): Fix new size to maybe_realloc().
(state_index): Fix new size to maybe_realloc().
(match_mb_charset): Cast negative integer to size_t.
src/dfa.h (dfastate): Change ptrdiff_t into size_t.
---
 src/dfa.c | 25 ++++++++++++++-----------
 src/dfa.h |  2 +-
 2 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/src/dfa.c b/src/dfa.c
index eeca257..a355790 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -158,7 +158,7 @@ to_uchar (char ch)
    are operators and others are terminal symbols.  Most (but not all) of these
    codes are returned by the lexical analyzer.  */
 
-typedef ptrdiff_t token;
+typedef size_t token;
 
 /* Predefined token values.  */
 enum
@@ -295,13 +295,13 @@ typedef struct
 
 /* States are indexed by state_num values.  These are normally
    nonnegative but -1 is used as a special value.  */
-typedef ptrdiff_t state_num;
+typedef size_t state_num;
 
 /* A bracket operator.
    e.g., [a-c], [[:alpha:]], etc.  */
 struct mb_char_classes
 {
-  ptrdiff_t cset;
+  size_t cset;
   bool invert;
   wchar_t *chars;               /* Normal characters.  */
   size_t nchars;
@@ -643,7 +643,7 @@ dfa_charclass_index (struct dfa *d, charclass const s)
   for (i = 0; i < d->cindex; ++i)
     if (equal (s, d->charclasses[i]))
       return i;
-  d->charclasses = maybe_realloc (d->charclasses, d->cindex, &d->calloc,
+  d->charclasses = maybe_realloc (d->charclasses, d->cindex + 1, &d->calloc,
                                   sizeof *d->charclasses);
   ++d->cindex;
   copyset (s, d->charclasses[i]);
@@ -987,7 +987,7 @@ parse_bracket_exp (void)
   chars_al = ranges_al = ch_classes_al = equivs_al = coll_elems_al = 0;
   if (MB_CUR_MAX > 1)
     {
-      dfa->mbcsets = maybe_realloc (dfa->mbcsets, dfa->nmbcsets,
+      dfa->mbcsets = maybe_realloc (dfa->mbcsets, dfa->nmbcsets + 1,
                                     &dfa->mbcsets_alloc,
                                     sizeof *dfa->mbcsets);
 
@@ -1068,7 +1068,7 @@ parse_bracket_exp (void)
 
                       work_mbc->ch_classes
                         = maybe_realloc (work_mbc->ch_classes,
-                                         work_mbc->nch_classes, &ch_classes_al,
+                                         work_mbc->nch_classes + 1, 
&ch_classes_al,
                                          sizeof *work_mbc->ch_classes);
                       work_mbc->ch_classes[work_mbc->nch_classes++] = wt;
                     }
@@ -1132,6 +1132,9 @@ parse_bracket_exp (void)
 
                   if (case_fold && (iswalpha (wc) || iswalpha (wc2)))
                     {
+                      work_mbc->ranges
+                        = maybe_realloc (work_mbc->ranges, work_mbc->nranges + 
1,
+                                         &ranges_al, sizeof *work_mbc->ranges);
                       work_mbc->ranges[work_mbc->nranges].beg = towupper (wc);
                       work_mbc->ranges[work_mbc->nranges++].end
                         = towupper (wc2);
@@ -1191,7 +1194,7 @@ parse_bracket_exp (void)
         }
       if (!setbit_wc (wc, ccl))
         {
-          work_mbc->chars = maybe_realloc (work_mbc->chars, work_mbc->nchars,
+          work_mbc->chars = maybe_realloc (work_mbc->chars, work_mbc->nchars + 
1,
                                            &chars_al, sizeof *work_mbc->chars);
           work_mbc->chars[work_mbc->nchars++] = wc;
         }
@@ -1635,7 +1638,7 @@ addtok (token t)
         {
           /* Characters have been handled above, so it is possible
              that the mbcset is empty now.  Do nothing in that case.  */
-          if (work_mbc->cset != -1)
+          if (work_mbc->cset != (size_t) -1)
             {
               addtok (CSET + work_mbc->cset);
               if (need_or)
@@ -2004,7 +2007,7 @@ insert (position p, position_set * s)
       return;
     }
 
-  s->elems = maybe_realloc (s->elems, count, &s->alloc, sizeof *s->elems);
+  s->elems = maybe_realloc (s->elems, count + 1, &s->alloc, sizeof *s->elems);
   for (i = count; i > lo; i--)
     s->elems[i] = s->elems[i - 1];
   s->elems[lo] = p;
@@ -2084,7 +2087,7 @@ state_index (struct dfa *d, position_set const *s, int 
context)
     }
 
   /* We'll have to create a new state.  */
-  d->states = maybe_realloc (d->states, d->sindex, &d->salloc,
+  d->states = maybe_realloc (d->states, d->sindex + 1, &d->salloc,
                              sizeof *d->states);
   d->states[i].hash = hash;
   alloc_position_set (&d->states[i].elems, s->nelem);
@@ -2989,7 +2992,7 @@ match_mb_charset (struct dfa *d, state_num s, position 
pos,
   match = !work_mbc->invert;
 
   /* Match in range 0-255?  */
-  if (wc < NOTCHAR && work_mbc->cset != -1
+  if (wc < NOTCHAR && work_mbc->cset != (size_t) -1
       && tstbit (to_uchar (wc), d->charclasses[work_mbc->cset]))
     goto charset_matched;
 
diff --git a/src/dfa.h b/src/dfa.h
index db29a62..64ce627 100644
--- a/src/dfa.h
+++ b/src/dfa.h
@@ -96,7 +96,7 @@ extern void dfaanalyze (struct dfa *, int);
 
 /* Compute, for each possible character, the transitions out of a given
    state, storing them in an array of integers. */
-extern void dfastate (ptrdiff_t, struct dfa *, ptrdiff_t []);
+extern void dfastate (size_t, struct dfa *, size_t []);
 
 /* Error handling. */
 
-- 
1.9.2

Reply via email to