Fix memory leak in edithost

The problem is that when the parameter 'pat' is null, the function locally
allocates a NULL string but never frees it.

Instead of tracking the local alloc, it is noted that the while(*pat) never
enters when there is a local alloc.
So instead of doing the local alloc, check that 'pat' is null before the
while(*pat) loop.

Found using clang's static analyzer - scan-build

Submitted by:	Thomas Rix <trix@juniper.net>
Reviewed by:	markm
Approved by:	sjg (mentor)
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D9689
This commit is contained in:
Stephen J. Kiernan 2017-06-01 19:21:30 +00:00
parent 1431a74845
commit 3fab177f90
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=319453

View file

@ -360,30 +360,30 @@ edithost(char *pat, char *host)
{
char *res = editedhost;
if (!pat)
pat = strdup("");
while (*pat) {
switch (*pat) {
if (pat) {
while (*pat) {
switch (*pat) {
case '#':
if (*host)
host++;
break;
case '#':
if (*host)
host++;
break;
case '@':
if (*host)
*res++ = *host++;
break;
case '@':
if (*host)
*res++ = *host++;
break;
default:
*res++ = *pat;
break;
default:
*res++ = *pat;
break;
}
if (res == &editedhost[sizeof editedhost - 1]) {
*res = '\0';
return;
}
pat++;
}
if (res == &editedhost[sizeof editedhost - 1]) {
*res = '\0';
return;
}
pat++;
}
if (*host)
(void) strncpy(res, host,