gnugo-devel
[Top][All Lists]
Advanced

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

[gnugo-devel] Re: patch: malloc(0) and array bound violation


From: Teun Burgers
Subject: [gnugo-devel] Re: patch: malloc(0) and array bound violation
Date: Wed, 23 Jun 2004 23:04:43 +0200
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)

I wrote:

The problem with malloc(0) returning 0, leading to assertion failures has not
been fixed to date. For a message on this subject see e.g.

http://lists.gnu.org/archive/html/gnugo-devel/2004-02/msg00037.html


One part of the attached patch (for owl.c) is an update for this problem for 
3.5.8.

This patch was buggy. It contained an excess malloc() invocation.
Revised patch attached.

Teun
diff -u tmp/gnugo-3.5.8/engine/optics.c ./gnugo-3.5.8/engine/optics.c
--- tmp/gnugo-3.5.8/engine/optics.c     2004-06-05 18:27:17.000000000 +0200
+++ ./gnugo-3.5.8/engine/optics.c       2004-06-21 14:09:16.000000000 +0200
@@ -1204,8 +1204,10 @@
       vital = black_vital_points;
     for (k = 0; k < best_vp->num_defenses && k < MAX_EYE_ATTACKS; k++)
       vital[pos].defense_points[k] = best_vp->defenses[k];
-    for (k = 0; k < best_vp->num_attacks; k++)
+    for (k = 0; k < best_vp->num_attacks; k++) {
+      ASSERT1(k < MAX_EYE_ATTACKS, pos);
       vital[pos].attack_points[k] = best_vp->attacks[k];
+    }
   }
 
   return 1;
diff -u tmp/gnugo-3.5.8/engine/owl.c ./gnugo-3.5.8/engine/owl.c
--- tmp/gnugo-3.5.8/engine/owl.c        2004-06-05 18:27:17.000000000 +0200
+++ ./gnugo-3.5.8/engine/owl.c  2004-06-23 12:06:52.000000000 +0200
@@ -3545,8 +3545,8 @@
       count_variations = save_count_variations;
     }
 
-    free(list->pattern_list);
-    free(list->pattern_heap);
+    free(list->pattern_list); list->pattern_list = NULL;
+    free(list->pattern_heap); list->pattern_heap = NULL;
   }
   list->counter = -1;
 }
@@ -3778,8 +3778,14 @@
    * but it is easier to allocate more than to count real number of
    * heap elements first.
    */
-  list->pattern_heap = malloc(list->counter * sizeof(*(list->pattern_heap)));
-  gg_assert(list->pattern_heap != NULL);
+  if (list->counter > 0) {
+    list->pattern_heap = (struct matched_pattern_data**)
+               malloc(list->counter * sizeof(struct matched_pattern_data*));
+    gg_assert(list->pattern_heap != NULL);
+  } else {
+    /* free() has defined behaviour for NULL pointer */
+    list->pattern_heap = NULL;
+  }
 
   for (pos = BOARDMIN; pos < BOARDMAX; pos++)
     list->first_pattern_index[pos] = -1;


reply via email to

[Prev in Thread] Current Thread [Next in Thread]