[poppler] Branch 'poppler-0.6' - goo/gmem.cc goo/gmem.h

Albert Astals Cid aacid at kemper.freedesktop.org
Mon Oct 22 12:57:45 PDT 2007


 goo/gmem.cc |   18 ++++++++++++++++++
 goo/gmem.h  |    3 +++
 2 files changed, 21 insertions(+)

New commits:
commit 73a1616c027f66863e8b2e84227191ae7cfd0353
Author: Albert Astals Cid <tsdgeos at bluebox.localdomain>
Date:   Mon Oct 22 21:57:09 2007 +0200

    Add gmallocn_checkoverflow, it's the same as gmallocn but returns NULL on overflow instead of doing exit()

diff --git a/goo/gmem.cc b/goo/gmem.cc
index f1f8f5f..3dce8ea 100644
--- a/goo/gmem.cc
+++ b/goo/gmem.cc
@@ -172,6 +172,24 @@ void *gmallocn(int nObjs, int objSize) GMEM_EXCEP {
   return gmalloc(n);
 }
 
+void *gmallocn_checkoverflow(int nObjs, int objSize) GMEM_EXCEP {
+  int n;
+
+  if (nObjs == 0) {
+    return NULL;
+  }
+  n = nObjs * objSize;
+  if (objSize <= 0 || nObjs < 0 || nObjs >= INT_MAX / objSize) {
+#if USE_EXCEPTIONS
+    throw GMemException();
+#else
+    fprintf(stderr, "Bogus memory allocation size\n");
+    return NULL;
+#endif
+  }
+  return gmalloc(n);
+}
+
 void *greallocn(void *p, int nObjs, int objSize) GMEM_EXCEP {
   int n;
 
diff --git a/goo/gmem.h b/goo/gmem.h
index ebe1455..39c2334 100644
--- a/goo/gmem.h
+++ b/goo/gmem.h
@@ -49,8 +49,11 @@ extern void *grealloc(void *p, size_t size) GMEM_EXCEP;
  * and size.  The result is similar to allocating nObjs * objSize
  * bytes, but there is an additional error check that the total size
  * doesn't overflow an int.
+ * The gmallocn_checkoverflow variant returns NULL instead of exiting
+ * the application if a overflow is detected
  */
 extern void *gmallocn(int nObjs, int objSize) GMEM_EXCEP;
+extern void *gmallocn_checkoverflow(int nObjs, int objSize) GMEM_EXCEP;
 extern void *greallocn(void *p, int nObjs, int objSize) GMEM_EXCEP;
 
 /*


More information about the poppler mailing list