Repository navigation
Use variable-length array instead of malloc/free. - #32
ryancdotorg wants to merge 1 commit into
Conversation
The only use of `malloc`/`free` in tomfastmath is in src/numtheory/fp_prime_random_ex.c, and the size required can be calculated from the function arguments, so we can just use a variable-length automatic array instead. Variable-length automatic arrays are a C99 feature, and TFM already doesn’t compile as C90, so we might as well take advantage of it.
|
There's also #4, which while being quite old at this point, also does away with memory allocation by having a fixed size buffer. |
|
I'd be happy to have that merged instead, though I think the variable length array is more elegant.
…On September 16, 2024 5:59:02 AM GMT+01:00, Richard Levitte ***@***.***> wrote:
There's also #4, which while being quite old at this point, also does away with memory allocation by having a fixed size buffer.
|
|
In the end, nothing stops both from being merged 😉 |
sjaeckel
left a comment
There was a problem hiding this comment.
I agree that the dynamic allocation does not really make sense here.
I don't like VLA's, so I'd propose to use a stack based buffer of maximum size instead.
| unsigned char *tmp, maskAND, maskOR_msb, maskOR_lsb; | ||
| int res, err, bsize, maskOR_msb_offset; | ||
| /* calc the byte size */ | ||
| unsigned char tmp[(size>>3)+(size&7?1:0)]; |
There was a problem hiding this comment.
| unsigned char tmp[(size>>3)+(size&7?1:0)]; | |
| unsigned char tmp[sizeof(a->dp)]; |
Why not simply allocate a statically sized stack array of max size?
This would also require something like the following, after calculating bsize.
if (bsize > sizeof(tmp))
return MP_VAL;
|
Would the use of VLAs diminish the risk of stack overflows? |
FMU the use of VLAs as done here increases the risk of a stack overflow, since there's no upper boundary of Calling this version of the API like this: The upper boundary of With a VLA here the stack usage would indeed be minimized in all cases where |
|
TBH I never checked the max. stack depth of tfm in detail and I just thought that this could become a problem if you increase the To be able to work with RSA16384 keys one has to set One thing I'm not sure of is if |
|
Okie, I understand re VLA. For the rest, I'll try to make time to look at that scratch space to see what's what |
|
Considering all the commentary here, I rebooted #4 → #39. Please have a look at that. @ryancdotorg, I would your commentary there. |
|
I'll try to understand it on Sunday, but TBH it's been a while since I touched it, so I'm not sure how much help I can provide. I'm happy with any solution that avoids |
The only use of
malloc/freein tomfastmath is in src/numtheory/fp_prime_random_ex.c, and the size required can be calculated from the function arguments, so we can just use a variable-length automatic array instead.Variable-length automatic arrays are a C99 feature, and TFM already doesn’t compile as C90, so we might as well take advantage of it.
This is a smaller part of a patchset to allow tomsfastmath to be compiled without stdlib, e.g. for freestanding webassembly.