Skip to content

Reject out-of-range column index in Dataset.add_formatter - #655

Open
c-tonneslan wants to merge 1 commit into
jazzband:masterfrom
c-tonneslan:fix-add-formatter-off-by-one
Open

Reject out-of-range column index in Dataset.add_formatter#655
c-tonneslan wants to merge 1 commit into
jazzband:masterfrom
c-tonneslan:fix-add-formatter-off-by-one

Conversation

@c-tonneslan

Copy link
Copy Markdown

Dataset.add_formatter guards the column index with col <= self.width, but column indices are 0-based, so the only valid indices are 0 .. width - 1. The <= lets col == width — always out of range — slip past the InvalidDatasetIndex guard.

The bad index isn't caught at registration; it surfaces later as a raw IndexError from row[col] when the formatter runs during export:

import tablib

d = tablib.Dataset()
d.headers = ['a', 'b']          # width 2, valid indices 0 and 1
d.append([1, 2])
d.add_formatter(2, lambda v: v * 10)   # index 2 == width, out of range
d.dict                                  # -> IndexError: list index out of range

Changing the guard to col < self.width rejects the invalid index at registration time with InvalidDatasetIndex, as intended. Valid indices, None (all columns), and header-name lookups are unaffected. InvalidDatasetIndex subclasses IndexError, so any caller already catching IndexError still works.

Added a regression test and myself to AUTHORS.

Signed-off-by: c-tonneslan <cst0520@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant