Home
checking for malloc failure - iomenu - interactive terminal-based selection menu HTML git clone git://bitreich.org/iomenu git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/iomenu DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit 8be3cf653dc554954a4170bd057a33cf2e8a0244 DIR parent 3f19ac319ee4156f4728b5d496788b632281a9b2 HTML Author: Josuah Demangeonā ā µ <mail@josuah.net> Date: Sat, 18 Mar 2017 00:07:17 +0100 checking for malloc failure Diffstat: M Makefile | 2 +- M iomenu.c | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) --- DIR diff --git a/Makefile b/Makefile @@ -1,4 +1,4 @@ -CFLAGS = -std=c99 -Wpedantic -Wall -Wextra -g -static +CFLAGS = -std=c99 -Wpedantic -Wall -Wextra -g -static -O0 OBJ = ${SRC:.c=.o} all: clean iomenu DIR diff --git a/iomenu.c b/iomenu.c @@ -73,11 +73,13 @@ void read_lines(void) { extern struct line **linev; + extern size_t linec, matching; char s[BUFSIZ]; size_t size = 1 << 4; - linev = malloc(sizeof(struct line *) * size); + if (!(linev = malloc(sizeof(struct line *) * size))) + die("malloc"); linev[0] = NULL; /* read the file into an array of lines */ @@ -95,10 +97,13 @@ read_lines(void) die("realloc"); } - linev[linec] = malloc(sizeof(struct line)); - linev[linec]->match = 1; - linev[linec]->text = malloc(len); + if (!(linev[linec] = malloc(sizeof(struct line)))) + die("malloc"); + if (!(linev[linec]->text = malloc(len))) + die("malloc"); strcpy(linev[linec]->text, s); + + linev[linec]->match = 1; } } @@ -202,11 +207,13 @@ draw_column(size_t pos, size_t col, size_t cols) fputs(pos == current ? "\033[30;47m " : " ", stderr); for (size_t i = 0; col < cols ;) { - size_t len = mblen(linev[pos]->text + i, BUFSIZ - i); + int len = mblen(linev[pos]->text + i, BUFSIZ - i); - if (len == 0) { + if (len < 0) { i++; continue; + } else if (len == 0) { + break; } col += linev[pos]->text[i] = '\t' ? pos + 1 % 8 : 1;