Skip to content

functions: Raise errors when +/- Infinity is returned from log or pow built-in functions - #39304

Open
SarahFrench wants to merge 3 commits into
mainfrom
fix-NaN-function-results
Open

SarahFrench wants to merge 3 commits into
mainfrom
fix-NaN-function-results

Conversation

@SarahFrench

Copy link
Copy Markdown
Member

Fixes #39292

This PR makes returning +/- Infinity from log or pow built-in functions invalid, and instead an error is raised.

This PR is going to be left in draft until we confirm if the issue is a bug report or feature request.

Target Release

TBD

Rollback Plan

  • If a change needs to be reverted, we will roll out an update to the code within 7 days.

Changes to Security Controls

Are there any changes to security controls (access controls, encryption, logging) in this pull request? If so, explain.

CHANGELOG entry

  • This change is user-facing and I added a changelog entry.
  • This change is not user-facing.

@SarahFrench SarahFrench added 1.16-backport If you add this label to a PR before merging, backport-assistant will open a new PR once merged 1.17-backport If you add this label to a PR before merging, backport-assistant will open a new PR once merged labels Sep 29, 2026
@SarahFrench
SarahFrench force-pushed the fix-NaN-function-results branch from 7d40b0d to 115e42e Compare September 29, 2026 15:54
@SarahFrench
SarahFrench marked this pull request as ready for review September 29, 2026 15:55
@SarahFrench
SarahFrench requested a review from a team as a code owner September 29, 2026 15:55
@mildwonkey

Copy link
Copy Markdown
Contributor

I'd be happy to hear what others think, but I think errors in these cases are more correct - terraform's focus is infrastructure, not maths, so I think it's valid to return errors instead of concepts (infinity, nan, etc).

Comment on lines +41 to 43
if math.IsNaN(result) || math.IsInf(result, 0) {
return cty.UnknownVal(cty.String), fmt.Errorf("result is not a number")
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Reviewer: Should +/- infinity return a different error? I could update this to return a different fmt.Errorf("result is not a finite number") or something.

@SarahFrench
SarahFrench enabled auto-merge (squash) September 29, 2026 16:47
Comment on lines -34 to -37
cty.NumberFloatVal(0),
cty.NumberFloatVal(10),
cty.NegativeInfinity,
false,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This old test case shows infinity being returned without an error, but I don't think this was part of an explicit decision to state that infinite values were valid returned values so we shouldn't be held back by it.

For me, the fact downstream code cannot process this value is a clear sign that raising an error instead is the correct behaviour.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1.16-backport If you add this label to a PR before merging, backport-assistant will open a new PR once merged 1.17-backport If you add this label to a PR before merging, backport-assistant will open a new PR once merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

log and pow return positive/negative infinity as valid numbers instead of errors

2 participants