Skip to content

Commit

Permalink
mpi: Avoid using freed pointer in mpi_lshift_limbs()
Browse files Browse the repository at this point in the history
At the start of the function we assign 'a->d' to 'ap'. Then we use the
RESIZE_IF_NEEDED macro on 'a' - this may free 'a->d' and replace it
with newly allocaetd storage. In that case, we'll be operating on
freed memory further down in the function when we index into 'ap[]'.
Since we don't actually need 'ap' until after the use of the
RESIZE_IF_NEEDED macro we can just delay the assignment to it until
after we've potentially resized, thus avoiding the issue.

While I was there anyway I also changed the integer variable 'n' to be
const. It might as well be since we only assign to it once and use it
as a constant, and then the compiler will tell us if we ever assign to
it in the future.

Signed-off-by: Jesper Juhl <[email protected]>
Acked-by: Dmitry Kasatkin <[email protected]>
Signed-off-by: James Morris <[email protected]>
  • Loading branch information
jjuhl authored and James Morris committed Apr 18, 2012
1 parent 86812bb commit 09c79b6
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/mpi/mpi-bit.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ int mpi_rshift(MPI x, MPI a, unsigned n)
*/
int mpi_lshift_limbs(MPI a, unsigned int count)
{
mpi_ptr_t ap = a->d;
int n = a->nlimbs;
const int n = a->nlimbs;
mpi_ptr_t ap;
int i;

if (!count || !n)
Expand All @@ -187,6 +187,7 @@ int mpi_lshift_limbs(MPI a, unsigned int count)
if (RESIZE_IF_NEEDED(a, n + count) < 0)
return -ENOMEM;

ap = a->d;
for (i = n - 1; i >= 0; i--)
ap[i + count] = ap[i];
for (i = 0; i < count; i++)
Expand Down

0 comments on commit 09c79b6

Please sign in to comment.