Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 1605922 - Account for negative sign in mp_radix_size r=bbeurdouche
Differential Revision: https://phabricator.services.mozilla.com/D86443

--HG--
extra : moz-landing-system : lando
  • Loading branch information
Kevin Jacobs committed Sep 8, 2020
1 parent 7290f54 commit 0ad7e68
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions gtests/freebl_gtest/mpi_unittest.cc
Expand Up @@ -192,6 +192,39 @@ TEST_F(MPITest, MpiFixlenOctetsZero) {
TestToFixedOctets(zero, sizeof(mp_digit) + 1);
}

TEST_F(MPITest, MpiRadixSizeNeg) {
char* str;
mp_int a;
mp_err rv;
const char* negative_edge =
"-5400000000000000003000000002200020090919017007777777777870000090"
"00000000007500443416610000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000075049054"
"18610000800555594485440016000031555550000000000000000220030200909"
"19017007777777700000000000000000000000000000000000000000000000000"
"00000000000500000000000000000000000000004668129841661000071000000"
"00000000000000000000000000000000000000000000000007504434166100000"
"00000000000000000000000000000000000000000000000000000000000000000"
"00000000075049054186100008005555944854400184572169555500000000000"
"0000022003020090919017007777777700000000000000000000";

rv = mp_init(&a);
ASSERT_EQ(MP_OKAY, rv);
rv = mp_read_variable_radix(&a, negative_edge, 10);
ASSERT_EQ(MP_OKAY, rv);

const int radixSize = mp_radix_size(&a, 10);
ASSERT_LE(0, radixSize);

str = (char*)malloc(radixSize);
ASSERT_NE(nullptr, str);
rv = mp_toradix(&a, str, 10);
ASSERT_EQ(MP_OKAY, rv);
ASSERT_EQ(0, strcmp(negative_edge, str));
free(str);
mp_clear(&a);
}

TEST_F(MPITest, MpiFixlenOctetsVarlen) {
std::vector<uint8_t> packed;
for (size_t i = 0; i < sizeof(mp_digit) * 2; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion lib/freebl/mpi/mpi.c
Expand Up @@ -2693,7 +2693,7 @@ mp_radix_size(mp_int *mp, int radix)

bits = USED(mp) * DIGIT_BIT - 1;

return s_mp_outlen(bits, radix);
return SIGN(mp) + s_mp_outlen(bits, radix);

} /* end mp_radix_size() */

Expand Down

0 comments on commit 0ad7e68

Please sign in to comment.