tzcode: Avoid memory leak if pthread_setspecific() fails.

Reported by:	Coverity (CID 1018472, 1018474)
MFC after:	1 week
Sponsored by:	Klara, Inc.
Differential Revision: https://reviews.freebsd.org/D38036
This commit is contained in:
Dag-Erling Smørgrav 2023-01-13 15:57:19 +01:00
parent 3e2e5eebfa
commit 96e68c393f

View File

@ -1761,7 +1761,10 @@ localtime(const time_t *timep)
if ((p_tm = malloc(sizeof(*p_tm))) == NULL) {
return (NULL);
}
_pthread_setspecific(localtime_key, p_tm);
if (_pthread_setspecific(localtime_key, p_tm) != 0) {
free(p_tm);
return (NULL);
}
}
}
return localtime_tzset(timep, p_tm, true);
@ -1829,7 +1832,10 @@ gmtime(const time_t *timep)
if ((p_tm = malloc(sizeof(*p_tm))) == NULL) {
return (NULL);
}
_pthread_setspecific(gmtime_key, p_tm);
if (_pthread_setspecific(gmtime_key, p_tm) != 0) {
free(p_tm);
return (NULL);
}
}
}
return gmtime_r(timep, p_tm);