Fixing a long standing bug in view.c



view.c erroneously assumes it can easily malloc a file if mmap failed and
passes view->s.st_size to g_malloc. But if largefile support has been enabled,
view->s.st_size is a 64 bit value and thus possibly exceeds the limit of
gulong which g_malloc takes for size. This patch fixes it.

2003-02-26  Philipp Thomas  <pthomas suse de>

	* view.c(load_view_file): Check file size doesn't exceed limit
	before passing it to g_malloc.

--- src/view.c
+++ src/view.c
@@ -585,11 +585,24 @@
     }
 #endif				/* HAVE_MMAP */
 
-    /* For those OS that dont provide mmap call. Try to load all the
+    /* For those OS's that don't provide mmap call. Try to load all the
      * file into memory (alex bcs zaporizhzhe ua). Also, mmap can fail
      * for any reason, so we use this as fallback (pavel ucw cz) */
 
-    view->data = (unsigned char *) g_malloc (view->s.st_size);
+    /* If large file support is enabled, s.st_size is a 64 bit value and
+     * g_malloc can't handle file sizes > ULONG_MAX */
+
+#if GLIB_MAJOR_VERSION < 2
+#  define MC_ULONG_MAX ULONG_MAX
+#else
+#  define MC_ULONG_MAX G_MAXULONG
+#endif
+
+    if  (sizeof(off_t) <= sizeof(gulong) || view->s.st_size < MC_ULONG_MAX)
+	view->data = (unsigned char*) g_malloc (view->s.st_size);
+    else
+	view->data = NULL;
+
     if (view->data == NULL
 	|| mc_lseek (view->file, 0, SEEK_SET) != 0
 	|| mc_read (view->file, view->data,

-- 
Philipp Thomas <pthomas suse de>
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nuremberg, Germany



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