Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 1509432 - Removed duplicated code between mp_set_int and mp_set_u…
…long. Created a gtest for this functions. r=KevinJacobs

Differential Revision: https://phabricator.services.mozilla.com/D30870

--HG--
extra : moz-landing-system : lando
  • Loading branch information
marcusburghardt committed May 13, 2019
1 parent 9d34394 commit c9065c3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
35 changes: 35 additions & 0 deletions gtests/freebl_gtest/mpi_unittest.cc
Expand Up @@ -148,6 +148,41 @@ TEST_F(MPITest, MpiCmpUnalignedTest) {
}
#endif

// The two follow tests ensure very similar mp_set_* functions are ok.
TEST_F(MPITest, MpiSetUlong) {
mp_int a, b, c;
MP_DIGITS(&a) = 0;
MP_DIGITS(&b) = 0;
MP_DIGITS(&c) = 0;
ASSERT_EQ(MP_OKAY, mp_init(&a));
ASSERT_EQ(MP_OKAY, mp_init(&b));
ASSERT_EQ(MP_OKAY, mp_init(&c));
EXPECT_EQ(MP_OKAY, mp_set_ulong(&a, 1));
EXPECT_EQ(MP_OKAY, mp_set_ulong(&b, 0));
EXPECT_EQ(MP_OKAY, mp_set_ulong(&c, -1));

mp_clear(&a);
mp_clear(&b);
mp_clear(&c);
}

TEST_F(MPITest, MpiSetInt) {
mp_int a, b, c;
MP_DIGITS(&a) = 0;
MP_DIGITS(&b) = 0;
MP_DIGITS(&c) = 0;
ASSERT_EQ(MP_OKAY, mp_init(&a));
ASSERT_EQ(MP_OKAY, mp_init(&b));
ASSERT_EQ(MP_OKAY, mp_init(&c));
EXPECT_EQ(MP_OKAY, mp_set_int(&a, 1));
EXPECT_EQ(MP_OKAY, mp_set_int(&b, 0));
EXPECT_EQ(MP_OKAY, mp_set_int(&c, -1));

mp_clear(&a);
mp_clear(&b);
mp_clear(&c);
}

TEST_F(MPITest, MpiFixlenOctetsZero) {
std::vector<uint8_t> zero = {0};
TestToFixedOctets(zero, 1);
Expand Down
26 changes: 6 additions & 20 deletions lib/freebl/mpi/mpi.c
Expand Up @@ -341,33 +341,19 @@ mp_set(mp_int *mp, mp_digit d)
mp_err
mp_set_int(mp_int *mp, long z)
{
int ix;
unsigned long v = labs(z);
mp_err res;

ARGCHK(mp != NULL, MP_BADARG);

mp_zero(mp);
if (z == 0)
return MP_OKAY; /* shortcut for zero */

if (sizeof v <= sizeof(mp_digit)) {
DIGIT(mp, 0) = v;
} else {
for (ix = sizeof(long) - 1; ix >= 0; ix--) {
if ((res = s_mp_mul_d(mp, (UCHAR_MAX + 1))) != MP_OKAY)
return res;

res = s_mp_add_d(mp, (mp_digit)((v >> (ix * CHAR_BIT)) & UCHAR_MAX));
if (res != MP_OKAY)
return res;
}
/* https://bugzilla.mozilla.org/show_bug.cgi?id=1509432 */
if ((res = mp_set_ulong(mp, v)) != MP_OKAY) { /* avoids duplicated code */
return res;
}
if (z < 0)

if (z < 0) {
SIGN(mp) = NEG;
}

return MP_OKAY;

} /* end mp_set_int() */

/* }}} */
Expand Down

0 comments on commit c9065c3

Please sign in to comment.