@@ -245,7 +245,8 @@ cdef class VarReader5:
245245
246246 Does necessary swapping and takes account of SDE formats
247247
248- Data may be returned in data_ptr, if this was an SDE
248+ Data may be returned in data_ptr, if this was an SDE.
249+ data_ptr must point to a buffer of size >= 4 bytes.
249250
250251 Returns 1 for success, full format; 2 for success, SDE format; -1
251252 if error arises
@@ -365,7 +366,8 @@ cdef class VarReader5:
365366 cdef int read_element_into (self ,
366367 cnp .uint32_t * mdtype_ptr ,
367368 cnp .uint32_t * byte_count_ptr ,
368- void * ptr ) except - 1 :
369+ void * ptr ,
370+ cnp .uint32_t max_byte_count ) except - 1 :
369371 ''' Read element into pre-allocated memory in `ptr`
370372
371373 Parameters
@@ -375,8 +377,9 @@ cdef class VarReader5:
375377 byte_count_ptr : uint32_t*
376378 pointer to uint32_t value to which we write the byte count
377379 ptr : void*
378- memory location into which to read. Memory is assumed large
379- enough to contain read data
380+ memory buffer into which to read
381+ max_byte_count : uint32_t
382+ size of the buffer pointed to by ptr
380383
381384 Returns
382385 -------
@@ -388,12 +391,16 @@ cdef class VarReader5:
388391 '''
389392 cdef :
390393 int mod8
394+ if max_byte_count < 4 :
395+ raise ValueError ('Unexpected amount of data to read (malformed input file?)' )
391396 cdef int res = self .cread_tag (
392397 mdtype_ptr ,
393398 byte_count_ptr ,
394399 < char * > ptr )
395400 cdef cnp .uint32_t byte_count = byte_count_ptr [0 ]
396401 if res == 1 : # full format
402+ if byte_count > max_byte_count :
403+ raise ValueError ('Unexpected amount of data to read (malformed input file?)' )
397404 res = self .cstream .read_into (ptr , byte_count )
398405 # Seek to next 64-bit boundary
399406 mod8 = byte_count % 8
@@ -485,14 +492,15 @@ cdef class VarReader5:
485492 raise TypeError ('Expecting miINT8 as data type' )
486493 return data
487494
488- cdef int read_into_int32s (self , cnp .int32_t * int32p ) except - 1 :
495+ cdef int read_into_int32s (self , cnp .int32_t * int32p , cnp . uint32_t max_byte_count ) except - 1 :
489496 ''' Read int32 values into pre-allocated memory
490497
491498 Byteswap as necessary. Specializes ``read_element_into``
492499
493500 Parameters
494501 ----------
495502 int32p : int32 pointer
503+ max_count : uint32_t
496504
497505 Returns
498506 -------
@@ -502,7 +510,7 @@ cdef class VarReader5:
502510 cdef :
503511 cnp .uint32_t mdtype , byte_count , n_ints
504512 int i , check_ints = 0
505- self .read_element_into (& mdtype , & byte_count , < void * > int32p )
513+ self .read_element_into (& mdtype , & byte_count , < void * > int32p , max_byte_count )
506514 if mdtype == miUINT32 :
507515 check_ints = 1
508516 elif mdtype != miINT32 :
@@ -596,7 +604,7 @@ cdef class VarReader5:
596604 header .name = None
597605 header .dims = None
598606 return header
599- header .n_dims = self .read_into_int32s (header .dims_ptr )
607+ header .n_dims = self .read_into_int32s (header .dims_ptr , sizeof ( header . dims_ptr ) )
600608 if header .n_dims > _MAT_MAXDIMS :
601609 raise ValueError ('Too many dimensions (%d) for numpy arrays'
602610 % header .n_dims )
@@ -896,7 +904,7 @@ cdef class VarReader5:
896904 list field_names
897905 object name
898906 # Read field names into list
899- cdef int res = self .read_into_int32s (& namelength )
907+ cdef int res = self .read_into_int32s (& namelength , 4 )
900908 if res != 1 :
901909 raise ValueError ('Only one value for namelength' )
902910 cdef object names = self .read_int8_string ()
0 commit comments