bug-gnulib
[Top][All Lists]
Advanced

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

Re: [Bug-gnulib] Patch proposal: 1-gary-safe-xfree.patch


From: Paul Eggert
Subject: Re: [Bug-gnulib] Patch proposal: 1-gary-safe-xfree.patch
Date: 15 Sep 2003 13:21:22 -0700
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

> they are none-the-less genuinely useful shorthand that can't be done
> without macros. Do you have a similar replacement functionality in
> mind?  Or do you find macros like these to be worse than the extra
> typing that would be required in their absence?

Yes, because code is clearer when function-like macros act like functions.

Also, less typing is often required if you avoid the macros.  For
example, compare this code (taken from GNU m4):

  new = XMALLOC (macro_definition, 1);

to this proposed replacement:

  new = xmalloc (sizeof *new);

The latter style is shorter in this case.  (In other cases it's
longer, of course.)  Another advantage of the latter style is that
it's immune to changes in the type of the variable.

Here's a proposed patch to GNU m4 to let it survive the loss of the
macros in question, so that you can see how much the code is actually
affected.

Aside: I notice that m4, after the proposed patch, uses calloc (1, S)
a lot.  Perhaps we should define a new zalloc (S) that is equivalent
to calloc (1, S), for convenience?  I've used zalloc in other
programs.

2003-09-15  Paul Eggert  <address@hidden>

        Don't use XMALLOC and XCALLOC.  Once we install the
        corresponding patch into gnulib, this will fix some
        address-calculation overflow bugs on hosts where calloc (A, B)
        returns garbage when A*B overflows.

        * m4/hash.c (m4_hash_new, node_new, m4_hash_resize,
        maybe_grow, m4_get_hash_iterator_next): Replace XMALLOC with
        xmalloc, XCALLOC with xcalloc.
        * m4/m4.c (m4_create): Likewise.
        * m4/m4private.h (m4_symbol_value_create): Likewise.
        * m4/output.c (m4_output_init): Likewise.
        * m4/symtab.c (symtab_fetch, m4_set_symbol_traced): Likewise.
        * m4/syntax.c (remove_syntax_attribute): Likewise.
        * src/freeze.c (reload_frozen_state): Likewise.
        * src/main.c (main): Likewise.

Index: m4/hash.c
===================================================================
RCS file: /cvsroot/m4/m4/m4/hash.c,v
retrieving revision 1.13
diff -p -u -r1.13 hash.c
--- m4/hash.c   11 Sep 2003 16:38:12 -0000      1.13
+++ m4/hash.c   15 Sep 2003 19:54:21 -0000
@@ -94,10 +94,10 @@ m4_hash_new (size_t size, m4_hash_hash_f
   if (size == 0)
     size = M4_HASH_DEFAULT_SIZE;
 
-  hash                 = XMALLOC (m4_hash, 1);
+  hash                 = xmalloc (sizeof *hash);
   HASH_SIZE (hash)     = size;
   HASH_LENGTH (hash)   = 0;
-  HASH_BUCKETS (hash)  = XCALLOC (hash_node *, size);
+  HASH_BUCKETS (hash)  = xcalloc (size, sizeof HASH_BUCKETS (hash));
   HASH_HASH_FUNC (hash)        = hash_func;
   HASH_CMP_FUNC (hash) = cmp_func;
 
@@ -178,7 +178,7 @@ node_new (const void *key, void *value)
     }
   else
     {
-      node     = XMALLOC (hash_node, 1);
+      node     = xmalloc (sizeof *node);
     }
 
   assert (node);
