Skip to content

fix: prevent crash on unclosed quotes in os-release files - #388

Open
pro-laxmi wants to merge 1 commit into
python-distro:masterfrom
pro-laxmi:fix/os-release-shlex-error
Open

pro-laxmi wants to merge 1 commit into
python-distro:masterfrom
pro-laxmi:fix/os-release-shlex-error

Conversation

@pro-laxmi

Copy link
Copy Markdown

Summary of Changes

When /etc/os-release is truncated, corrupted, or contains unclosed quotation marks (e.g. NAME="Fedora Linux), shlex.shlex raises ValueError("No closing quotation"). Currently, this unhandled exception crashes the entire program and any application or library importing distro.

Root Cause

_parse_os_release_content called list(lexer) without catching ValueError.

Fix

Wrapped shlex lexer tokenization in a try...except ValueError block with an automatic fallback to robust line-by-line parsing (key, val = line.split("=", 1)), stripping surrounding quotes.

Verification & Testing

Tested with malformed os-release streams and unclosed quotes:

bad_content = io.StringIO('NAME="Fedora Linux\nID=fedora\nVERSION_ID=44\n')
info = distro.LinuxDistribution._parse_os_release_content(bad_content)
assert info['id'] == 'fedora'
assert info['name'] == 'Fedora Linux'

Parsed successfully without crashing.

@HorlogeSkynet HorlogeSkynet left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hey @pro-laxmi, thanks for you PR.

Could you address the following points :

  • code comments in the for loop shouldn't be removed
  • io.StringIO wrapping of lines could be replaced by a lines.seek(os.SEEK_SET) if input file ends up being manually parsed the hard way (shouldn't occur much, and these files don't usually contain much data)
  • only shlex.shlex call should be wrapped in the try..except ValueError block

Thanks, bye 👋

@pro-laxmi
pro-laxmi force-pushed the fix/os-release-shlex-error branch from 66f5818 to 6a95f5f Compare November 13, 2026 15:44
@pro-laxmi

Copy link
Copy Markdown
Author

Hey @HorlogeSkynet, thanks for the review and suggestions!

I've updated the PR to address all three points:

  • Restored the original code comments inside the for loop.
  • Replaced the io.StringIO wrapper with lines.seek(os.SEEK_SET) on fallback (and removed the unused import io).
  • Narrowed the try...except ValueError block strictly around the shlex.shlex token generation.

Tested locally with both valid and malformed os-release streams and everything is working smoothly. Let me know if there's anything else needed! 😊

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants