Package: qiv
Version: 2.0-4.2
Severity: minor
Tags: patch

Many of the calls to malloc() and realloc() are not checked for a NULL
return value, and are subsequently used under the assumption that a
valid pointer was returned.  In the (addmittedly unlikely) case that
the system is unable to allocate the memory, qiv quits somewhat less
graciously than would be desirable.

I have attached my stock do-or-die memory handling functions (public
domain, but easily relicensable to GPL if desired), and a patch to
incorporate those functions into qiv.  But, of course, it would be
just as easy to check the return of malloc at the call point or to
spin your own xmalloc...

I realize that the change is rather nitpicky, my appologies, but I
still think it could stand to be fixed.  ;)

Cheers,
Kevin

-- System Information:
Debian Release: testing/unstable
  APT prefers testing
  APT policy: (990, 'testing'), (500, 'unstable')
Architecture: i386 (i686)
Shell:  /bin/sh linked to /bin/bash
Kernel: Linux 2.6.17.20060719a
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)

Versions of packages qiv depends on:
ii  gdk-imlib11                  1.9.14-30   imaging library for use with gtk
ii  libc6                        2.3.6-19    GNU C Library: Shared libraries
ii  libglib1.2                   1.2.10-10.1 The GLib library of C routines
ii  libgtk1.2                    1.2.10-18   The GIMP Toolkit set of widgets fo
ii  libx11-6                     2:1.0.0-7   X11 client-side library
ii  libxext6                     1:1.0.0-4   X11 miscellaneous extension librar
ii  libxi6                       1:1.0.0-5   X11 Input extension library
ii  libxinerama1                 1:1.0.1-4   X11 Xinerama extension library

qiv recommends no packages.

-- no debconf information
diff -ur qiv-2.0.orig/image.c qiv-2.0/image.c
--- qiv-2.0.orig/image.c	2006-08-14 15:08:45.000000000 -0600
+++ qiv-2.0/image.c	2006-08-14 21:55:29.000000000 -0600
@@ -14,6 +14,7 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include "qiv.h"
+#include "xmalloc.h"
 
 static void setup_win(qiv_image *);
 static void really_set_static_grav(GdkWindow *);
@@ -230,7 +231,7 @@
     gdk_window_set_back_pixmap(root_win, q->p, FALSE);
   } else {
     GdkGC *rootGC;
-    buffer = calloc(1, screen_x * screen_y);
+    buffer = xcalloc(1, screen_x * screen_y);
     rootGC = gdk_gc_new(root_win);
     temp = gdk_pixmap_create_from_data(root_win, buffer, screen_x, 
 	       screen_y, gvis->depth, &image_bg, &image_bg);
diff -ur qiv-2.0.orig/Makefile qiv-2.0/Makefile
--- qiv-2.0.orig/Makefile	2006-08-14 15:08:45.000000000 -0600
+++ qiv-2.0/Makefile	2006-08-14 21:55:29.000000000 -0600
@@ -66,7 +66,7 @@
 LIBS      = `imlib-config --libs-gdk`
 
 PROGRAM   = qiv
-OBJS      = main.o image.o event.o options.o utils.o
+OBJS      = main.o image.o event.o options.o utils.o xmalloc.o
 HEADERS   = qiv.h
 DEFINES   = $(patsubst %,-DEXTN_%, $(EXTNS)) \
             $(GETOPT_LONG) \
diff -ur qiv-2.0.orig/Makefile.Solaris qiv-2.0/Makefile.Solaris
--- qiv-2.0.orig/Makefile.Solaris	2006-08-14 15:08:45.000000000 -0600
+++ qiv-2.0/Makefile.Solaris	2006-08-14 21:55:55.000000000 -0600
@@ -66,7 +66,7 @@
 LIBS      = `imlib-config --libs-gdk` -R`imlib-config --prefix`/lib
 
 PROGRAM   = qiv
-OBJS      = main.o image.o event.o options.o utils.o
+OBJS      = main.o image.o event.o options.o utils.o xmalloc.o
 HEADERS   = qiv.h
 DEFINES   = $(patsubst %,-DEXTN_%, $(EXTNS)) \
             $(GETOPT_LONG) \
diff -ur qiv-2.0.orig/options.c qiv-2.0/options.c
--- qiv-2.0.orig/options.c	2006-08-14 15:08:45.000000000 -0600
+++ qiv-2.0/options.c	2006-08-14 21:55:29.000000000 -0600
@@ -16,6 +16,7 @@
 #else
 #include "lib/getopt.h"
 #endif
+#include "xmalloc.h"
 
 extern char *optarg;
 extern int optind, opterr, optopt;
@@ -297,7 +298,7 @@
     if((cnt = argc - optind) > 0) {
         if (!images) {
             max_image_cnt = 8192;
-            image_names = (char**)malloc(max_image_cnt * sizeof(char*));
+            image_names = (char**)xmalloc(max_image_cnt * sizeof(char*));
         }
         while (cnt-- > 0) {
             if (stat(argv[optind], &sb) >= 0 && S_ISDIR(sb.st_mode)) {
@@ -307,7 +308,7 @@
             else {
                 if (images >= max_image_cnt) {
                     max_image_cnt += 8192;
-                    image_names = (char**)realloc(image_names,max_image_cnt*sizeof(char*));
+                    image_names = (char**)xrealloc(image_names,max_image_cnt*sizeof(char*));
                 }
                 image_names[images++] = argv[optind++];
             }
diff -ur qiv-2.0.orig/utils.c qiv-2.0/utils.c
--- qiv-2.0.orig/utils.c	2006-08-14 15:08:45.000000000 -0600
+++ qiv-2.0/utils.c	2006-08-14 21:55:29.000000000 -0600
@@ -17,6 +17,7 @@
 #include <fcntl.h>
 #include <errno.h>
 #include "qiv.h"
+#include "xmalloc.h"
 
 #ifdef STAT_MACROS_BROKEN
 #undef S_ISDIR
@@ -86,7 +87,7 @@
     qiv_deletedfile *del;
 
     if (!deleted_files)
-      deleted_files = (qiv_deletedfile*)calloc(MAX_DELETE,sizeof *deleted_files);
+      deleted_files = (qiv_deletedfile*)xcalloc(MAX_DELETE,sizeof *deleted_files);
 
     del = &deleted_files[delete_idx++];
     if (delete_idx == MAX_DELETE)
@@ -248,7 +249,7 @@
   stat(filename, &before);
 
   if (!buffer) 
-    buffer = malloc(MAXOUTPUTBUFFER + 1);
+    buffer = xmalloc(MAXOUTPUTBUFFER + 1);
 
   *numlines = 0;
   *output = lines;
@@ -503,7 +504,7 @@
   int n,m,p,q;
 
   if (!rindices)
-    rindices = (int *) malloc((unsigned) max_rand_num*sizeof(int));
+    rindices = (int *) xmalloc((unsigned) max_rand_num*sizeof(int));
   if (rsize != num) {
     rsize = num;
     index = -1;
@@ -558,9 +559,9 @@
 	    if (images >= max_image_cnt) {
 		max_image_cnt += 8192;
 		if (!image_names)
-		    image_names = (char**)malloc(max_image_cnt * sizeof(char*));
+		    image_names = (char**)xmalloc(max_image_cnt * sizeof(char*));
 		else
-		    image_names = (char**)realloc(image_names,max_image_cnt*sizeof(char*));
+		    image_names = (char**)xrealloc(image_names,max_image_cnt*sizeof(char*));
 	    }
 	    image_names[images++] = strdup(name);
 	}
@@ -581,7 +582,7 @@
 
 	if (!images) {
 		max_image_cnt = 8192;
-		image_names = (char**)malloc(max_image_cnt * sizeof(char*));
+		image_names = (char**)xmalloc(max_image_cnt * sizeof(char*));
 	}
 
 	while (1) {
@@ -603,7 +604,7 @@
 		else {
 			if (images >= max_image_cnt) {
 				max_image_cnt += 8192;
-				image_names = (char**)realloc(image_names,max_image_cnt*sizeof(char*));
+				image_names = (char**)xrealloc(image_names,max_image_cnt*sizeof(char*));
 			}
 			image_names[images++] = strdup(line);
 		}
/* xmalloc.c - Do-or-die Memory management functions.
 * 
 * Created by Kevin Locke (from numerous canonical examples)
 *
 * I hereby place this file in the public domain.  It may be freely reproduced,
 * distributed, used, modified, built upon, or otherwise employed by anyone
 * for any purpose without restriction.
 */

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>

#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
#endif

void *xmalloc(size_t size)
{
	void *allocated = malloc(size);

	if (allocated == NULL) {
		fprintf(stderr, "Error:  Insufficient memory "
# if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
				"(attempt to malloc %zu bytes)\n",
#else
				"(attempt to malloc %u bytes)\n",
#endif
				 size);
		exit(EXIT_FAILURE);
	}

	return allocated;
}

void *xcalloc(size_t num, size_t size)
{
	void *allocated = calloc(num, size);

	if (allocated == NULL) {
		fprintf(stderr, "Error:  Insufficient memory "
# if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
				"(attempt to calloc %zu bytes)\n",
#else
				"(attempt to calloc %u bytes)\n",
#endif
				 size);
		exit(EXIT_FAILURE);
	}

	return allocated;
}

void *xrealloc(void *ptr, size_t size)
{
	void *allocated;

	/* Protect against non-standard behavior */
	if (ptr == NULL) {
		allocated = malloc(size);
	} else {
		allocated = realloc(ptr, size);
	}

	if (allocated == NULL) {
		fprintf(stderr, "Error:  Insufficient memory "
# if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
				"(attempt to realloc %zu bytes)\n",
#else
				"(attempt to realloc %u bytes)\n",
#endif
				 size);
		exit(EXIT_FAILURE);
	}

	return allocated;
}
/* xmalloc.h - Header for do-or-die memory management functions.
 * 
 * Created by Kevin Locke (to accompany xmalloc.c)
 *
 * I hereby place this file in the public domain.  It may be freely reproduced,
 * distributed, used, modified, built upon, or otherwise employed by anyone
 * for any purpose without restriction.
 */

extern void *xmalloc(size_t size);
extern void *xcalloc(size_t num, size_t size);
extern void *xrealloc(void *ptr, size_t size);

Reply via email to