@@ -345,7 +345,7 @@ m4_hash_resize (m4_hash *hash, size_t si
   original_buckets     = HASH_BUCKETS (hash);
 
   HASH_SIZE (hash)     = size;
-  HASH_BUCKETS (hash)= XCALLOC (hash_node *, size);
+  HASH_BUCKETS (hash)   = xcalloc (size, sizeof HASH_BUCKETS (hash));
 
   {
     size_t i;
@@ -376,7 +376,7 @@ maybe_grow (m4_hash *hash)
 
       /* HASH sizes are always 1 less than a power of 2.  */
       HASH_SIZE (hash)    = (2* (1+ original_size)) -1;
-      HASH_BUCKETS (hash) = XCALLOC (hash_node *, hash->size);
+      HASH_BUCKETS (hash) = xcalloc (hash->size, sizeof HASH_BUCKETS (hash));
 
       {
        size_t i;
@@ -447,7 +447,7 @@ m4_get_hash_iterator_next (const m4_hash
   /* On the first iteration, allocate an iterator.  */
   if (!place)
     {
-      place = XCALLOC (m4_hash_iterator, 1);
+      place = xcalloc (1, sizeof *place);
       ITERATOR_HASH (place) = hash;
     }
 
Index: m4/m4.c
===================================================================
RCS file: /cvsroot/m4/m4/m4/m4.c,v
retrieving revision 1.10
diff -p -u -r1.10 m4.c
--- m4/m4.c     27 Aug 2003 17:10:12 -0000      1.10
+++ m4/m4.c     15 Sep 2003 19:54:22 -0000
@@ -26,7 +26,7 @@
 m4 *
 m4_create (void)
 {
-  m4 *context = XCALLOC (m4, 1);
+  m4 *context = xcalloc (1, sizeof *context);
 
   context->symtab = m4_symtab_create (0, &context->no_gnu_extensions);
   context->syntax = m4_syntax_create ();
@@ -36,7 +36,7 @@ m4_create (void)
 
   context->nesting_limit = DEFAULT_NESTING_LIMIT;
 
-  context->search_path  = XCALLOC (m4__search_path_info, 1);
+  context->search_path  = xcalloc (1, sizeof context->search_path);
 
   return context;
 }
Index: m4/m4private.h
===================================================================
RCS file: /cvsroot/m4/m4/m4/m4private.h,v
retrieving revision 1.38
diff -p -u -r1.38 m4private.h
--- m4/m4private.h      11 Sep 2003 16:38:12 -0000      1.38
+++ m4/m4private.h      15 Sep 2003 19:54:22 -0000
@@ -158,7 +158,7 @@ struct m4_symbol_value {
 #  define m4_get_symbol_traced(S)      ((S)->traced)
 #  define m4_set_symbol_traced(S, V)   ((S)->traced = (V))
 
-#  define m4_symbol_value_create()     (XCALLOC (m4_symbol_value, 1))
+#  define m4_symbol_value_create()     xcalloc (1, sizeof (m4_symbol_value))
 #  define m4_symbol_value_delete(V)    (DELETE (V))
 
 #  define m4_is_symbol_value_text(V)   ((V)->type == M4_SYMBOL_TEXT)
Index: m4/output.c
===================================================================
RCS file: /cvsroot/m4/m4/m4/output.c,v
retrieving revision 1.16
diff -p -u -r1.16 output.c
--- m4/output.c 11 Sep 2003 16:38:12 -0000      1.16
+++ m4/output.c 15 Sep 2003 19:54:22 -0000
@@ -105,7 +105,7 @@ int m4_output_current_line;
 void
 m4_output_init (void)
 {
-  diversion_table = XMALLOC (struct diversion, 1);
+  diversion_table = xmalloc (sizeof *diversion_table);
   diversions = 1;
   diversion_table[0].file = stdout;
   diversion_table[0].buffer = NULL;
Index: m4/symtab.c
===================================================================
RCS file: /cvsroot/m4/m4/m4/symtab.c,v
retrieving revision 1.46
diff -p -u -r1.46 symtab.c
--- m4/symtab.c 11 Sep 2003 16:38:12 -0000      1.46
+++ m4/symtab.c 15 Sep 2003 19:54:23 -0000
@@ -127,7 +127,7 @@ symtab_fetch (m4_symbol_table *symtab, c
     }
   else
     {
-      symbol = XCALLOC (m4_symbol, 1);
+      symbol = xcalloc (1, sizeof *symbol);
       m4_hash_insert (symtab->table, xstrdup (name), symbol);
     }
 
@@ -414,7 +414,7 @@ m4_set_symbol_traced (m4_symbol *symbol,
 m4_symbol_value *
 m4_symbol_value_create (void)
 {
-  return XCALLOC (m4_symbol_value, 1);
+  return xcalloc (1, sizeof (m4_symbol_value));
 }
 
 #undef m4_get_symbol_value
Index: m4/syntax.c
===================================================================
RCS file: /cvsroot/m4/m4/m4/syntax.c,v
retrieving revision 1.9
diff -p -u -r1.9 syntax.c
--- m4/syntax.c 11 Sep 2003 16:38:12 -0000      1.9
+++ m4/syntax.c 15 Sep 2003 19:54:23 -0000
@@ -98,7 +98,7 @@ static int remove_syntax_attribute (m4_s
 m4_syntax_table *
 m4_syntax_create (void)
 {
-  m4_syntax_table *syntax = XCALLOC (m4_syntax_table, 1);
+  m4_syntax_table *syntax = xcalloc (1, sizeof *syntax);
   int ch;
 
   for (ch = 256; --ch > 0;)
Index: src/freeze.c
===================================================================
RCS file: /cvsroot/m4/m4/src/freeze.c,v
retrieving revision 1.35
diff -p -u -r1.35 freeze.c
--- src/freeze.c        27 Aug 2003 17:10:13 -0000      1.35
+++ src/freeze.c        15 Sep 2003 19:54:23 -0000
@@ -465,7 +465,7 @@ reload_frozen_state (m4 *context, const 
 
          if (bp)
            {
-             m4_symbol_value *token = XCALLOC (m4_symbol_value, 1);
+             m4_symbol_value *token = xcalloc (1, sizeof *token);
 
              if (bp->groks_macro_args)
                BIT_SET (VALUE_FLAGS (token), VALUE_MACRO_ARGS_BIT);
@@ -651,7 +651,7 @@ reload_frozen_state (m4 *context, const 
 
        /* Enter a macro having an expansion text as a definition.  */
        {
-         m4_symbol_value *token = XCALLOC (m4_symbol_value, 1);
+         m4_symbol_value *token = xcalloc (1, sizeof *token);
          lt_dlhandle handle = 0;
 
          if (number[2] > 0)
Index: src/main.c
===================================================================
RCS file: /cvsroot/m4/m4/src/main.c,v
retrieving revision 1.53
diff -p -u -r1.53 main.c
--- src/main.c  12 Sep 2003 15:17:49 -0000      1.53
+++ src/main.c  15 Sep 2003 19:54:24 -0000
@@ -268,7 +268,7 @@ main (int argc, char *const *argv, char 
       case 'm':
        /* Arguments that cannot be handled until later are accumulated.  */
 
-       new = XMALLOC (macro_definition, 1);
+       new = xmalloc (sizeof *new);
        new->code = optchar;
        new->macro = optarg;
        new->next = NULL;
@@ -419,7 +419,7 @@ warranty; not even for MERCHANTABILITY o
 
       for (env = envp; *env != NULL; env++)
        {
-         new = XMALLOC (macro_definition, 1);
+         new = xmalloc (sizeof *new);
          new->code = 'D';
          new->macro = *env;
          new->next = head;




reply via email to

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