Skip to content

Fix UnboundLocalError in nested config updates - #10598

Open
krantboy wants to merge 1 commit into
aws:developfrom
krantboy:fix-10348-nested-config-unbound
Open

Fix UnboundLocalError in nested config updates#10598
krantboy wants to merge 1 commit into
aws:developfrom
krantboy:fix-10348-nested-config-unbound

Conversation

@krantboy

@krantboy krantboy commented Aug 23, 2026

Copy link
Copy Markdown

Fixes #10348

ConfigFileWriter._update_subattributes() reads current_indent on every line
but only assigns it when OPTION_REGEX matches, so any non-option line directly
after a nested key is read before assignment. i has the same problem when the
nested key is the last line in the file and the loop body never runs.

All three of these shapes crash on aws configure set s3.signature_version s3v4:

[default]        [default]        [default]
s3 =             s3 =             s3 =
# comment        [profile foo]    (end of file)
UnboundLocalError: cannot access local variable 'current_indent'
where it is not associated with a value

The fix classifies each line explicitly instead of carrying the previous match's
indent forward, and seeds i before the loop. It also adds a missing trailing
newline before appending, without which the above would turn the crash into a
corrupted config file.

Tests cover all three shapes plus an empty nested block with no trailing
newline; each fails before the change.

@krantboy
krantboy requested a review from a team as a code owner August 23, 2026 08:34
_update_subattributes() read ``current_indent`` on every line but only
assigned it when OPTION_REGEX matched.  Any non-option line directly
after a nested key -- a comment, a blank line, or a section header --
was therefore read before assignment and raised UnboundLocalError.
``i`` had the same problem when the nested key was the last line in the
file, leaving the for-else branch with no loop variable.

Classify each line explicitly rather than carrying the indent of the
previous match forward, and seed ``i`` so an empty loop range takes the
same append path as running off the end of the file.

Appending to a block at the end of a file with no trailing newline joins
two lines together, so add the missing newline first.  Without this the
change above turns a crash into a silently corrupted config file.

Fixes aws#10348
@krantboy
krantboy force-pushed the fix-10348-nested-config-unbound branch from a340ff7 to b1afbc0 Compare August 23, 2026 08:37
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.

aws configure set s3.* can raise UnboundLocalError when updating nested config blocks

1 participant