Skip to content

Commit 8e62f8d

Browse files
committed
Fix astropy#3545: Segfault when column header is skipped
1 parent 8f149fe commit 8e62f8d

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
@@ -92,12 +92,12 @@ void resize_comments(tokenizer_t *self)
9292
{
9393
// Double the size of the comments string
9494
self->comment_lines = (char *) realloc(self->comment_lines,
95-
2 * self->comment_lines_len);
95+
self->comment_pos + 1);
9696
// Set the second (newly allocated) half of the column string to all zeros
9797
memset(self->comment_lines + self->comment_lines_len * sizeof(char), 0,
98-
self->comment_lines_len * sizeof(char));
98+
(self->comment_pos + 1 - self->comment_lines_len) * sizeof(char));
9999

100-
self->comment_lines_len *= 2;
100+
self->comment_lines_len *= self->comment_pos + 1;
101101
}
102102

103103
/*
@@ -123,7 +123,7 @@ static inline void push(tokenizer_t *self, char c, int col)
123123

124124
static inline void push_comment(tokenizer_t *self, char c)
125125
{
126-
if (self->comment_pos == self->comment_lines_len)
126+
if (self->comment_pos >= self->comment_lines_len)
127127
resize_comments(self);
128128
self->comment_lines[self->comment_pos++] = c;
129129
}
@@ -428,6 +428,8 @@ int tokenize(tokenizer_t *self, int end, int header, int num_cols)
428428
++self->source_pos;
429429
}
430430

431+
if (col >= self->num_cols)
432+
RETURN(TOO_MANY_COLS);
431433
END_FIELD(); // whitespace counts as a field
432434
}
433435
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,3 +772,16 @@ def test_empty_quotes(parallel):
772772
table = read_basic('a b\n1 ""\n2 ""', parallel=parallel)
773773
expected = Table([[1, 2], [0, 0]], names=('a', 'b'))
774774
assert_table_equal(table, expected)
775+
776+
@pytest.mark.parametrize("parallel", [True, False])
777+
def test_fast_tab_with_names(parallel):
778+
"""
779+
Make sure the C reader doesn't segfault when the header for the
780+
first column is missing [#3545]
781+
"""
782+
content = """#
783+
\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
784+
-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"""
785+
head = ['A{0}'.format(i) for i in range(28)]
786+
table = read_tab(content, data_start=1,
787+
parallel=parallel, names=head)

0 commit comments

Comments
 (0)