From ab7093775fc639d12c9f056a1cd3c935591db46f Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 13 Mar 2017 14:44:22 +0100 Subject: [PATCH] lib/colors: assert rathen than overflow [-Wnull-dereference] lib/colors.c: In function 'colors_read_schemes': lib/colors.c:412:7: warning: potential null pointer dereference [-Wnull-dereference] Reported-by: Sami Kerola Signed-off-by: Karel Zak --- lib/colors.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/colors.c b/lib/colors.c index 4b280fe27..dda617057 100644 --- a/lib/colors.c +++ b/lib/colors.c @@ -342,6 +342,7 @@ static char *colors_get_homedir(char *buf, size_t bufsz) static int cn_sequence(const char *str, char **seq) { char *in, *out; + int len; if (!str) return -EINVAL; @@ -357,7 +358,7 @@ static int cn_sequence(const char *str, char **seq) } /* convert xx;yy sequences to "\033[xx;yy" */ - if (asprintf(seq, "\033[%sm", str) < 1) + if ((len = asprintf(seq, "\033[%sm", str)) < 1) return -ENOMEM; for (in = *seq, out = *seq; in && *in; in++) { @@ -409,6 +410,8 @@ static int cn_sequence(const char *str, char **seq) } in++; } + + assert ((out - *seq) <= len); *out = '\0'; return 0;