Author: cazfi
Date: Thu Dec 31 23:31:45 2015
New Revision: 31277

URL: http://svn.gna.org/viewcvs/freeciv?rev=31277&view=rev
Log:
Check sanity of zero-size malloc() configure time, and leave out the avoidance 
code
against it from fc_malloc() if such code is not needed.

See patch #6736

Modified:
    trunk/configure.ac
    trunk/utility/mem.c

Modified: trunk/configure.ac
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/configure.ac?rev=31277&r1=31276&r2=31277&view=diff
==============================================================================
--- trunk/configure.ac  (original)
+++ trunk/configure.ac  Thu Dec 31 23:31:45 2015
@@ -83,6 +83,27 @@
 dnl Sed is needed for the configuration
 dnl of the clients, database setting and the mapimg toolkits.
 AC_PROG_SED
+
+AC_CACHE_CHECK([for malloc(0) return value], [fc_cv_malloc_zero_ok], [
+AC_RUN_IFELSE([AC_LANG_SOURCE([[
+#include <stdlib.h>
+
+int
+main()
+{
+  void *allocation = malloc(0);
+
+  if (allocation != NULL) {
+    free(allocation);
+  }
+
+  return (allocation == NULL);
+}
+]])], [fc_cv_malloc_zero_ok=true], [fc_cv_malloc_zero_ok=false], 
[fc_cv_malloc_zero_ok=false])])
+
+if test "x$fc_cv_malloc_zero_ok" = "xtrue" ; then
+  AC_DEFINE([MALLOC_ZERO_OK], [1], [It's ok to call malloc() for zero bytes])
+fi
 
 dnl set default values
 fcdb_all=no

Modified: trunk/utility/mem.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/utility/mem.c?rev=31277&r1=31276&r2=31277&view=diff
==============================================================================
--- trunk/utility/mem.c (original)
+++ trunk/utility/mem.c Thu Dec 31 23:31:45 2015
@@ -70,7 +70,7 @@
   always return a valid pointer (even for a 0-byte malloc).
 
*******************************************************************************/
 
 void *fc_real_malloc(size_t size,
-                    const char *called_as, int line, const char *file)
+                     const char *called_as, int line, const char *file)
 {
   void *ptr;
 
@@ -79,12 +79,15 @@
   /* Some systems return NULL on malloc(0)
    * According to ANSI C, the return is implementation-specific, 
    * this is a safe guard. Having the extra byte is, of course, harmless. */
+#ifndef MALLOC_ZERO_OK
   size = MAX(size, 1);
-    
+#endif
+
   ptr = malloc(size);
-  if (!ptr) {
+  if (ptr == NULL) {
     handle_alloc_failure(size, called_as, line, file);
   }
+
   return ptr;
 }
 


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to