Skip to content

Heap corruption when reading unstructured CGNS: cg_elements_read_f writes into intType connectivity buffer #42

@bmlowe1

Description

@bmlowe1

Description

I’m seeing nondeterministic heap corruption (e.g. corrupt size vs prev_size) when pysurf reads unstructured CGNS files. The crash shows up around the second zone iteration (often on deallocate), but the root cause appears to be earlier memory corruption in the connectivity read.

cg_elements_read_f seems to expect an integer(cgsize_t) buffer, but pySurf passes zones(iZone)%sections(sec)%elemConn (declared as integer(intType)) directly. When cgsize_t is 64-bit and intType is 32-bit, CGNS writes past the buffer / corrupts the heap.

zones(iZone)%sections(sec)%elemConn, CG_Null, ierr)

Workaround / Fix

Reading element connectivity into a temporary cgsize_t array and then assigning fixes it:

integer(kind=cgsize_t), allocatable :: elements(:)

if (allocated(elements)) deallocate(elements)
allocate(elements(nConn*nElem))
call cg_elements_read_f(cg, base, iZone, sec, elements, CG_Null, ierr)
if (ierr .eq. CG_ERROR) call cg_error_exit_f

zones(iZone)%sections(sec)%elemConn = int(elements, kind=intType) + zoneStart

(Original code passed zones(iZone)%sections(sec)%elemConn directly to cg_elements_read_f.)

Suggestion

Either store elemConn as cgsize_t internally, or always read into a cgsize_t temporary and convert to intType afterward.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions