Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICU-22950 Do not initialize currency for NumberPropertyMapper when useCurrency is false #3250

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions icu4c/source/i18n/number_mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ MacroProps NumberPropertyMapper::oldToNew(const DecimalFormatProperties& propert
!properties.currencyPluralInfo.fPtr.isNull() ||
!properties.currencyUsage.isNull() ||
warehouse.affixProvider.get().hasCurrencySign());
CurrencyUnit currency = resolveCurrency(properties, locale, status);
UCurrencyUsage currencyUsage = properties.currencyUsage.getOrDefault(UCURR_USAGE_STANDARD);
CurrencyUnit currency;
UCurrencyUsage currencyUsage;
Comment on lines +77 to +78
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expanded the rest of this (long) function, searched for "currency", and found some code that uses these variables but is gated by conditions other than useCurrency.

  • if (!properties.currencyUsage.isNull())
  • if (exportedProperties != nullptr)

If we think we can guarantee that useCurrency is true there, then we should add U_ASSERT(useCurrency) there.
For the second one, maybe a default-constructed currency is fine if not useCurrency?
Otherwise, we may need to add a runtime check for useCurrency.

@sffc @mihnita @richgillam @eggrobin

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Markus is right; this is a concern.

For if (!properties.currencyUsage.isNull()), I wonder if this could just be changed to useCurrency?

For exportedProperties->currency, I agree that a default-constructed currency ought to be fine.

For if (precision.fType == Precision::PrecisionType::RND_CURRENCY), I wonder if we could change this to if (useCurrency && precision.fType == Precision::PrecisionType::RND_CURRENCY)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an assertion to if (!properties.currencyUsage.isNull()), since useCurrency is true when !properties.currencyUsage.isNull()
Used if (useCurrency && precision.fType == Precision::PrecisionType::RND_CURRENCY), thank you!

if (useCurrency) {
currency = resolveCurrency(properties, locale, status);
currencyUsage = properties.currencyUsage.getOrDefault(UCURR_USAGE_STANDARD);
// NOTE: Slicing is OK.
macros.unit = currency; // NOLINT
}
Expand Down Expand Up @@ -129,6 +131,7 @@ MacroProps NumberPropertyMapper::oldToNew(const DecimalFormatProperties& propert
}
Precision precision;
if (!properties.currencyUsage.isNull()) {
U_ASSERT(useCurrency);
precision = Precision::constructCurrency(currencyUsage).withCurrency(currency);
} else if (roundingIncrement != 0.0) {
if (PatternStringUtils::ignoreRoundingIncrement(roundingIncrement, maxFrac)) {
Expand Down Expand Up @@ -276,7 +279,7 @@ MacroProps NumberPropertyMapper::oldToNew(const DecimalFormatProperties& propert
exportedProperties->maximumIntegerDigits = maxInt == -1 ? INT32_MAX : maxInt;

Precision rounding_;
if (precision.fType == Precision::PrecisionType::RND_CURRENCY) {
if (useCurrency && precision.fType == Precision::PrecisionType::RND_CURRENCY) {
rounding_ = precision.withCurrency(currency, status);
} else {
rounding_ = precision;
Expand Down