diff options
author | Werner Koch <wk@gnupg.org> | 2013-08-08 15:16:48 +0200 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2013-08-08 15:17:27 +0200 |
commit | 426cbc9feca0c8f46208fb3670adab95f9e46087 (patch) | |
tree | 5c68e2917c9cffcd53d875e5445caa6eaf2bd07c | |
parent | cc082642c1b0f2a3e9ca78e1ffd3f64417c204bd (diff) | |
download | libgcrypt-426cbc9feca0c8f46208fb3670adab95f9e46087.tar.gz |
mpi: Clear immutable flag on the result of gcry_mpi_set.
* mpi/mpiutil.c (gcry_mpi_set): Reset immutable and const flags.
* tests/mpitests.c (test_const_and_immutable): Add a test for this.
--
gcry_mpi_set shall behave like gcry_mpi_copy and thus reset those
special flags. Problem reported by Christian Grothoff.
Signed-off-by: Werner Koch <wk@gnupg.org>
-rw-r--r-- | mpi/mpiutil.c | 1 | ||||
-rw-r--r-- | tests/mpitests.c | 17 |
2 files changed, 18 insertions, 0 deletions
diff --git a/mpi/mpiutil.c b/mpi/mpiutil.c index cff15b74..a65d2361 100644 --- a/mpi/mpiutil.c +++ b/mpi/mpiutil.c @@ -397,6 +397,7 @@ gcry_mpi_set( gcry_mpi_t w, gcry_mpi_t u) MPN_COPY( wp, up, usize ); w->nlimbs = usize; w->flags = u->flags; + w->flags &= ~(16|32); /* Reset the immutable and constant flags. */ w->sign = usign; return w; } diff --git a/tests/mpitests.c b/tests/mpitests.c index 03c15b90..e1c51d16 100644 --- a/tests/mpitests.c +++ b/tests/mpitests.c @@ -143,6 +143,23 @@ test_const_and_immutable (void) if (!gcry_mpi_get_flag (one, GCRYMPI_FLAG_CONST)) die ("const flag unexpectly cleared\n"); + + second_one = gcry_mpi_set (NULL, GCRYMPI_CONST_ONE); + if (gcry_mpi_get_flag (second_one, GCRYMPI_FLAG_IMMUTABLE)) + die ("immutable flag not cleared by mpi_set (NULL,x)\n"); + if (gcry_mpi_get_flag (second_one, GCRYMPI_FLAG_CONST)) + die ("const flag not cleared by mpi_set (NULL,x)\n"); + gcry_mpi_release (second_one); + + second_one = gcry_mpi_set_ui (NULL, 42); + gcry_mpi_set (second_one, GCRYMPI_CONST_ONE); + if (gcry_mpi_get_flag (second_one, GCRYMPI_FLAG_IMMUTABLE)) + die ("immutable flag not cleared after mpi_set (a,x)\n"); + if (gcry_mpi_get_flag (second_one, GCRYMPI_FLAG_CONST)) + die ("const flag not cleared mpi_set (a,x)\n"); + gcry_mpi_release (second_one); + + /* Due to the the constant flag the release below should be a NOP and will leak memory. */ gcry_mpi_release (one); |