Skip to content

createPairs() raises IndexError when the keyword count is odd #9

Description

@dmccoystephenson

createPairs() indexes self.keywords[i+1] with no bounds check:

def createPairs(self):
    # match keywords in pairs
    for i in range(0, len(self.keywords), 2):
        pair = []
        pair.append(self.keywords[i])
        pair.append(self.keywords[i+1])
        self.pairs.append(pair)

When the keyword list has an odd length, the final iteration reads past the end and raises IndexError.

Verification

The method was called directly on a 3-element list:

$ python3 -c "
import sys
sys.path.insert(0, 'src')
from ideaCollisionGenerator import IdeaCollisionGenerator
g = IdeaCollisionGenerator()
g.keywords = ['a', 'b', 'c']
try:
    g.createPairs()
    print('no error, pairs =', g.pairs)
except IndexError as e:
    print('IndexError:', e)
"
IndexError: list index out of range

Reachability

This is not reachable through the CLI today, because getKeywords() hardcodes numKeywords = 10. It becomes reachable the moment the keyword count is made configurable, or whenever createPairs() is called on a list assembled some other way.

Suggested resolution

A fix should decide explicitly what an odd trailing keyword means — most plausibly that it is dropped, or that it forms a one-element pair — rather than adding a silent guard. Whichever is chosen, the fix should arrive with a test that calls createPairs() directly on an odd-length list, since the behavior cannot be observed through the CLI.

This issue body was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).


drafted by Claude on behalf of Daniel Stephenson

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions