You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
createPairs() raises IndexError when the keyword count is odd #9
createPairs() indexes self.keywords[i+1] with no bounds check:
defcreatePairs(self):
# match keywords in pairsforiinrange(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.
Activity
Reject an odd or non-positive keyword count in the constructor
Replace the ordinal ladder and make the keyword count a parameter (#20)