]> Zhao Yanbai Git Server - minix.git/commitdiff
use malloc() + copy + free() instead of realloc()
authorBen Gras <ben@minix3.org>
Fri, 23 Jun 2006 12:07:41 +0000 (12:07 +0000)
committerBen Gras <ben@minix3.org>
Fri, 23 Jun 2006 12:07:41 +0000 (12:07 +0000)
lib/syslib/safecopies.c

index 01dff96e0b265b0a5dd0087407b069295f48cbc0..e88bf6531ce091a77571d417656aa1395a11a5e5 100644 (file)
@@ -66,19 +66,26 @@ cpf_grow(void)
        new_size = (1+ngrants)*2;
        assert(new_size > ngrants);
 
-       /* Grow block to new size. */
-       if(!(new_grants=realloc(grants, new_size * sizeof(grants[0]))))
+       /* Allocate a block of new size. */
+       if(!(new_grants=malloc(new_size * sizeof(grants[0]))))
                return;
 
+       /* Copy old block to new block. */
+       if(grants && ngrants > 0)
+               memcpy(new_grants, grants, ngrants * sizeof(grants[0]));
+
        /* Make sure new slots are marked unused (CPF_USED is clear). */
        for(g = ngrants; g < new_size; g++)
-               grants[g].cp_flags = 0;
+               new_grants[g].cp_flags = 0;
 
        /* Inform kernel about new size (and possibly new location). */
-       if(sys_privctl(SELF, SYS_PRIV_SET_GRANTS, new_size, new_grants))
+       if(sys_privctl(SELF, SYS_PRIV_SET_GRANTS, new_size, new_grants)) {
+               free(new_grants);
                return; /* Failed - don't grow then. */
+       }
 
        /* Update internal data. */
+       if(grants && ngrants > 0) free(grants);
        grants = new_grants;
        ngrants = new_size;
 }