Skip to content
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

gf_getfld() leaks memory on error #189

Closed
edwardhartnett opened this issue Nov 17, 2021 · 0 comments · Fixed by #190
Closed

gf_getfld() leaks memory on error #189

edwardhartnett opened this issue Nov 17, 2021 · 0 comments · Fixed by #190
Assignees
Labels
bug Something isn't working

Comments

@edwardhartnett
Copy link
Contributor

edwardhartnett commented Nov 17, 2021

In gf_getfld() we have:

!         If found Section 1, decode elements in Identification Section.
          if (isecnum.eq.1) then
              iofst = iofst-40    ! reset offset to beginning of section
              call gf_unpack1(cgrib, lcgrib, iofst, gfld%idsect,
     &             gfld%idsectlen, jerr)
              if (jerr.ne.0) then
                  ierr = 15
                  return
              endif
          endif

!         If found Section 2, Grab local section. Save in case this is
!         the latest one before the requested field.
          if (isecnum.eq.2) then
              iofst = iofst-40    ! reset offset to beginning of section
              if (associated(gfld%local)) deallocate(gfld%local)
              call gf_unpack2(cgrib, lcgrib, iofst, gfld%locallen,
     &             gfld%local, jerr)
              if (jerr.ne.0) then
                  ierr = 16
                  return
              endif
          endif

The gf_unpack1() function allocates an array in gfld. If the function later exits, this memory is not freed and causes a leak.

The solution is to call gf_free() on gfld before an error exit.

There are a bunch of currently commented-out tests that demonstrate this bug in test_getfield.f90.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant