Author: cazfi
Date: Thu Dec 31 23:31:51 2015
New Revision: 31278

URL: http://svn.gna.org/viewcvs/freeciv?rev=31278&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:
    branches/S2_6/configure.ac
    branches/S2_6/utility/mem.c

Modified: branches/S2_6/configure.ac
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/configure.ac?rev=31278&r1=31277&r2=31278&view=diff
==============================================================================
--- branches/S2_6/configure.ac  (original)
+++ branches/S2_6/configure.ac  Thu Dec 31 23:31:51 2015
@@ -80,6 +80,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: branches/S2_6/utility/mem.c
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/utility/mem.c?rev=31278&r1=31277&r2=31278&view=diff
==============================================================================
--- branches/S2_6/utility/mem.c (original)
+++ branches/S2_6/utility/mem.c Thu Dec 31 23:31:51 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