Skip to content

Conversation

@nmcclatchey
Copy link

Fixes resource leaks in npy_load() and npz_load() that would occur if an exception were to be thrown during load. Because NPY files can be constructed deliberately to trigger such exceptions, addressing this leak is vital for cases where programs may load user-supplied NPY files and attempt recovery on error.

Fixes issue rogersce#53, which notes that an exception thrown when loading an npz file would cause an open FILE pointer to be leaked. This bug could be triggered intentionally (via specially-crafted NPZ files), so fixing it is a priority.
Fixes issue rogersce#52, which notes that an exception thrown when loading an npy file would cause an open FILE pointer to be leaked. This bug could be triggered intentionally (via specially-crafted NPY files), so fixing it is a priority.
@nmcclatchey
Copy link
Author

Note: Resolves #52 and #53 .

@s-trinh
Copy link

s-trinh commented Nov 8, 2025

Thanks for the code. I have used it with the following changes:

  FILE *fp;
  AutoCloser() : fp(nullptr) { }
  ~AutoCloser()
  {
    if (fp != nullptr) {
      fclose(fp);
    }
  }
};

I have added the constructor, otherwise my compiler complains about fp not being initialized.
The fp != nullptr is probably useless but it looks like sending NULL (so also nullptr?) to fclose is undefined behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants