From 004b9f5864086c09c72dd840356ef4adf57250bd Mon Sep 17 00:00:00 2001 From: Bruno Haible 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. --- ChangeLog | 5 +++++ lib/setenv.c | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index a133b4c6b5..fcdea885de 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2023-06-02 Bruno Haible + + setenv: Don't crash if malloc() returns NULL. + * lib/setenv.c (rpl_setenv): Check malloca() return value. + 2023-06-01 Bruno Haible getprogname: Add support for ASCII-compatible environments in z/OS. diff --git a/lib/setenv.c b/lib/setenv.c index ebfd4e550e..22b12fd018 100644 --- a/lib/setenv.c +++ b/lib/setenv.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1992, 1995-2003, 2005-2022 Free Software Foundation, Inc. +/* Copyright (C) 1992, 1995-2003, 2005-2023 Free Software Foundation, Inc. This file is part of the GNU C Library. This file is free software: you can redistribute it and/or modify @@ -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.39.5