|
|
15c3a2 |
diff -r e3123b88d012 memory.c
|
|
|
15c3a2 |
--- a/memory.c Tue Aug 16 22:02:45 2022 +0200
|
|
|
15c3a2 |
+++ b/memory.c Fri Aug 19 06:25:37 2022 +0900
|
|
|
15c3a2 |
@@ -29,7 +29,8 @@
|
|
|
15c3a2 |
see https://www.gnu.org/licenses/. */
|
|
|
15c3a2 |
|
|
|
15c3a2 |
#include <stdio.h>
|
|
|
15c3a2 |
-#include <stdlib.h> /* for malloc, realloc, free */
|
|
|
15c3a2 |
+#include <stdlib.h> /* for malloc, free */
|
|
|
15c3a2 |
+#include <string.h> /* for memcpy, explicit_bzero */
|
|
|
15c3a2 |
|
|
|
15c3a2 |
#include "gmp-impl.h"
|
|
|
15c3a2 |
|
|
|
15c3a2 |
@@ -98,11 +99,28 @@
|
|
|
15c3a2 |
new_size += 2 * GMP_LIMB_BYTES;
|
|
|
15c3a2 |
#endif
|
|
|
15c3a2 |
|
|
|
15c3a2 |
- ret = realloc (oldptr, new_size);
|
|
|
15c3a2 |
- if (ret == 0)
|
|
|
15c3a2 |
+ if (new_size == 0)
|
|
|
15c3a2 |
+ {
|
|
|
15c3a2 |
+ explicit_bzero (oldptr, old_size);
|
|
|
15c3a2 |
+ free (oldptr);
|
|
|
15c3a2 |
+ return NULL;
|
|
|
15c3a2 |
+ }
|
|
|
15c3a2 |
+ else if (old_size == new_size)
|
|
|
15c3a2 |
+ return oldptr;
|
|
|
15c3a2 |
+ else
|
|
|
15c3a2 |
{
|
|
|
15c3a2 |
- fprintf (stderr, "GNU MP: Cannot reallocate memory (old_size=%lu new_size=%lu)\n", (long) old_size, (long) new_size);
|
|
|
15c3a2 |
- abort ();
|
|
|
15c3a2 |
+ /* We can't simply call realloc, as it may allocate memory from
|
|
|
15c3a2 |
+ a different arena. */
|
|
|
15c3a2 |
+ ret = malloc (new_size);
|
|
|
15c3a2 |
+ if (ret == NULL)
|
|
|
15c3a2 |
+ {
|
|
|
15c3a2 |
+ fprintf (stderr, "GNU MP: Cannot reallocate memory (old_size=%lu new_size=%lu)\n", (long) old_size, (long) new_size);
|
|
|
15c3a2 |
+ explicit_bzero(oldptr, old_size);
|
|
|
15c3a2 |
+ abort();
|
|
|
15c3a2 |
+ }
|
|
|
15c3a2 |
+ memcpy (ret, oldptr, MIN(old_size, new_size));
|
|
|
15c3a2 |
+ explicit_bzero (oldptr, old_size);
|
|
|
15c3a2 |
+ free (oldptr);
|
|
|
15c3a2 |
}
|
|
|
15c3a2 |
|
|
|
15c3a2 |
#ifdef DEBUG
|
|
|
15c3a2 |
@@ -141,5 +159,6 @@
|
|
|
15c3a2 |
blk_ptr = p - 1;
|
|
|
15c3a2 |
}
|
|
|
15c3a2 |
#endif
|
|
|
15c3a2 |
+ explicit_bzero (blk_ptr, blk_size);
|
|
|
15c3a2 |
free (blk_ptr);
|
|
|
15c3a2 |
}
|