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

Double free in freopen on OOM #354

Closed
pfusik opened this issue Aug 11, 2024 · 0 comments
Closed

Double free in freopen on OOM #354

pfusik opened this issue Aug 11, 2024 · 0 comments

Comments

@pfusik
Copy link
Contributor

pfusik commented Aug 11, 2024

static FILE *init_file(FILE *stream) {
  ...
  if ((rc->buffer = (char *)malloc(BUFSIZ)) == NULL) {
    /* No memory */
    free(rc);
    return NULL;
  }
FILE *freopen(const char *restrict filename, const char *restrict mode,
              FILE *restrict stream) {
  ...
  if (init_file(stream) == NULL) {
    /* Re-init failed. */
    free(stream->filename);
    free(stream);
    return NULL;
  }

Wondering if free(stream) can attempt to free the memory just freed by free(rc) ?

pfusik added a commit to pfusik/llvm-mos-sdk that referenced this issue Aug 21, 2024
Rewrite `freopen` to not duplicate `fclose` and `fopen` code.
Reuse the file list and possibly the buffer in `freopen`.

Fix llvm-mos#354
@pfusik pfusik mentioned this issue Aug 21, 2024
pfusik added a commit to pfusik/llvm-mos-sdk that referenced this issue Aug 22, 2024
* Improve error handling in `fclose`

* Rewrite `freopen` to not duplicate `fclose` and `fopen` code

* Make `freopen` more performant by reusing the file list and possibly
  the buffer

* Fix double free in `freopen` on OOM (llvm-mos#354)

Fix llvm-mos#354
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

No branches or pull requests

1 participant