On Sat, 16 Feb 2002, Steve Fink wrote:

> > -      key->keys = (KEY_PAIR*)realloc(key->keys,sizeof(KEY_PAIR)*size);
> > +      key->keys = (KEY_PAIR**)realloc(key->keys,sizeof(KEY_PAIR)*size);
>
> That seems rather suspicious. I don't know anything about the KEY_PAIR
> type, but allocating a chunk of memory big enough to fit N structures
> and then using it as an array of pointers to individual structures...
> well, something's fishy.
>

key here is a KEY :

struct _key {
  INTVAL size;
  KEY_PAIR** keys;
};

typedef struct _key KEY


So the cast to 'KEY_PAIR **' is valid - however I have a feeling that what
might really be wanted on the RHS is 'sizeof(KEY_PAIR *)*size' - of course
all that is happening if that is the case is that too much memory is being
allocated to store the KEY_PAIR structures which is undesireable but not a
showstopper :)

The patch for key.c then would become :


Index: key.c
===================================================================
RCS file: /home/perlcvs/parrot/key.c,v
retrieving revision 1.19
diff -u -r1.19 key.c
--- key.c       11 Feb 2002 17:59:11 -0000      1.19
+++ key.c       17 Feb 2002 09:13:12 -0000
@@ -180,10 +180,10 @@
       return;
     }
     if(size > key->size) {
-      KEY_PAIR* pair = (KEY_PAIR*)realloc(key->keys,sizeof(KEY_PAIR)*size);
+      KEY_PAIR* pair = (KEY_PAIR*)realloc(key->keys,sizeof(KEY_PAIR *)*size);
       if(pair != NULL) {
        INTVAL i;
-        key->keys = pair;
+        key->keys = (KEY_PAIR **)pair;
        for(i=key->size;i<size;i++) {
          key->keys[i]->type = enum_key_undef;
        }
@@ -197,7 +197,7 @@
       for(i=size;i<key->size;i++) {
         /* Memory leak in the making */
       }
-      key->keys = (KEY_PAIR*)realloc(key->keys,sizeof(KEY_PAIR)*size);
+      key->keys = (KEY_PAIR**)realloc(key->keys,sizeof(KEY_PAIR *)*size);
     }
     key->size = size;
   }
@@ -241,7 +241,7 @@

   if(key != NULL) {
     if((idx >= 0) && (idx < key->size)) {
-      KEY_PAIR* pair = &key->keys[idx];
+      KEY_PAIR* pair = (KEY_PAIR *)&key->keys[idx];
       return pair->type;
     }
     else {
@@ -266,7 +266,7 @@

   if(key != NULL) {
     if((idx >= 0) && (idx < key->size)) {
-      KEY_PAIR* pair = &key->keys[idx];
+      KEY_PAIR* pair = (KEY_PAIR *)&key->keys[idx];
       if(pair != NULL) {
         return pair;
       }
@@ -388,7 +388,7 @@
     if(key->size > 0) {
       /* Memory leak in the making */
       key->size--;
-      key->keys = (KEY_PAIR*)realloc(key->keys,sizeof(KEY_PAIR)*key->size);
+      key->keys = (KEY_PAIR**)realloc(key->keys,sizeof(KEY_PAIR *)*key->size);
     }
     else if(key->size == 0) {
       fprintf(stderr,
@@ -416,7 +416,7 @@

   if(key != NULL) {
     if((idx >= 0) && (idx < key->size)) {
-      KEY_PAIR* pair = &key->keys[idx];
+      KEY_PAIR* pair = (KEY_PAIR *)&key->keys[idx];
       pair->type++;
     }
     else {



/J\
-- 
Jonathan Stowe                      |
<http://www.gellyfish.com>          |      This space for rent
                                    |

Reply via email to