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  <[EMAIL PROTECTED]>

        * 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 ([EMAIL PROTECTED]). Also, mmap can fail
      * for any reason, so we use this as fallback ([EMAIL PROTECTED]) */
 
-    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 <[EMAIL PROTECTED]>
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nuremberg, Germany
_______________________________________________
Mc-devel mailing list
[EMAIL PROTECTED]
http://mail.gnome.org/mailman/listinfo/mc-devel

Reply via email to