fix: Reject out-of-range values in isOnVaultBaseGrid

Check isRepresentable before building an STAmount, so an out-of-range
user-supplied value is rejected in preclaim instead of throwing.
This commit is contained in:
Vito committed 2026-10-13 14:14:22 +02:00
1 parent cd63845918
commit 77296557bc
3 files changed
+11 -2

No files matched your search

+2 -1
View File
@@ -320,7 +320,8 @@ vaultBaseScale(Asset const& asset, std::uint8_t scale);
/**
* Returns true iff value is exactly representable both as an STAmount of
* asset and on the base grid at baseScale (no precision lost rounding
* TowardsZero at baseScale). Shared representability check for
* TowardsZero at baseScale). Returns false, rather than throwing, for a value
* out of range for the asset. Shared representability check for
* VaultCreate::preclaim, VaultSet::preclaim and LoanBrokerSet::preclaim's
* AssetsMaximum / DebtMaximum / cover-rate field checks.
*
+3 -1
View File
@@ -569,7 +569,9 @@ vaultBaseScale(Asset const& asset, std::uint8_t scale)
[[nodiscard]] bool
isOnVaultBaseGrid(Asset const& asset, Number const& value, int baseScale)
{
return STAmount{asset, value} == value &&
// isRepresentable first: building an STAmount from an out-of-range value
// throws, and this checks user-supplied values in preclaim.
return isRepresentable(asset, value) && STAmount{asset, value} == value &&
roundToAsset(asset, value, baseScale, Number::RoundingMode::TowardsZero) == value;
}
@@ -22,6 +22,7 @@
#include <helpers/Account.h>
#include <protocol/VaultTestHelpers.h>
#include <cstdint>
#include <memory>
#include <optional>
#include <unordered_set>
@@ -242,6 +243,11 @@ TEST(VaultGrid, is_on_vault_base_grid)
EXPECT_EQ(vaultBaseScale(xrpIssue(), 6), 0);
EXPECT_TRUE(isOnVaultBaseGrid(xrpIssue(), Number{7}, 0));
// Out of range for the asset: rejected instead of throwing.
EXPECT_FALSE(isOnVaultBaseGrid(
xrpIssue(), Number{static_cast<std::int64_t>(STAmount::kMaxNativeN)} + 1, 0));
EXPECT_FALSE(isOnVaultBaseGrid(iou, Number{1, 100}, baseScale));
}
TEST(VaultGrid, clamp_vault_outflow)