Skip to content

Commit

Permalink
Check for errors with realloc and remove unnecessary malloc import
Browse files Browse the repository at this point in the history
  • Loading branch information
aresch committed Jun 7, 2016
1 parent 92e1ebc commit d3c779b
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion rencode/rencode.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ if py3:
unicode = str

from cpython cimport bool
from libc.stdlib cimport realloc, malloc, free
from libc.stdlib cimport realloc, free
from libc.string cimport memcpy

__version__ = ("Cython", 1, 0, 4)
Expand Down Expand Up @@ -151,11 +151,15 @@ cdef swap_byte_order_double(char *c):

cdef write_buffer_char(char **buf, unsigned int *pos, char c):
buf[0] = <char*>realloc(buf[0], pos[0] + 1)
if buf[0] == NULL:
raise MemoryError("Error in realloc, 1 byte needed")
memcpy(&buf[0][pos[0]], &c, 1)
pos[0] += 1

cdef write_buffer(char **buf, unsigned int *pos, void* data, int size):
buf[0] = <char*>realloc(buf[0], pos[0] + size)
if buf[0] == NULL:
raise MemoryError("Error in realloc, %d bytes needed", size)
memcpy(&buf[0][pos[0]], data, size)
pos[0] += size

Expand Down

0 comments on commit d3c779b

Please sign in to comment.