From df6e714f3f92c757cdc9e69383cc56d4ba52c5bd Mon Sep 17 00:00:00 2001 From: Mark Nudelman Date: Sat, 17 Apr 2021 10:47:16 -0700 Subject: [PATCH] Fix some compiler and analysis tool warnings. --- ch.c | 2 +- filename.c | 2 +- lesskey.c | 18 ++++++++++++++++-- lesskey_parse.c | 5 +++-- optfunc.c | 9 +++++---- screen.c | 2 +- 6 files changed, 27 insertions(+), 11 deletions(-) diff --git a/ch.c b/ch.c index 1559a836..d0db474c 100644 --- a/ch.c +++ b/ch.c @@ -853,7 +853,7 @@ ch_init(f, flags) * Allocate and initialize a new filestate. */ thisfile = (struct filestate *) - calloc(1, sizeof(struct filestate)); + ecalloc(1, sizeof(struct filestate)); thisfile->buflist.next = thisfile->buflist.prev = END_OF_CHAIN; thisfile->nbufs = 0; thisfile->flags = flags; diff --git a/filename.c b/filename.c index 3132baaf..c7e3bf08 100644 --- a/filename.c +++ b/filename.c @@ -270,7 +270,7 @@ homefile(filename) /* * Look for the file anywhere on search path. */ - pathname = (char *) calloc(_MAX_PATH, sizeof(char)); + pathname = (char *) ecalloc(_MAX_PATH, sizeof(char)); #if MSDOS_COMPILER==DJGPPC { char *res = searchpath(filename); diff --git a/lesskey.c b/lesskey.c index 8403a9be..90e2a5e5 100644 --- a/lesskey.c +++ b/lesskey.c @@ -115,6 +115,20 @@ lesskey_parse_error(s) fprintf(stderr, "%s\n", s); } + void * +ecalloc(count, size) + int count; + unsigned int size; +{ + void *p; + + p = calloc(count, size); + if (p != NULL) + return (p); + fprintf(stderr, "lesskey: cannot allocate %d bytes of memory\n", count*size); + exit(1); +} + static char * mkpathname(dirname, filename) char *dirname; @@ -122,7 +136,7 @@ mkpathname(dirname, filename) { char *pathname; - pathname = calloc(strlen(dirname) + strlen(filename) + 2, sizeof(char)); + pathname = ecalloc(strlen(dirname) + strlen(filename) + 2, sizeof(char)); strcpy(pathname, dirname); strcat(pathname, PATHNAME_SEP); strcat(pathname, filename); @@ -284,7 +298,7 @@ main(argc, argv) char *path = getenv("HOMEPATH"); if (drive != NULL && path != NULL) { - char *env = (char *) calloc(strlen(drive) + + char *env = (char *) ecalloc(strlen(drive) + strlen(path) + 6, sizeof(char)); strcpy(env, "HOME="); strcat(env, drive); diff --git a/lesskey_parse.c b/lesskey_parse.c index a6503269..3190cd11 100644 --- a/lesskey_parse.c +++ b/lesskey_parse.c @@ -10,6 +10,7 @@ extern void lesskey_parse_error(char *msg); extern char *homefile(char *filename); +extern void *ecalloc(int count, unsigned int size); static int linenum; static int errors; @@ -131,7 +132,7 @@ xbuf_init(xbuf) struct xbuffer *xbuf; { xbuf->size = 16; - xbuf->data = calloc(xbuf->size, sizeof(char)); + xbuf->data = ecalloc(xbuf->size, sizeof(char)); xbuf->end = 0; } @@ -146,7 +147,7 @@ xbuf_add(xbuf, ch) if (xbuf->end >= xbuf->size) { xbuf->size = xbuf->size * 2; - char *data = calloc(xbuf->size, sizeof(char)); + char *data = ecalloc(xbuf->size, sizeof(char)); memcpy(data, xbuf->data, xbuf->end); free(xbuf->data); xbuf->data = data; diff --git a/optfunc.c b/optfunc.c index b22632b7..23a94d86 100644 --- a/optfunc.c +++ b/optfunc.c @@ -156,7 +156,7 @@ opt_j(type, s) char *s; { PARG parg; - char buf[16]; + char buf[24]; int len; int err; @@ -192,7 +192,7 @@ opt_j(type, s) } else { - sprintf(buf, ".%06ld", jump_sline_fraction); + SNPRINTF1(buf, sizeof(buf), ".%06ld", jump_sline_fraction); len = (int) strlen(buf); while (len > 2 && buf[len-1] == '0') len--; @@ -221,7 +221,7 @@ opt_shift(type, s) char *s; { PARG parg; - char buf[16]; + char buf[24]; int len; int err; @@ -257,7 +257,7 @@ opt_shift(type, s) } else { - sprintf(buf, ".%06ld", shift_count_fraction); + SNPRINTF1(buf, sizeof(buf), ".%06ld", shift_count_fraction); len = (int) strlen(buf); while (len > 2 && buf[len-1] == '0') len--; @@ -268,6 +268,7 @@ opt_shift(type, s) break; } } + public void calc_shift_count(VOID_PARAM) { diff --git a/screen.c b/screen.c index 78428e67..47f597ee 100644 --- a/screen.c +++ b/screen.c @@ -2519,7 +2519,7 @@ tput_fmt(fmt, color, f_putc) int color; int (*f_putc)(int); { - char buf[16]; + char buf[32]; if (color == attrcolor) return; SNPRINTF1(buf, sizeof(buf), fmt, color);