From 6c9b59a9c20c1422346f74ae3cd558f3317deb6a Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Fri, 2 Jun 2023 20:11:36 +0200
Subject: [PATCH] setenv: Don't crash if malloc() returns NULL.

* lib/setenv.c (rpl_setenv): Check malloca() return value.

Reference:https://github.com/coreutils/gnulib/commit/6c9b59a9c20c1422346f74ae3cd558f3317deb6a
Conflict:Changelog Context adaptation

---
 ChangeLog    | 5 +++++
 lib/setenv.c | 5 +++++
 2 files changed, 10 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 869096eb41..48fe27441e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2023-06-02  Bruno Haible  <bruno@clisp.org>
+
+	setenv: Don't crash if malloc() returns NULL.
+	* lib/setenv.c (rpl_setenv): Check malloca() return value.
+
 2023-02-27  ChuanGang Jiang  <jiangchuanganghw@outlook.com>
 
        fts: fail gracefully when out of memory
diff --git a/lib/setenv.c b/lib/setenv.c
index f0b889969f..22b12fd018 100644
--- a/lib/setenv.c
+++ b/lib/setenv.c
@@ -375,6 +375,11 @@ rpl_setenv (const char *name, const char *value, int replace)
           int saved_errno;
           size_t len = strlen (value);
           tmp = malloca (len + 2);
+          if (tmp == NULL)
+            {
+              errno = ENOMEM;
+              return -1;
+            }
           /* Since leading '=' is eaten, double it up.  */
           *tmp = '=';
           memcpy (tmp + 1, value, len + 1);
-- 
2.27.0

