git: 96e68c393f65 - main - tzcode: Avoid memory leak if pthread_setspecific() fails.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Sat, 14 Jan 2023 17:20:06 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=96e68c393f65046c0cf69d29f89efcfee949e5de

commit 96e68c393f65046c0cf69d29f89efcfee949e5de
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2023-01-13 14:57:19 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2023-01-14 17:16:30 +0000

    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
---
 contrib/tzcode/localtime.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/contrib/tzcode/localtime.c b/contrib/tzcode/localtime.c
index 30ae18a965e8..1b52e26a5faf 100644
--- a/contrib/tzcode/localtime.c
+++ b/contrib/tzcode/localtime.c
@@ -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);