Tim Ruehsen tim.ruehsen@gmx.de writes:
Am Friday 14 September 2012 schrieb Niels Möller:
If we need a workaround, the right place is in the realloc wrapppers in realloc.c.
We need a workaround since different libraries handle it differently. But simplified it is just a if (p) free(p);
I think something like this (untested) should solve the problem. We should also document somewhere that buffer->realloc(p, 0) is expected to free the storage completely.
--- a/realloc.c +++ b/realloc.c @@ -34,13 +34,25 @@ void * nettle_realloc(void *ctx UNUSED, void *p, unsigned length) { + if (length == 0) + { + free (p); + return NULL; + } return realloc(p, length); }
void * nettle_xrealloc(void *ctx UNUSED, void *p, unsigned length) { - void *n = realloc(p, length); - if (length && !n) + void *n; + if (length == 0) + { + free (p); + return NULL; + } + + n = realloc(p, length); + if (!n) { fprintf(stderr, "Virtual memory exhausted.\n");
You could tell which points you would like to have changed, and i'll change them here. After we are done, the patch could be applied in whole. I'll do the coding and you the review...
Thanks. Then I'd like you to try to keep the interface of the HL, H, LDATA, LDUP macros intact, so you don't need to modify each and every test case, but add whatever additional machinery is needed to avoid memory leaks (linked list of allocated strings, GNU obstack, allocation from a static area, or whatever seems suitable). For the test files, I don't think it's a problem if we keep the allocations a bit longer than necessary. If that approach gets too painful, we'll have to consider some other way.
I prefer to do things in small pieces, so if I get to it before the rest of the fixes are ready, I'll check in missing mpz_clear and similar.
Regards, /Niels