aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJesper Juhl <jj@chaosbits.net>2012-02-06 04:07:04 -0500
committerJames Morris <james.l.morris@oracle.com>2012-04-17 22:14:28 -0400
commit09c79b60960bdd4b00916219402eabfa5e479c5a (patch)
treed8be1892ca81cbd194ac5e8c198f0f939d432257 /lib
parent86812bb0de1a3758dc6c7aa01a763158a7c0638a (diff)
mpi: Avoid using freed pointer in mpi_lshift_limbs()
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 <jj@chaosbits.net> Acked-by: Dmitry Kasatkin <dmitry.kasatkin@intel.com> Signed-off-by: James Morris <james.l.morris@oracle.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/mpi/mpi-bit.c5
1 files changed, 3 insertions, 2 deletions
diff --git a/lib/mpi/mpi-bit.c b/lib/mpi/mpi-bit.c
index 2f526627e4f5..0c505361da19 100644
--- a/lib/mpi/mpi-bit.c
+++ b/lib/mpi/mpi-bit.c
@@ -177,8 +177,8 @@ int mpi_rshift(MPI x, MPI a, unsigned n)
177 */ 177 */
178int mpi_lshift_limbs(MPI a, unsigned int count) 178int mpi_lshift_limbs(MPI a, unsigned int count)
179{ 179{
180 mpi_ptr_t ap = a->d; 180 const int n = a->nlimbs;
181 int n = a->nlimbs; 181 mpi_ptr_t ap;
182 int i; 182 int i;
183 183
184 if (!count || !n) 184 if (!count || !n)
@@ -187,6 +187,7 @@ int mpi_lshift_limbs(MPI a, unsigned int count)
187 if (RESIZE_IF_NEEDED(a, n + count) < 0) 187 if (RESIZE_IF_NEEDED(a, n + count) < 0)
188 return -ENOMEM; 188 return -ENOMEM;
189 189
190 ap = a->d;
190 for (i = n - 1; i >= 0; i--) 191 for (i = n - 1; i >= 0; i--)
191 ap[i + count] = ap[i]; 192 ap[i + count] = ap[i];
192 for (i = 0; i < count; i++) 193 for (i = 0; i < count; i++)