From 084d5d667397a562b34296c78c89ea9f40e71433 Mon Sep 17 00:00:00 2001 From: nick black Date: Mon, 12 Apr 2021 03:23:55 -0400 Subject: [PATCH] notcurses_init: free memstream on error paths --- src/lib/notcurses.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/lib/notcurses.c b/src/lib/notcurses.c index 241304b3d..38f669f89 100644 --- a/src/lib/notcurses.c +++ b/src/lib/notcurses.c @@ -932,6 +932,8 @@ notcurses* notcurses_core_init(const notcurses_options* opts, FILE* outfp){ if(ret == NULL){ return ret; } + ret->rstate.mstream = NULL; + ret->rstate.mstreamfp = NULL; ret->loglevel = opts->loglevel; if(!(opts->flags & NCOPTION_INHIBIT_SETLOCALE)){ init_lang(ret); @@ -945,7 +947,6 @@ notcurses* notcurses_core_init(const notcurses_options* opts, FILE* outfp){ }else{ fprintf(stderr, "Encoding (\"%s\") was neither ANSI_X3.4-1968 nor UTF-8, refusing to start\n Did you call setlocale()?\n", encoding ? encoding : "none found"); - free(ret); return NULL; } if(outfp == NULL){ @@ -1041,6 +1042,7 @@ notcurses* notcurses_core_init(const notcurses_options* opts, FILE* outfp){ goto err; } if(ret->ttyfd >= 0){ + reset_term_attributes(ret); if(!(opts->flags & NCOPTION_NO_CLEAR_BITMAPS)){ if(sprite_init(ret)){ free_plane(ret->stdplane); @@ -1055,7 +1057,6 @@ notcurses* notcurses_core_init(const notcurses_options* opts, FILE* outfp){ free_plane(ret->stdplane); goto err; } - reset_term_attributes(ret); } if((ret->rstate.mstreamfp = open_memstream(&ret->rstate.mstream, &ret->rstate.mstrsize)) == NULL){ free_plane(ret->stdplane); @@ -1088,6 +1089,10 @@ notcurses* notcurses_core_init(const notcurses_options* opts, FILE* outfp){ err: fprintf(stderr, "Alas, you will not be going to space today.\n"); // FIXME looks like we have some memory leaks on this error path? + if(ret->rstate.mstreamfp){ + fclose(ret->rstate.mstreamfp); + } + free(ret->rstate.mstream); tcsetattr(ret->ttyfd, TCSANOW, &ret->tpreserved); drop_signals(ret); pthread_mutex_destroy(&ret->statlock);