diff --git a/auxlily.c b/auxlily.c index 970ed15..5097176 100644 --- a/auxlily.c +++ b/auxlily.c @@ -907,6 +907,39 @@ out_of_memory(void) panic("no memory"); } +void * +(smalloc_safe)(size_t s SMALLOC_DEBUG_ARGS) +{ + void *rv; + + hold_all_sigs(); + rv = (smalloc)(s SMALLOC_DEBUG_ARGSCALL); + rele_all_sigs(); + return rv; +} + +void * +(srealloc_safe)(void *v, size_t s SMALLOC_DEBUG_ARGS) +{ + void *rv; + + hold_all_sigs(); + rv = (srealloc)(v, s SMALLOC_DEBUG_ARGSCALL); + rele_all_sigs(); + return rv; +} + +void * +(scalloc_safe)(size_t nmemb, size_t size SMALLOC_DEBUG_ARGS) +{ + void *rv; + + hold_all_sigs(); + rv = (scalloc)(nmemb, size SMALLOC_DEBUG_ARGSCALL); + rele_all_sigs(); + return rv; +} + #ifndef HAVE_ASSERTS void * smalloc(size_t s SMALLOC_DEBUG_ARGS) diff --git a/extern.h b/extern.h index df04603..9c9b448 100644 --- a/extern.h +++ b/extern.h @@ -150,7 +150,7 @@ int my_getopt(int argc, char *const argv[], const char *optstring); # define optopt my_optopt #endif -/* Memory allocation routines */ +/* Memory allocation routines; the _safe versions temporarily block signals */ #ifdef HAVE_ASSERTS # define SMALLOC_DEBUG_ARGS , char const *mdbg_file, int mdbg_line # define SMALLOC_DEBUG_ARGSCALL , mdbg_file, mdbg_line @@ -159,6 +159,9 @@ int my_getopt(int argc, char *const argv[], const char *optstring); # define SMALLOC_DEBUG_ARGSCALL #endif +void * smalloc_safe(size_t s SMALLOC_DEBUG_ARGS); +void * srealloc_safe(void *v, size_t s SMALLOC_DEBUG_ARGS); +void * scalloc_safe(size_t nmemb, size_t size SMALLOC_DEBUG_ARGS); void * smalloc(size_t s SMALLOC_DEBUG_ARGS); void * srealloc(void *v, size_t s SMALLOC_DEBUG_ARGS); void * scalloc(size_t nmemb, size_t size SMALLOC_DEBUG_ARGS); @@ -169,6 +172,9 @@ void sfree(void *v SMALLOC_DEBUG_ARGS); void smemreset(void); /* The *smemtrace* command */ int smemtrace(void *v); +# define smalloc_safe(SZ) smalloc_safe(SZ, __FILE__, __LINE__) +# define srealloc_safe(P,SZ) srealloc_safe(P, SZ, __FILE__, __LINE__) +# define scalloc_safe(N,SZ) scalloc_safe(N, SZ, __FILE__, __LINE__) # define smalloc(SZ) smalloc(SZ, __FILE__, __LINE__) # define srealloc(P,SZ) srealloc(P, SZ, __FILE__, __LINE__) # define scalloc(N,SZ) scalloc(N, SZ, __FILE__, __LINE__) diff --git a/fio.c b/fio.c index 1e360ce..f7b25f4 100644 --- a/fio.c +++ b/fio.c @@ -285,13 +285,14 @@ _fgetline_byone(char **line, size_t *linesize, size_t *llen, { int c; - if (*line == NULL || *linesize < LINESIZE + n + 1) - *line = (srealloc)(*line, *linesize = LINESIZE + n + 1 - SMALLOC_DEBUG_ARGSCALL); + assert(*linesize == 0 || *line != NULL); for (;;) { - if (n >= *linesize - 128) - *line = (srealloc)(*line, *linesize += 256 + if (*linesize <= LINESIZE || n >= *linesize - 128) { + *linesize += ((*line == NULL) + ? LINESIZE + n + 1 : 256); + *line = (srealloc_safe)(*line, *linesize SMALLOC_DEBUG_ARGSCALL); + } c = getc(fp); if (c != EOF) { (*line)[n++] = c; @@ -329,7 +330,7 @@ char * return _fgetline_byone(line, linesize, llen, fp, appendnl, 0 SMALLOC_DEBUG_ARGSCALL); if (*line == NULL || *linesize < LINESIZE) - *line = (srealloc)(*line, *linesize = LINESIZE + *line = (srealloc_safe)(*line, *linesize = LINESIZE SMALLOC_DEBUG_ARGSCALL); sz = *linesize <= *cnt ? *linesize : *cnt + 1; if (sz <= 1 || fgets(*line, sz, fp) == NULL) @@ -341,7 +342,7 @@ char * i_llen = _length_of_line(*line, sz); *cnt -= i_llen; while ((*line)[i_llen - 1] != '\n') { - *line = (srealloc)(*line, *linesize += 256 + *line = (srealloc_safe)(*line, *linesize += 256 SMALLOC_DEBUG_ARGSCALL); sz = *linesize - i_llen; sz = (sz <= *cnt ? sz : *cnt + 1); @@ -376,15 +377,14 @@ int * bypass it by read() then. */ if (fileno(ibuf) == 0 && (options & OPT_TTYIN)) { - if (*linebuf == NULL || *linesize < LINESIZE + n + 1) - *linebuf = (srealloc)(*linebuf, - *linesize = LINESIZE + n + 1 - SMALLOC_DEBUG_ARGSCALL); + assert(*linesize == 0 || *linebuf != NULL); for (;;) { - if (n >= *linesize - 128) - *linebuf = (srealloc)(*linebuf, - *linesize += 256 + if (*linesize <= LINESIZE || n >= *linesize - 128) { + *linesize += ((*linebuf == NULL) + ? LINESIZE + n + 1 : 256); + *linebuf = (srealloc_safe)(*linebuf, *linesize SMALLOC_DEBUG_ARGSCALL); + } again: sz = read(0, *linebuf + n, *linesize - n - 1); if (sz > 0) { @@ -486,7 +486,7 @@ readstr_input(char const *prompt, char const *string) /* FIXME SIGS<->leaks */ slen = (string != NULL) ? strlen(string) : 0; if (slen) { linesize = slen + LINESIZE + 1; - linebuf = smalloc(linesize); + linebuf = smalloc_safe(linesize); if (slen) memcpy(linebuf, string, slen + 1); } diff --git a/lex.c b/lex.c index 9b3ec1d..99657b9 100644 --- a/lex.c +++ b/lex.c @@ -792,6 +792,7 @@ onintr(int s) while (sourcing) unstack(); + termios_state_reset(); close_all_files(); if (image >= 0) { diff --git a/tty.c b/tty.c index 0488522..cd07de2 100644 --- a/tty.c +++ b/tty.c @@ -655,7 +655,7 @@ _ncl_check_grow(struct line *l, size_t no SMALLOC_DEBUG_ARGS) i <<= 1; *l->x_bufsize = i; l->line.cbuf = - *l->x_buf = (srealloc)(*l->x_buf, i SMALLOC_DEBUG_ARGSCALL); + *l->x_buf = (srealloc_safe)(*l->x_buf, i SMALLOC_DEBUG_ARGSCALL); } }