Skip to content

BUG: Fixed grow_buffer to grow when capacity is reached #12504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1199,3 +1199,4 @@ Bug Fixes
- Bug in ``DataFrame.apply`` in which reduction was not being prevented for cases in which ``dtype`` was not a numpy dtype (:issue:`12244`)
- Bug when initializing categorical series with a scalar value. (:issue:`12336`)
- Bug when specifying a UTC ``DatetimeIndex`` by setting ``utc=True`` in ``.to_datetime`` (:issue:`11934`)
- Bug when increasing the buffer size of CSV reader in ``read_csv`` (:issue:`12494`)
20 changes: 20 additions & 0 deletions pandas/io/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2635,6 +2635,26 @@ def test_eof_states(self):
self.assertRaises(Exception, self.read_csv,
StringIO(data), escapechar='\\')

def test_grow_boundary_at_cap(self):
# See gh-12494
#
# Cause of error was the fact that pandas
# was not increasing the buffer size when
# the desired space would fill the buffer
# to capacity, which later would cause a
# buffer overflow error when checking the
# EOF terminator of the CSV stream
def test_empty_header_read(count):
s = StringIO(',' * count)
expected = DataFrame(columns=[
'Unnamed: {i}'.format(i=i)
for i in range(count + 1)])
df = read_csv(s)
tm.assert_frame_equal(df, expected)

for count in range(1, 101):
test_empty_header_read(count)


class TestPythonParser(ParserTests, tm.TestCase):

Expand Down
2 changes: 1 addition & 1 deletion pandas/src/parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ static void *grow_buffer(void *buffer, int length, int *capacity,
void *newbuffer = buffer;

// Can we fit potentially nbytes tokens (+ null terminators) in the stream?
while ( (length + space > cap) && (newbuffer != NULL) ){
while ( (length + space >= cap) && (newbuffer != NULL) ){
cap = cap? cap << 1 : 2;
buffer = newbuffer;
newbuffer = safe_realloc(newbuffer, elsize * cap);
Expand Down