diff mbox series

[pseudo,2/3] pseudo_setupenvp: Handle malloc failure safely

Message ID 20260320-some-fixes-v1-2-f5ca33dbf180@pbarker.dev
State New
Headers show
Series Memory management fixes and b4 config | expand

Commit Message

Paul Barker March 20, 2026, 12:01 p.m. UTC
If malloc() fails, don't try to write to a NULL pointer. Instead skip
the snprintf() call in the same way we do in other malloc error handling
in this function.

Issue found by Claude Opus 4.6, but fix implemented by me.

Signed-off-by: Paul Barker <paul@pbarker.dev>
---
 pseudo_util.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/pseudo_util.c b/pseudo_util.c
index 4abcc155c286..3a060275f115 100644
--- a/pseudo_util.c
+++ b/pseudo_util.c
@@ -1201,9 +1201,10 @@  pseudo_setupenvp(char * const *envp) {
 			char *newenv = malloc(len);
 			if (!newenv) {
 				pseudo_diag("fatal: can't allocate new %s variable.\n", PRELINK_LIBRARIES);
+			} else {
+				snprintf(newenv, len, PRELINK_LIBRARIES "=%s", ld_preload);
+				new_envp[j++] = newenv;
 			}
-			snprintf(newenv, len, PRELINK_LIBRARIES "=%s", ld_preload);
-			new_envp[j++] = newenv;
 			free(ld_preload);
 		}
 	}
@@ -1229,9 +1230,10 @@  pseudo_setupenvp(char * const *envp) {
 			char *newenv = malloc(len);
 			if (!newenv) {
 				pseudo_diag("fatal: can't allocate new variable.\n");
+			} else {
+				snprintf(newenv, len, "%s=%s", pseudo_env[i].key, pseudo_env[i].value);
+				new_envp[j++] = newenv;
 			}
-			snprintf(newenv, len, "%s=%s", pseudo_env[i].key, pseudo_env[i].value);
-			new_envp[j++] = newenv;
 		}
 	}
 	new_envp[j++] = NULL;