You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The NewFromFloat function panics when provided with an invalid currency code, due to an attempt to access .Fraction on a nil pointer. This behavior is inconsistent with the New function, which handles invalid currency inputs by falling back to a default currency. This may lead to unexpected crashes in applications relying on the NewFromFloat function for currency conversion when invalid currency codes are inadvertently passed, or not yet registered in the library.
Steps to Reproduce
Use NewFromFloat with an invalid/unrecognized currency code.
Observe the panic due to a nil pointer dereference when attempting to access .Fraction.
A minimal code example demonstrating the issue is available here: Go Playground
Expected Behavior
Ideally, NewFromFloat should have a consistent invalid/unrecognized currency handling strategy with New, possibly by defaulting to a "safe" currency configuration when the currency code is not recognized, instead of panicking.
Possible Solution
A straightforward fix could be aligning the way currencies are set up in currencyDecimals during instantiation with how it's done in New. Essentially, we'd be replicating the currency creation logic.
I'm not sure if this is a known issue, or even if this is actually expected behavior, but if it's not, it should be an easy fix... I would gladly open a PR for this one!
The text was updated successfully, but these errors were encountered:
A straightforward fix could be aligning the way currencies are set up in currencyDecimals during instantiation with how it's done in New. Essentially, we'd be replicating the currency creation logic.
Yes, that would be the fix, I would really appreciate if you could open the PR, looking at the code it doesn't look like it should cause any backwards compatibility issues
Description
The
NewFromFloat
function panics when provided with an invalid currency code, due to an attempt to access.Fraction
on a nil pointer. This behavior is inconsistent with theNew
function, which handles invalid currency inputs by falling back to a default currency. This may lead to unexpected crashes in applications relying on theNewFromFloat
function for currency conversion when invalid currency codes are inadvertently passed, or not yet registered in the library.Steps to Reproduce
NewFromFloat
with an invalid/unrecognized currency code..Fraction
.A minimal code example demonstrating the issue is available here: Go Playground
Expected Behavior
Ideally,
NewFromFloat
should have a consistent invalid/unrecognized currency handling strategy withNew
, possibly by defaulting to a "safe" currency configuration when the currency code is not recognized, instead of panicking.Possible Solution
A straightforward fix could be aligning the way currencies are set up in currencyDecimals during instantiation with how it's done in New. Essentially, we'd be replicating the currency creation logic.
I'm not sure if this is a known issue, or even if this is actually expected behavior, but if it's not, it should be an easy fix... I would gladly open a PR for this one!
The text was updated successfully, but these errors were encountered: