1 minor finding · 2 suggestions
Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive for their assessments.
Findings
[minor] · Explicit CAmount construction accepts unsigned values that wrap
CAmount{u} now compiles for uint64_t or size_t values above INT64_MAX and stores a negative amount, unlike the old alias; no current out-of-range caller was identified.
Location: src/consensus/amount.h:36 (head).
Details and suggested fix
The constructor accepts any non-bool integral type and initializes its int64_t member from that value. For an unsigned value above INT64_MAX, CAmount{u} therefore compiles and stores the value modulo 2^64, producing a negative amount. With the old typedef int64_t CAmount, this brace initialization was a narrowing error, and this change makes CAmount{...} the standard explicit spelling.
The scalar operator/ and operator% templates use the same constraint, so dividing a negative amount by a size_t divides its unsigned reinterpretation. That behavior is unchanged from the base. For example, recipients_sum / vecSend.size() in spend.cpp is safe only because CreateTransaction first rejects negative recipient amounts.
Restrict these templates to types whose values fit in int64_t, such as signed integers and unsigned types narrower than 64 bits, so out-of-range uses fail to compile. Call sites such as CAmount(group_pos.size()) in the coin-selection fuzz targets would then need an explicit cast. No current caller passing a value above INT64_MAX was identified, so production impact is unconfirmed.
Optional improvements
[suggestion] · QAmount allows implicit raw-integer use at the GUI amount field
BitcoinAmountField::value() and setValue() still accept bool, floating-point, and raw-integer expressions, reopening conversions this change removes elsewhere; no current misuse was found.
Location: src/qt/bitcoinamountfield.h:35 (head).
Details and suggested fix
value() returns QAmount, a public CAmount subclass with an implicit operator qint64(), and a public setValue(qint64) overload was added. Both support the moc-generated code for Q_PROPERTY(qint64 value READ value WRITE setValue ...), but they also let ordinary callers use conversions this change removes elsewhere. For example, value() <= 0, if (value()), and value() * 0.5 compile through qint64. setValue(true) and setValue(d) for a double d also compile because the CAmount constructor rejects those types and overload resolution selects setValue(qint64).
Current callers use safe forms, such as value(nullptr) <= 0*sats in sendcoinsentry.cpp; the concern is future GUI code. Give the Qt property separate accessors while keeping value() typed:
Q_PROPERTY(qint64 value READ qintValue WRITE setQintValue NOTIFY valueChanged USER true)
qint64 qintValue() const { return value().Int(); }
void setQintValue(qint64 v) { setValue(CAmount{v}); }
Then value() can return CAmount, and QAmount and the public setValue(qint64) overload can be removed. The property name and type remain unchanged. The two sendcoinsdialog.cpp call sites would need small edits: use CAmount{...} for the setting value and .Int() when saving the custom fee.
[suggestion] · CAmount's compile-time restrictions lack regression tests
No tests pin the restrictions on construction, conversion, or amount multiplication, so later changes could relax them without a test failure.
Location: src/consensus/amount.h:34 (head).
Details and suggested fix
The guarantees introduced here are compile-time properties: the deleted default constructor, the explicit non_bool_integral constructor, and the absence of CAmount * CAmount. The changes to amount_tests.cpp only respell existing CFeeRate runtime checks; no test asserts these restrictions.
Add static assertions in amount_tests.cpp, following the pattern in util_not_null_tests.cpp. Check that CAmount is not default-constructible, is not constructible from bool or double, is not implicitly convertible from int64_t, and is constructible from int64_t. Pin operator restrictions with a dependent requires-expression, such as a Multipliable concept and static_assert(!Multipliable<CAmount>).
Earlier concerns
Earlier reports raised these, with the titles and severities shown there. This review neither republished nor resolved them.
- [suggestion]: Remove the unused
operator*=helper