From 4673c2eaba443678c4dc6ae74ea16a489b415fed Mon Sep 17 00:00:00 2001
From: liyunfei <liyunfei33@huawei.com>
Date: Tue, 19 Sep 2023 09:31:43 +0800
Subject: [PATCH] Prevent environment variables from exceeding NAME_MAX

---
 llvm/lib/Support/Unix/Path.inc | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc
index 2ae7c6dc..f13f3165 100644
--- a/llvm/lib/Support/Unix/Path.inc
+++ b/llvm/lib/Support/Unix/Path.inc
@@ -1427,8 +1427,12 @@ static const char *getEnvTempDir() {
   // variable.
   const char *EnvironmentVariables[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"};
   for (const char *Env : EnvironmentVariables) {
-    if (const char *Dir = std::getenv(Env))
+    if (const char *Dir = std::getenv(Env)) {
+      if(std::strlen(Dir) > NAME_MAX) {
+        continue;
+      }
       return Dir;
+    }
   }
 
   return nullptr;
-- 