Size res in BN_div correctly
We allocated two more words than were needed. Sizing it more than
the width is suspicious and with the confusing pointer indirection
removed, it becomes clear that, throughout the entire function, we only
ever write to indices 0 through loop-2. That is, it should be sized for
loop-1.
Bug: 358687140
Change-Id: I9e33ce7d2c4e5b6fae9ec59bdee34b2d3480addc
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/70177
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Bob Beck <bbe@google.com>
diff --git a/crypto/fipsmodule/bn/div.c b/crypto/fipsmodule/bn/div.c
index d05b747..a4c0ded 100644
--- a/crypto/fipsmodule/bn/div.c
+++ b/crypto/fipsmodule/bn/div.c
@@ -267,7 +267,7 @@
// for later.
const int numerator_neg = numerator->neg;
res->neg = (numerator_neg ^ divisor->neg);
- if (!bn_wexpand(res, loop + 1)) {
+ if (!bn_wexpand(res, loop - 1)) {
goto err;
}
res->width = loop - 1;