Skip to content

Fix IndexError when parsing malformed git remote output - #714

Open
ptanh05 wants to merge 1 commit into
TheKevJames:masterfrom
ptanh05:fix/git-remote-indexerror
Open

ptanh05 wants to merge 1 commit into
TheKevJames:masterfrom
ptanh05:fix/git-remote-indexerror

Conversation

@ptanh05

@ptanh05 ptanh05 commented May 14, 2026

Copy link
Copy Markdown

Summary

  • Fix IndexError in coveralls/git.py when git remote -v produces malformed lines containing (fetch) with fewer than 2 space-separated tokens
  • Add a length guard before indexing into line.split() results in the remotes list comprehension
  • Add regression test to verify the fix

Test plan

  • Run pytest tests/test_git_malformed_remote.py -v to verify the bug is fixed
  • Run pytest tests/ --ignore=tests/git_test.py --ignore=tests/cli_test.py --ignore=tests/api_test.py --ignore=tests/data/ to verify no regressions

git remote -v can produce lines with fewer than 2 space-separated tokens
(e.g., a bare "(fetch)" line), causing the list comprehension in git_info()
to raise an IndexError. Add a length check before accessing split() results
to skip malformed lines gracefully.
@ptanh05
ptanh05 requested a review from TheKevJames as a code owner May 14, 2026 01:17

@TheKevJames TheKevJames left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for submitting this fix!

Do you have an example of a case when git remote -v would provide this output? My understanding was that the output of that command was pretty standardized, so IMO it doesn't make sense to guard against a case that can't really happen. Is there some sort of git version, config flag, remote state, or alternate porcelain involved?

@TheKevJames TheKevJames added the needs-info Needs more information from the reporter label Oct 2, 2026
@mgkvibez

Copy link
Copy Markdown

The guard does fix the crash it describes. I took the current parser and the patched one and ran both over the same output:

current : IndexError: list index out of range
PR #714 : [{'name': 'origin', 'url': 'https://github.com/user/repo.git'}]

Two things I would look at before merging, both about the shape of the fix rather than whether it works.

The guard admits one malformed line the parser still mis-reads. len(line.split()) >= 2 is true for a two-token line like origin (fetch), and then the URL becomes the marker:

[{'name': 'origin', 'url': '(fetch)'}]

That is the same class of bug, one step further in. git remote -v separates the name from the URL with a tab and always ends the line with the marker, so anchoring on that removes the whole class rather than the one instance: name, url = line.split('\t', 1) and then strip the trailing (fetch), or keep the two-way split and take ' '.join(parts[1:-1]) for the URL. It also survives a URL containing a space, which parts[1] silently truncates.

The for parts in [line.split()] trick works but costs more than it saves. It gets one split() per line inside a comprehension, which is a fair instinct, but the original comprehension calls line.split() twice and a reader can see both; the walrus or a small helper (def _remote(line) returning the dict or None) keeps that, and matches how the rest of git.py reads.

One practical note: this was opened in May and that block has moved since. The except clause underneath is now except (RuntimeError, OSError) rather than CoverallsException, so the patch needs a rebase, though the comprehension itself is byte-identical to what you see here, so the fix still applies cleanly once rebased.

The regression test is worth keeping whichever form the fix takes; it mocks run_command and covers exactly the line that used to raise, which is more than the suite had for this path.

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

needs-info Needs more information from the reporter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants