Remove undefined behavior

* Taking the negative of a signed negative is UB, but
  taking the negative of an unsigned is not.
This commit is contained in:
Howard Hinnant
2022-09-15 11:31:15 -04:00
committed by Elliot Lee
parent d275a2ab72
commit 6fcd654bee
6 changed files with 255 additions and 183 deletions

View File

@@ -2092,37 +2092,53 @@ public:
using namespace jtx;
Env env{*this, features};
for (auto NumberSwitchOver : {false, true})
{
Env env{*this, features};
if (NumberSwitchOver)
env.enableFeature(fixUniversalNumber);
else
env.disableFeature(fixUniversalNumber);
auto const gw = Account{"gateway"};
auto const alice = Account{"alice"};
auto const bob = Account{"bob"};
auto const USD = gw["USD"];
auto const gw = Account{"gateway"};
auto const alice = Account{"alice"};
auto const bob = Account{"bob"};
auto const USD = gw["USD"];
env.fund(XRP(10000), gw, alice, bob);
env.fund(XRP(10000), gw, alice, bob);
env(rate(gw, 1.005));
env(rate(gw, 1.005));
env(trust(alice, USD(1000)));
env(trust(bob, USD(1000)));
env(trust(gw, alice["USD"](50)));
env(trust(alice, USD(1000)));
env(trust(bob, USD(1000)));
env(trust(gw, alice["USD"](50)));
env(pay(gw, bob, bob["USD"](1)));
env(pay(alice, gw, USD(50)));
env(pay(gw, bob, bob["USD"](1)));
env(pay(alice, gw, USD(50)));
env(trust(gw, alice["USD"](0)));
env(trust(gw, alice["USD"](0)));
env(offer(alice, USD(50), XRP(150000)));
env(offer(bob, XRP(100), USD(0.1)));
env(offer(alice, USD(50), XRP(150000)));
env(offer(bob, XRP(100), USD(0.1)));
auto jrr = ledgerEntryState(env, alice, gw, "USD");
BEAST_EXPECT(
jrr[jss::node][sfBalance.fieldName][jss::value] ==
"49.96666666666667");
jrr = ledgerEntryState(env, bob, gw, "USD");
BEAST_EXPECT(
jrr[jss::node][sfBalance.fieldName][jss::value] ==
"-0.9665000000333333");
auto jrr = ledgerEntryState(env, alice, gw, "USD");
BEAST_EXPECT(
jrr[jss::node][sfBalance.fieldName][jss::value] ==
"49.96666666666667");
jrr = ledgerEntryState(env, bob, gw, "USD");
if (NumberSwitchOver)
{
BEAST_EXPECT(
jrr[jss::node][sfBalance.fieldName][jss::value] ==
"-0.9665000000333333");
}
else
{
BEAST_EXPECT(
jrr[jss::node][sfBalance.fieldName][jss::value] ==
"-0.966500000033334");
}
}
}
void

View File

@@ -905,100 +905,143 @@ public:
{
testcase("IOU to IOU");
NumberSO stNumberSO{true};
Quality q1 = get_quality("1", "1");
for (auto NumberSwitchOver : {false, true})
{
NumberSO stNumberSO{NumberSwitchOver};
Quality q1 = get_quality("1", "1");
// Highly exaggerated 50% transfer rate for the input and output:
Rate const rate{parityRate.value + (parityRate.value / 2)};
// Highly exaggerated 50% transfer rate for the input and output:
Rate const rate{parityRate.value + (parityRate.value / 2)};
// TAKER OWNER
// QUAL OFFER FUNDS QUAL OFFER FUNDS
// EXPECTED
// EUR USD
attempt(
Sell,
"N:N",
q1,
{"2", "2"},
"10",
q1,
{"2", "2"},
"10",
{"2", "2"},
eur(),
usd(),
rate,
rate);
attempt(
Sell,
"N:B",
q1,
{"4", "4"},
"10",
q1,
{"4", "4"},
"4",
{"2.666666666666667", "2.666666666666667"},
eur(),
usd(),
rate,
rate);
attempt(
Buy,
"N:T",
q1,
{"1", "1"},
"10",
q1,
{"2", "2"},
"10",
{"1", "1"},
eur(),
usd(),
rate,
rate);
attempt(
Buy,
"N:BT",
q1,
{"2", "2"},
"10",
q1,
{"6", "6"},
"5",
{"2", "2"},
eur(),
usd(),
rate,
rate);
attempt(
Buy,
"N:TB",
q1,
{"2", "2"},
"2",
q1,
{"6", "6"},
"1",
{"0.6666666666666667", "0.6666666666666667"},
eur(),
usd(),
rate,
rate);
attempt(
Sell,
"A:N",
q1,
{"2", "2"},
"2.5",
q1,
{"2", "2"},
"10",
{"1.666666666666667", "1.666666666666667"},
eur(),
usd(),
rate,
rate);
// TAKER OWNER
// QUAL OFFER FUNDS QUAL OFFER FUNDS
// EXPECTED
// EUR USD
attempt(
Sell,
"N:N",
q1,
{"2", "2"},
"10",
q1,
{"2", "2"},
"10",
{"2", "2"},
eur(),
usd(),
rate,
rate);
if (NumberSwitchOver)
{
attempt(
Sell,
"N:B",
q1,
{"4", "4"},
"10",
q1,
{"4", "4"},
"4",
{"2.666666666666667", "2.666666666666667"},
eur(),
usd(),
rate,
rate);
}
else
{
attempt(
Sell,
"N:B",
q1,
{"4", "4"},
"10",
q1,
{"4", "4"},
"4",
{"2.666666666666666", "2.666666666666666"},
eur(),
usd(),
rate,
rate);
}
attempt(
Buy,
"N:T",
q1,
{"1", "1"},
"10",
q1,
{"2", "2"},
"10",
{"1", "1"},
eur(),
usd(),
rate,
rate);
attempt(
Buy,
"N:BT",
q1,
{"2", "2"},
"10",
q1,
{"6", "6"},
"5",
{"2", "2"},
eur(),
usd(),
rate,
rate);
attempt(
Buy,
"N:TB",
q1,
{"2", "2"},
"2",
q1,
{"6", "6"},
"1",
{"0.6666666666666667", "0.6666666666666667"},
eur(),
usd(),
rate,
rate);
if (NumberSwitchOver)
{
attempt(
Sell,
"A:N",
q1,
{"2", "2"},
"2.5",
q1,
{"2", "2"},
"10",
{"1.666666666666667", "1.666666666666667"},
eur(),
usd(),
rate,
rate);
}
else
{
attempt(
Sell,
"A:N",
q1,
{"2", "2"},
"2.5",
q1,
{"2", "2"},
"10",
{"1.666666666666666", "1.666666666666666"},
eur(),
usd(),
rate,
rate);
}
}
}
void

View File

@@ -543,6 +543,9 @@ public:
void
enableFeature(uint256 const feature);
void
disableFeature(uint256 const feature);
private:
void
fund(bool setDefaultRipple, STAmount const& amount, Account const& account);

View File

@@ -466,6 +466,14 @@ Env::enableFeature(uint256 const feature)
app().config().features.insert(feature);
}
void
Env::disableFeature(uint256 const feature)
{
// Env::close() must be called for feature
// enable to take place.
app().config().features.erase(feature);
}
} // namespace jtx
} // namespace test