[PATCH] one issue found by Coverity

Dennis Preiser <[email protected]> Thu, 7 Nov 2013 18:23:19 +0100
Newsgroups gmane.network.tin.devel
Message-ID <[email protected]>
--1yeeQ81UyVL57Vl7
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

CID 1125483, dereference null return value in string.c:wstrunc()

Dennis

--1yeeQ81UyVL57Vl7
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="2013-11-07.diff"

diff -urp tin-2.1.4_r1/src/string.c tin-2.1.4_r2/src/string.c
--- tin-2.1.4_r1/src/string.c	2013-10-11 12:38:08.000000000 +0200
+++ tin-2.1.4_r2/src/string.c	2013-11-07 17:39:17.000000000 +0100
@@ -1069,6 +1069,7 @@ wstrunc(
 
 	if (wcswidth(wtmp, wcslen(wtmp)) > len) {
 		/* wtmp must be truncated */
+		size_t len_tail;
 		wchar_t *wtmp2, *tail;
 
 		if (tinrc.utf8_graphics) {
@@ -1082,10 +1083,11 @@ wstrunc(
 		} else
 			tail = char2wchar_t(TRUNC_TAIL);
 
-		wtmp2 = wcspart(wtmp, len - wcslen(tail), FALSE);
+		len_tail = tail ? wcslen(tail) : 0;
+		wtmp2 = wcspart(wtmp, len - len_tail, FALSE);
 		free(wtmp);
-		wtmp = my_realloc(wtmp2, sizeof(wchar_t) * (wcslen(wtmp2) + wcslen(tail) + 1));	/* wtmp2 isn't valid snymore and doesn't have to be free()ed */
-		wcscat(wtmp, tail);
+		wtmp = my_realloc(wtmp2, sizeof(wchar_t) * (wcslen(wtmp2) + len_tail + 1));	/* wtmp2 isn't valid snymore and doesn't have to be free()ed */
+		wcscat(wtmp, tail ? tail : L"");
 		free(tail);
 	}
 

--1yeeQ81UyVL57Vl7--