Skip to content

Fix protocol validation: replace finally with else and store parsed int#3965

Merged
petyaslavova merged 2 commits intoredis:masterfrom
bysiber:fix-protocol-validation-unbound-error
Feb 20, 2026
Merged

Fix protocol validation: replace finally with else and store parsed int#3965
petyaslavova merged 2 commits intoredis:masterfrom
bysiber:fix-protocol-validation-unbound-error

Conversation

@bysiber
Copy link
Copy Markdown
Contributor

@bysiber bysiber commented Feb 20, 2026

Two issues in the protocol validation logic during connection initialization:

1. finally block causes UnboundLocalError when ValueError is raised

When protocol is a non-numeric string like "abc", int(protocol) raises ValueError. The except ValueError clause re-raises as ConnectionError. But the finally block always executes — and p was never assigned, so p < 2 raises UnboundLocalError, replacing the meaningful error message with a confusing one.

Changed finally to else so the range check only runs after successful parsing.

2. Async connection stores raw protocol instead of parsed p

The sync version correctly does self.protocol = p (the validated integer), but the async version had self.protocol = protocol (the raw input, which could be a string). This forces workarounds throughout the async codebase (self.protocol not in [2, "2"], int(self.protocol), etc.). Fixed to store the parsed integer p, consistent with the sync version.

@jit-ci
Copy link
Copy Markdown

jit-ci bot commented Feb 20, 2026

Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset.

In case there are security findings, they will be communicated to you as a comment inside the PR.

Hope you’ll enjoy using Jit.

Questions? Comments? Want to learn more? Get in touch with us.

Copy link
Copy Markdown
Collaborator

@petyaslavova petyaslavova left a comment

Choose a reason for hiding this comment

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

LGTM.

@petyaslavova petyaslavova merged commit 6b524e6 into redis:master Feb 20, 2026
120 of 121 checks passed
@petyaslavova petyaslavova added maintenance Maintenance (CI, Releases, etc) labels Feb 20, 2026
petyaslavova added a commit that referenced this pull request Feb 25, 2026
…nt (#3965)

Co-authored-by: petyaslavova <petya.slavova@redis.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Maintenance (CI, Releases, etc)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants