Skip to content

Commit 3673c21

Browse files
mdboomembray
authored andcommitted
Fix #3545: Segfault when column header is skipped
1 parent 69f4267 commit 3673c21

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

‎astropy/io/ascii/src/tokenizer.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ void resize_comments(tokenizer_t *self)
100100
{
101101
// Double the size of the comments string
102102
self->comment_lines = (char *) realloc(self->comment_lines,
103-
2 * self->comment_lines_len);
103+
self->comment_pos + 1);
104104
// Set the second (newly allocated) half of the column string to all zeros
105105
memset(self->comment_lines + self->comment_lines_len * sizeof(char), 0,
106-
self->comment_lines_len * sizeof(char));
106+
(self->comment_pos + 1 - self->comment_lines_len) * sizeof(char));
107107

108-
self->comment_lines_len *= 2;
108+
self->comment_lines_len *= self->comment_pos + 1;
109109
}
110110

111111
/*
@@ -130,7 +130,7 @@ static inline void push(tokenizer_t *self, char c, int col)
130130
*/
131131
static inline void push_comment(tokenizer_t *self, char c)
132132
{
133-
if (self->comment_pos == self->comment_lines_len)
133+
if (self->comment_pos >= self->comment_lines_len)
134134
{
135135
resize_comments(self);
136136
}
@@ -453,6 +453,8 @@ int tokenize(tokenizer_t *self, int end, int header, int num_cols)
453453
++self->source_pos;
454454
}
455455

456+
if (col >= self->num_cols)
457+
RETURN(TOO_MANY_COLS);
456458
END_FIELD(); // whitespace counts as a field
457459
}
458460
}

‎astropy/io/ascii/tests/test_c_reader.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,3 +873,16 @@ def test_empty_quotes(parallel, read_basic):
873873
table = read_basic('a b\n1 ""\n2 ""', parallel=parallel)
874874
expected = Table([[1, 2], [0, 0]], names=('a', 'b'))
875875
assert_table_equal(table, expected)
876+
877+
@pytest.mark.parametrize("parallel", [True, False])
878+
def test_fast_tab_with_names(parallel, read_tab):
879+
"""
880+
Make sure the C reader doesn't segfault when the header for the
881+
first column is missing [#3545]
882+
"""
883+
content = """#
884+
\tdecDeg\tRate_pn_offAxis\tRate_mos2_offAxis\tObsID\tSourceID\tRADeg\tversion\tCounts_pn\tRate_pn\trun\tRate_mos1\tRate_mos2\tInserted_pn\tInserted_mos2\tbeta\tRate_mos1_offAxis\trcArcsec\tname\tInserted\tCounts_mos1\tInserted_mos1\tCounts_mos2\ty\tx\tCounts\toffAxis\tRot
885+
-3.007559\t0.0000\t0.0010\t0013140201\t0\t213.462574\t0\t2\t0.0002\t0\t0.0001\t0.0001\t0\t1\t0.66\t0.0217\t3.0\tfakeXMMXCS J1413.8-0300\t3\t1\t2\t1\t398.000\t127.000\t5\t13.9\t72.3\t"""
886+
head = ['A{0}'.format(i) for i in range(28)]
887+
table = read_tab(content, data_start=1,
888+
parallel=parallel, names=head)

0 commit comments

Comments
 (0)