bug-gnu-utils
[Top][All Lists]
Advanced

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

diffutils: xmalloc and xrealloc


From: Neal H Walfield
Subject: diffutils: xmalloc and xrealloc
Date: Fri, 14 Dec 2001 00:32:05 +0100
User-agent: Gnus/5.090004 (Oort Gnus v0.04) Emacs/21.1

Diff utils has several different implementations of xmalloc and
xrealloc scattered across a few different files.  These should really
be unified into a single implementation, i.e. the one found in
xmalloc.c (which, at the moment, is only used by the diff program).

This patch attempts to correct this by moving the prototypes to a new
header, xmalloc.h, and changing all of the callers of xmalloc and
xrealloc to use a single implementation -- the one found in xmalloc.c.
Additionally, this patch also catches a bug where the sdiff program
was using realloc instead of xrealloc.

2001-12-13  Neal H Walfield  <address@hidden>

        * Makefile.in (diff_o): Add xmalloc.o.
        (diff3_o): Likewise.
        (sdiff_o): Likewise.

        * diff.h: Move declarations of xmalloc and xrealloc from
        here . . .
        * xmalloc.h: . . . to this new file.
        
        * analyze.c: Add #include "xmalloc.h" for xmalloc and xrealloc
        declarations.
        * cmp.c: Likewise.
        * diff.c: Likewise.
        * diff3.c: Likewise.
        * dir.c: Likewise.
        * io.c: Likewise.
        * sdiff.c: Likewise.
        * util.c: Likewise.

        * cmp.c: Remove the now redundant xmalloc declaration.
        * diff3.c: Remove the now redunant xmalloc and xrealloc
        declarations.
        * sdiff.c: Remove the redundant xmalloc declaration.

        * diff3.c (xmalloc): Removed; superseded by xmalloc.c.
        (xrealloc): Likewise.
        * sdiff.c (xmalloc): Likewise.
        * util.c (xmalloc): Likewise.
        (xrealloc): Likewise.

        * sdiff.c (diffarg): Use xrealloc, not realloc.


diff -upr diffutils-2.7.orig/Makefile.in diffutils-2.7/Makefile.in
--- diffutils-2.7.orig/Makefile.in      Thu Sep 22 18:47:00 1994
+++ diffutils-2.7/Makefile.in   Thu Dec 13 22:53:05 2001
@@ -1,5 +1,5 @@
 # Makefile for GNU DIFF
-# Copyright (C) 1988,1989,1991,1992,1993,1994 Free Software Foundation, Inc.
+# Copyright (C) 1988,89,91,92,93,94,2001 Free Software Foundation, Inc.
 #
 # This file is part of GNU DIFF.
 #
@@ -79,15 +79,16 @@ COMPILE = $(CC) -c $(CPPFLAGS) $(DEFS) -
 
 diff_o = diff.o analyze.o cmpbuf.o dir.o io.o util.o \
        context.o ed.o ifdef.o normal.o side.o \
-       fnmatch.o getopt.o getopt1.o regex.o version.o $(ALLOCA) $(LIBOBJS)
+       fnmatch.o getopt.o getopt1.o regex.o version.o xmalloc.o \
+       $(ALLOCA) $(LIBOBJS)
 diff: $(diff_o)
        $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(diff_o) $(LIBS)
 
-diff3_o = diff3.o getopt.o getopt1.o version.o $(LIBOBJS)
+diff3_o = diff3.o getopt.o getopt1.o version.o xmalloc.o $(LIBOBJS)
 diff3: $(diff3_o)
        $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(diff3_o) $(LIBS)
 
-sdiff_o = sdiff.o getopt.o getopt1.o version.o $(LIBOBJS)
+sdiff_o = sdiff.o getopt.o getopt1.o version.o xmalloc.o $(LIBOBJS)
 sdiff: $(sdiff_o)
        $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(sdiff_o) $(LIBS)
 
diff -upr diffutils-2.7.orig/diff.h diffutils-2.7/diff.h
--- diffutils-2.7.orig/diff.h   Thu Sep 22 18:47:00 1994
+++ diffutils-2.7/diff.h        Thu Dec 13 22:38:59 2001
@@ -309,8 +313,6 @@ void print_rcs_script PARAMS((struct cha
 void print_sdiff_script PARAMS((struct change *));
 
 /* util.c */
-VOID *xmalloc PARAMS((size_t));
-VOID *xrealloc PARAMS((VOID *, size_t));
 char *concat PARAMS((char const *, char const *, char const *));
 char *dir_file_pathname PARAMS((char const *, char const *));
 int change_letter PARAMS((int, int));
diff -upr diffutils-2.7.orig/analyze.c diffutils-2.7/analyze.c
--- diffutils-2.7.orig/analyze.c        Wed Nov 10 09:28:27 1993
+++ diffutils-2.7/analyze.c     Thu Dec 13 23:25:55 2001
@@ -1,5 +1,5 @@
 /* Analyze file differences for GNU DIFF.
-   Copyright (C) 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
+   Copyright (C) 1988, 89, 92, 93, 2001 Free Software Foundation, Inc.
 
 This file is part of GNU DIFF.
 
@@ -32,6 +32,7 @@ the Free Software Foundation, 675 Mass A
 
 #include "diff.h"
 #include "cmpbuf.h"
+#include "xmalloc.h"
 
 extern int no_discards;
 
diff -upr diffutils-2.7.orig/cmp.c diffutils-2.7/cmp.c
--- diffutils-2.7.orig/cmp.c    Thu Sep 22 18:47:00 1994
+++ diffutils-2.7/cmp.c Thu Dec 13 22:47:33 2001
@@ -1,5 +1,5 @@
 /* cmp -- compare two files.
-   Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
+   Copyright (C) 1990, 91, 92, 93, 94, 2001 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -21,6 +21,7 @@
 #include <stdio.h>
 #include "getopt.h"
 #include "cmpbuf.h"
+#include "xmalloc.h"
 
 extern char const version_string[];
 
@@ -29,7 +30,6 @@ void error (int, int, char const *, ...)
 #else
 void error ();
 #endif
-VOID *xmalloc PARAMS((size_t));
 
 static int cmp PARAMS((void));
 static off_t file_position PARAMS((int));
diff -upr diffutils-2.7.orig/diff.c diffutils-2.7/diff.c
--- diffutils-2.7.orig/diff.c   Thu Sep 22 18:47:00 1994
+++ diffutils-2.7/diff.c        Thu Dec 13 22:40:24 2001
@@ -1,5 +1,5 @@
 /* GNU DIFF main routine.
-   Copyright (C) 1988, 1989, 1992, 1993, 1994 Free Software Foundation, Inc.
+   Copyright (C) 1988, 1989, 1992, 1993, 1994, 2001 Free Software Foundation, 
Inc.
 
 This file is part of GNU DIFF.
 
@@ -25,6 +25,7 @@ the Free Software Foundation, 675 Mass A
 #include <signal.h>
 #include "getopt.h"
 #include "fnmatch.h"
+#include "xmalloc.h"
 
 #ifndef DEFAULT_WIDTH
 #define DEFAULT_WIDTH 130
diff -upr diffutils-2.7.orig/diff3.c diffutils-2.7/diff3.c
--- diffutils-2.7.orig/diff3.c  Sat Oct  1 06:14:15 1994
+++ diffutils-2.7/diff3.c       Thu Dec 13 22:43:58 2001
@@ -1,5 +1,5 @@
 /* Three way file comparison program (diff3) for Project GNU.
-   Copyright (C) 1988, 1989, 1992, 1993, 1994 Free Software Foundation, Inc.
+   Copyright (C) 1988, 89, 92, 93, 94, 2001 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -21,6 +21,7 @@
 #include <stdio.h>
 #include <signal.h>
 #include "getopt.h"
+#include "xmalloc.h"
 
 extern char const version_string[];
 
@@ -175,9 +176,6 @@ static int merge;
 
 static char *program_name;
 
-static VOID *xmalloc PARAMS((size_t));
-static VOID *xrealloc PARAMS((VOID *, size_t));
-
 static char *read_diff PARAMS((char const *, char const *, char **));
 static char *scan_diff_line PARAMS((char *, char **, size_t *, char *, int));
 static enum diff_type process_diff_control PARAMS((char **, struct diff_block 
*));
@@ -1734,27 +1732,6 @@ myread (fd, ptr, size)
   size_t result = read (fd, ptr, size);
   if (result == -1)
     perror_with_exit ("read failed");
-  return result;
-}
-
-static VOID *
-xmalloc (size)
-     size_t size;
-{
-  VOID *result = (VOID *) malloc (size ? size : 1);
-  if (!result)
-    fatal ("memory exhausted");
-  return result;
-}
-
-static VOID *
-xrealloc (ptr, size)
-     VOID *ptr;
-     size_t size;
-{
-  VOID *result = (VOID *) realloc (ptr, size ? size : 1);
-  if (!result)
-    fatal ("memory exhausted");
   return result;
 }
 
diff -upr diffutils-2.7.orig/dir.c diffutils-2.7/dir.c
--- diffutils-2.7.orig/dir.c    Mon Sep 12 17:52:22 1994
+++ diffutils-2.7/dir.c Thu Dec 13 22:41:22 2001
@@ -1,5 +1,5 @@
 /* Read, sort and compare two directories.  Used for GNU DIFF.
-   Copyright (C) 1988, 1989, 1992, 1993, 1994 Free Software Foundation, Inc.
+   Copyright (C) 1988, 89, 92, 93, 94, 2001 Free Software Foundation, Inc.
 
 This file is part of GNU DIFF.
 
@@ -18,6 +18,7 @@ along with GNU DIFF; see the file COPYIN
 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 #include "diff.h"
+#include "xmalloc.h"
 
 /* Read the directory named by DIR and store into DIRDATA a sorted vector
    of filenames for its contents.  DIR->desc == -1 means this directory is
diff -upr diffutils-2.7.orig/io.c diffutils-2.7/io.c
--- diffutils-2.7.orig/io.c     Thu Sep 22 18:47:00 1994
+++ diffutils-2.7/io.c  Thu Dec 13 22:41:48 2001
@@ -1,5 +1,5 @@
 /* File I/O for GNU DIFF.
-   Copyright (C) 1988, 1989, 1992, 1993, 1994 Free Software Foundation, Inc.
+   Copyright (C) 1988, 89, 92, 93, 94, 2001 Free Software Foundation, Inc.
 
 This file is part of GNU DIFF.
 
@@ -18,6 +18,7 @@ along with GNU DIFF; see the file COPYIN
 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 #include "diff.h"
+#include "xmalloc.h"
 
 /* Rotate a value n bits to the left. */
 #define UINT_BIT (sizeof (unsigned) * CHAR_BIT)
diff -upr diffutils-2.7.orig/sdiff.c diffutils-2.7/sdiff.c
--- diffutils-2.7.orig/sdiff.c  Sat Oct  1 06:14:15 1994
+++ diffutils-2.7/sdiff.c       Thu Dec 13 23:14:50 2001
@@ -1,5 +1,5 @@
 /* SDIFF -- interactive merge front end to diff
-   Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
+   Copyright (C) 1992, 1993, 1994, 2001 Free Software Foundation, Inc.
 
 This file is part of GNU DIFF.
 
@@ -23,6 +23,7 @@ the Free Software Foundation, 675 Mass A
 #include <stdio.h>
 #include <signal.h>
 #include "getopt.h"
+#include "xmalloc.h"
 
 /* Size of chunks read from files which must be parsed into lines. */
 #define SDIFF_BUFSIZE ((size_t) 65536)
@@ -54,7 +55,6 @@ struct line_filter;
 
 static FILE *ck_fopen PARAMS((char const *, char const *));
 static RETSIGTYPE catchsig PARAMS((int));
-static VOID *xmalloc PARAMS((size_t));
 static char const *expand_name PARAMS((char *, int, char const *));
 static int edit PARAMS((struct line_filter *, int, struct line_filter *, int, 
FILE*));
 static int interact PARAMS((struct line_filter *, struct line_filter *, struct 
line_filter *, FILE*));
@@ -204,18 +204,6 @@ perror_fatal (msg)
   exiterr ();
 }
 
-
-/* malloc freely or DIE! */
-static VOID *
-xmalloc (size)
-     size_t size;
-{
-  VOID *r = (VOID *) malloc (size);
-  if (!r)
-    fatal ("memory exhausted");
-  return r;
-}
-
 static FILE *
 ck_fopen (fname, type)
      char const *fname, *type;
@@ -652,8 +640,8 @@ diffarg (a)
          diffargsmax = 8;
        }
       diffargsmax *= 2;
-      diffargv = (char const **) realloc (diffargv,
-                                         diffargsmax * sizeof (char const *));
+      diffargv = (char const **) xrealloc (diffargv,
+                                          diffargsmax * sizeof (char const *));
       if (! diffargv)
        fatal ("out of memory");
     }
diff -upr diffutils-2.7.orig/util.c diffutils-2.7/util.c
--- diffutils-2.7.orig/util.c   Sat Oct  1 06:14:15 1994
+++ diffutils-2.7/util.c        Thu Dec 13 22:42:19 2001
@@ -1,5 +1,5 @@
 /* Support routines for GNU DIFF.
-   Copyright (C) 1988, 1989, 1992, 1993, 1994 Free Software Foundation, Inc.
+   Copyright (C) 1988, 89, 92, 93, 94, 2001 Free Software Foundation, Inc.
 
 This file is part of GNU DIFF.
 
@@ -18,6 +18,7 @@ along with GNU DIFF; see the file COPYIN
 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 #include "diff.h"
+#include "xmalloc.h"
 
 #ifndef PR_PROGRAM
 #define PR_PROGRAM "/bin/pr"
@@ -682,42 +678,6 @@ analyze_hunk (hunk, first0, last0, first
   *inserts = show_to;
 }
 
-/* malloc a block of memory, with fatal error message if we can't do it. */
-
-VOID *
-xmalloc (size)
-     size_t size;
-{
-  register VOID *value;
-
-  if (size == 0)
-    size = 1;
-
-  value = (VOID *) malloc (size);
-
-  if (!value)
-    fatal ("memory exhausted");
-  return value;
-}
-
-/* realloc a block of memory, with fatal error message if we can't do it. */
-
-VOID *
-xrealloc (old, size)
-     VOID *old;
-     size_t size;
-{
-  register VOID *value;
-
-  if (size == 0)
-    size = 1;
-
-  value = (VOID *) realloc (old, size);
-
-  if (!value)
-    fatal ("memory exhausted");
-  return value;
-}
 
 /* Concatenate three strings, returning a newly malloc'd string.  */

diff -uprN diffutils-2.7.orig/xmalloc.h diffutils-2.7/xmalloc.h
--- diffutils-2.7.orig/xmalloc.h        Thu Jan  1 01:00:00 1970
+++ diffutils-2.7/xmalloc.h     Thu Dec 13 22:39:36 2001
@@ -0,0 +1,21 @@
+/* xmalloc definitions for GNU DIFF
+   Copyright (C) 2001 Free Software Foundation, Inc.
+
+This file is part of GNU DIFF.
+
+GNU DIFF is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU DIFF is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU DIFF; see the file COPYING.  If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+
+VOID *xmalloc PARAMS((size_t));
+VOID *xrealloc PARAMS((VOID *, size_t));



 



reply via email to

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