Better free() tracking in constructor failure cases
This commit is contained in:
parent
286532e602
commit
2b08f88f19
|
@ -37,6 +37,8 @@ static void *new_driver(termkey_t *tk, const char *term)
|
|||
// Excellent - we'll continue
|
||||
|
||||
termkey_csi *csi = malloc(sizeof *csi);
|
||||
if(!csi)
|
||||
return NULL;
|
||||
|
||||
csi->tk = tk;
|
||||
|
||||
|
@ -51,6 +53,8 @@ static void *new_driver(termkey_t *tk, const char *term)
|
|||
csi->ncsifuncs = 32;
|
||||
|
||||
csi->csifuncs = malloc(sizeof(csi->csifuncs[0]) * csi->ncsifuncs);
|
||||
if(!csi->csifuncs)
|
||||
goto abort_free_csi;
|
||||
|
||||
for(i = 0; i < csi->ncsifuncs; i++)
|
||||
csi->csifuncs[i].sym = TERMKEY_SYM_UNKNOWN;
|
||||
|
@ -119,6 +123,11 @@ static void *new_driver(termkey_t *tk, const char *term)
|
|||
register_csifunc(csi, TERMKEY_TYPE_FUNCTION, 20, 34, NULL);
|
||||
|
||||
return csi;
|
||||
|
||||
abort_free_csi:
|
||||
free(csi);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void free_driver(void *private)
|
||||
|
|
|
@ -33,6 +33,8 @@ static void *new_driver(termkey_t *tk, const char *term)
|
|||
return NULL;
|
||||
|
||||
termkey_ti *ti = malloc(sizeof *ti);
|
||||
if(!ti)
|
||||
return NULL;
|
||||
|
||||
ti->tk = tk;
|
||||
|
||||
|
@ -40,6 +42,8 @@ static void *new_driver(termkey_t *tk, const char *term)
|
|||
ti->nseqs = 0;
|
||||
|
||||
ti->seqs = malloc(ti->alloced_seqs * sizeof(ti->seqs[0]));
|
||||
if(!ti->seqs)
|
||||
goto abort_free_ti;
|
||||
|
||||
int i;
|
||||
for(i = 0; strfnames[i]; i++) {
|
||||
|
@ -64,6 +68,11 @@ static void *new_driver(termkey_t *tk, const char *term)
|
|||
}
|
||||
|
||||
return ti;
|
||||
|
||||
abort_free_ti:
|
||||
free(ti);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void free_driver(void *private)
|
||||
|
|
21
termkey.c
21
termkey.c
|
@ -96,10 +96,8 @@ termkey_t *termkey_new_full(int fd, int flags, size_t buffsize, int waittime)
|
|||
tk->flags = flags;
|
||||
|
||||
tk->buffer = malloc(buffsize);
|
||||
if(!tk->buffer) {
|
||||
free(tk);
|
||||
return NULL;
|
||||
}
|
||||
if(!tk->buffer)
|
||||
goto abort_free_tk;
|
||||
|
||||
tk->buffstart = 0;
|
||||
tk->buffcount = 0;
|
||||
|
@ -113,6 +111,8 @@ termkey_t *termkey_new_full(int fd, int flags, size_t buffsize, int waittime)
|
|||
|
||||
tk->nkeynames = 64;
|
||||
tk->keynames = malloc(sizeof(tk->keynames[0]) * tk->nkeynames);
|
||||
if(!tk->keynames)
|
||||
goto abort_free_buffer;
|
||||
|
||||
int i;
|
||||
for(i = 0; i < tk->nkeynames; i++)
|
||||
|
@ -147,7 +147,7 @@ termkey_t *termkey_new_full(int fd, int flags, size_t buffsize, int waittime)
|
|||
|
||||
if(!tk->driver_info) {
|
||||
fprintf(stderr, "Unable to find a terminal driver\n");
|
||||
return NULL;
|
||||
goto abort_free_keynames;
|
||||
}
|
||||
|
||||
if(!(flags & TERMKEY_FLAG_NOTERMIOS)) {
|
||||
|
@ -164,6 +164,17 @@ termkey_t *termkey_new_full(int fd, int flags, size_t buffsize, int waittime)
|
|||
}
|
||||
|
||||
return tk;
|
||||
|
||||
abort_free_keynames:
|
||||
free(tk->keynames);
|
||||
|
||||
abort_free_buffer:
|
||||
free(tk->buffer);
|
||||
|
||||
abort_free_tk:
|
||||
free(tk);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
termkey_t *termkey_new(int fd, int flags)
|
||||
|
|
Loading…
Reference in New Issue