From 91bb8c995f977d289077e6a6dceff74f4aed60b6 Mon Sep 17 00:00:00 2001
From: Arthur de Jong <arthur@arthurdejong.org>
Date: Tue, 27 Aug 2024 21:20:29 +0200
Subject: [PATCH] Fix NULL pointer deref on memory allocation failure

This fixes a NULL pointer dereference when a call to malloc() failed.

Closes https://github.com/arthurdejong/nss-pam-ldapd/issues/70
---
 nslcd/passwd.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/nslcd/passwd.c b/nslcd/passwd.c
index a4e2678..59b21d0 100644
--- a/nslcd/passwd.c
+++ b/nslcd/passwd.c
@@ -5,7 +5,7 @@
 
    Copyright (C) 1997-2005 Luke Howard
    Copyright (C) 2006 West Consulting
-   Copyright (C) 2006-2017 Arthur de Jong
+   Copyright (C) 2006-2024 Arthur de Jong
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -293,8 +293,16 @@ char *dn2uid(MYLDAP_SESSION *session, const char *dn, char *buf, size_t buflen)
   /* see if we have a cached entry */
   pthread_mutex_lock(&dn2uid_cache_mutex);
   if (dn2uid_cache == NULL)
+  {
     dn2uid_cache = dict_new();
-  if ((dn2uid_cache != NULL) && ((cacheentry = dict_get(dn2uid_cache, dn)) != NULL))
+    if (dn2uid_cache == NULL)
+    {
+      log_log(LOG_ERR, "dict_new() failed to allocate memory");
+      pthread_mutex_unlock(&dn2uid_cache_mutex);
+      return NULL;
+    }
+  }
+  if ((cacheentry = dict_get(dn2uid_cache, dn)) != NULL)
   {
     if ((cacheentry->uid != NULL) && (strlen(cacheentry->uid) < buflen))
     {
-- 
2.33.0

