Error handler for GMP?
Marco Bodrato
bodrato at mail.dm.unipi.it
Sat Jun 12 14:49:18 UTC 2021
Ciao John! È un piacere vederti da queste parti!
Il 2021-03-22 09:55 abbott at dima.unige.it ha scritto:
> Does GMP offer a way to return/throw rather than simply aborting upon
> overflow?
No, it doesn't. Yet.
> I could not see anything relevant in the documentation.
The theme emerges every now and then on the list.
And the problem is that it seems difficult, for the caller, to handle
possible memory leaks, and coherence of the internal state to do
anything better than to abort...
But I think that lazy allocation, we have it from GMP-6.2.0, can help.
I think it should be possible to write a function, using mpz_t or mpq_t
only, that:
1) has a clear distinction between readable-only and writable-only
parameters.
2) frees (mpz_clear) all the writable variables, and re-inits them
(mpz_init, which is lazy now, and does not allocate anything).
3) sets (mp_set_memory_functions) memory functions that will keep track
of any block allocated by GMP functions
4) sets the callback on error...
5) does the heavy work
6) resets callback and memory functions and returns "done".
If during the step 5 an error occours:
6) frees any memory block allocated after step 3;
7) re-inits all the writable variables (mpz_init, to set everything to a
coherent state)
8) resets callback and memory functions and returns "fail".
Anyway, for internal coherence, I think that we should at least check
that all the functions in the library that might "abort" do this using
an "exception". For this, I propose the attached patch.
Then, we might think if we want to let the user install a hook on the
__gmp_exception function.
Ĝis,
m
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mpz_overflow.diff
Type: text/x-diff
Size: 2869 bytes
Desc: not available
URL: <https://gmplib.org/list-archives/gmp-devel/attachments/20210612/a67520a5/attachment-0001.bin>
More information about the gmp-devel
mailing list