Avoid future chance of out-of-bounds error #540
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Regarding issue #529: do not assume that more than enough memory is automatically allocated and set to
0.The old code assumes that there is always enough memory allocated to go one further than
x.dp[x.used - 1]andthat this memory is set to zero. This can lead to surprises if e.g.: heap is short but not short enough to use tomsfastmath and the developer decides to pepper the code with
mp_shrink()to give the OS a chance to reclaim memory. If that is done ins_mp_sub()the assumptions fail and either an out-of-bounds error is caught or memory read with random content which can be zero. Depending on the input. And the compiler.It is not much, but let's be nice to the future developers.
Cost involved: one
mp_digit, its assignment and one branch (a ternary).This fixes #529