Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: don't increment a null pointer in set -A



In coverity CID 1255831, it warns about this code which handles set -A
and set +A. There's no actual bug here but the following change is more
efficient (thanks to arrlen_gt) and makes the code somewhat cleaner at
least in my opinion. In the case of set -A, we assign a to NULL and then
increment it with each loop. Incrementing a NULL pointer is harmless but
still a bit icky.

Unfortunately, coverity will probably still complain about this because
it can't connect the fact that (!*args) will only ever be true when a
wasn't/isn't null.

Oliver

diff --git a/Src/builtin.c b/Src/builtin.c
index fb59738f3..73cfe7ad1 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -695,13 +695,11 @@ bin_set(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))
 	char **a = NULL, **y;
 	int len = arrlen(args);
 
-	if (array < 0 && (a = getaparam(arrayname))) {
-	    int al = arrlen(a);
-
-	    if (al > len)
-		len = al;
+	if (array < 0 && (a = getaparam(arrayname)) && arrlen_gt(a, len)) {
+	    a += len;
+	    len += arrlen(a);
 	}
-	for (x = y = zalloc((len + 1) * sizeof(char *)); len--; a++) {
+	for (x = y = zalloc((len + 1) * sizeof(char *)); len--;) {
 	    if (!*args)
 		args = a;
 	    *y++ = ztrdup(*args++);



Messages sorted by: Reverse Date, Date, Thread, Author