Skip to content

fix: report out-of-bounds Fn::Select on a literal list - #4695

Merged
kddejong merged 2 commits into
aws-cloudformation:mainfrom
kddejong:fix/issue/4693
Sep 10, 2026
Merged

kddejong merged 2 commits into
aws-cloudformation:mainfrom
kddejong:fix/issue/4693

Conversation

@kddejong

@kddejong kddejong commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Fixes #4693

Fn::Select with a hardcoded index that is greater than or equal to the length of a hardcoded list passed linting with 0 errors, then failed at deploy time:

Template error: Fn::Select cannot select nonexistent value at index 5

Root cause

select() in _resolvers_cfn.py simply continues when the index is out of bounds, yielding nothing. Even if it emitted a resolution error, BaseFn.resolve() short-circuits when the target property's schema has no constraining keywords (_has_schema_constraints), so an out-of-bounds !Select in an unconstrained context (e.g. an Outputs value) would still pass silently.

Fix

Detect the out-of-bounds case directly in the E1017 rule (Select.py). The length of a literal list is fixed at authoring time — functions used as list items still each count as one element — so a hardcoded, non-negative integer index >= len(list) is always a definite deploy-time failure. Because the check lives in the rule (not the resolver), it fires regardless of the target property's schema.

Only fires when both the index is a hardcoded non-negative integer and the list is a hardcoded array; a Ref/function index or a function-produced list is left alone (it can resolve to anything at deploy time).

$ cfn-lint template.yaml
E1017 5 is not a valid index for a list of length 2
template.yaml:7:7

Tests

  • Updated an existing case that incorrectly asserted an out-of-bounds !Select [1, ["bar"]] produced no error.
  • Added cases for a plain literal out-of-bounds index and one where the list contains functions as items.

An Fn::Select with a hardcoded index greater than or equal to the length
of a hardcoded list passed linting silently, then failed at deploy time
with "Fn::Select cannot select nonexistent value at index N". Detect this
definite error in the E1017 rule so it is reported regardless of the
target property's schema constraints.

Fixes aws-cloudformation#4693
@codecov

codecov Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.65%. Comparing base (3a3a240) to head (6458c43).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4695   +/-   ##
=======================================
  Coverage   94.64%   94.65%           
=======================================
  Files         434      434           
  Lines       15543    15558   +15     
  Branches     3015     3018    +3     
=======================================
+ Hits        14711    14726   +15     
  Misses        454      454           
  Partials      378      378           
Flag Coverage Δ
unittests 94.65% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Replace the imperative out-of-bounds check with a per-instance schema.
When the list is hardcoded its length is fixed, so inject a "maximum" of
len-1 alongside the existing "minimum": 0 and let the standard keyword
validators report the out-of-bounds index with their standard message
(e.g. "5 is greater than the maximum of 1").
@kddejong
kddejong merged commit 33cae2d into aws-cloudformation:main Sep 10, 2026
21 checks passed
@kddejong
kddejong deleted the fix/issue/4693 branch September 10, 2026 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Out-of-bounds Fn::Select on literal list silently passes linting, causing deploy-time failure

1 participant