public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] tools/nolibc/stdlib: fix getenv() with empty environment
@ 2024-10-16 11:14 Thomas Weißschuh
  2024-10-16 12:17 ` Willy Tarreau
  0 siblings, 1 reply; 4+ messages in thread
From: Thomas Weißschuh @ 2024-10-16 11:14 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, Paul E. McKenney
  Cc: linux-kernel, stable, Thomas Weißschuh

The environ pointer itself is never NULL, this is guaranteed by crt.h.
However if the environment is empty, environ will point to a NULL
pointer.
While this case will be checked by the loop later, this only happens
after the first loop iteration.
To avoid reading invalid memory inside the loop, fix the test that
checks for an empty environment.

Fixes: 077d0a392446 ("tools/nolibc/stdlib: add a simple getenv() implementation")
Cc: stable@vger.kernel.org
Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
---
 tools/include/nolibc/stdlib.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h
index 75aa273c23a6153db6a32facaea16457a522703b..c31967378cf1f699283d801487c1a91d17e4d1ce 100644
--- a/tools/include/nolibc/stdlib.h
+++ b/tools/include/nolibc/stdlib.h
@@ -90,7 +90,7 @@ char *getenv(const char *name)
 {
 	int idx, i;
 
-	if (environ) {
+	if (*environ) {
 		for (idx = 0; environ[idx]; idx++) {
 			for (i = 0; name[i] && name[i] == environ[idx][i];)
 				i++;

---
base-commit: 2f87d0916ce0d2925cedbc9e8f5d6291ba2ac7b2
change-id: 20241016-nolibc-getenv-f99f4d3df2cd

Best regards,
-- 
Thomas Weißschuh <thomas.weissschuh@linutronix.de>


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-10-16 14:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-16 11:14 [PATCH] tools/nolibc/stdlib: fix getenv() with empty environment Thomas Weißschuh
2024-10-16 12:17 ` Willy Tarreau
2024-10-16 13:01   ` Thomas Weißschuh
2024-10-16 14:49     ` Willy Tarreau

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox