[llvm] [flang-rt][device] Use snprintf result for length (PR #172239)

Valentin Clement バレンタイン クレメン via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 14 16:53:17 PST 2025


https://github.com/clementval created https://github.com/llvm/llvm-project/pull/172239

The buffer might not be null terminated on the device and result in 1 byte invalid read when trying to get the length. 

>From 574ef9d6ea404550ef29ff0030bd228312bb6df0 Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Sun, 14 Dec 2025 16:51:48 -0800
Subject: [PATCH] [flang-rt][device] Use snprintf result for length

---
 flang-rt/lib/runtime/external-unit.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/flang-rt/lib/runtime/external-unit.cpp b/flang-rt/lib/runtime/external-unit.cpp
index 63a93c12eec57..6a609a22d093a 100644
--- a/flang-rt/lib/runtime/external-unit.cpp
+++ b/flang-rt/lib/runtime/external-unit.cpp
@@ -201,9 +201,9 @@ bool ExternalFileUnit::OpenAnonymousUnit(common::optional<OpenStatus> status,
   // I/O to an unconnected unit reads/creates a local file, e.g. fort.7
   std::size_t pathMaxLen{32};
   auto path{SizedNew<char>{handler}(pathMaxLen)};
-  std::snprintf(path.get(), pathMaxLen, "fort.%d", unitNumber_);
+  int len = std::snprintf(path.get(), pathMaxLen, "fort.%d", unitNumber_);
   OpenUnit(status, action, position, std::move(path),
-      runtime::strlen(path.get()), convert, handler);
+      len >= 0 ? static_cast<std::size_t>(len) : 0, convert, handler);
   return IsConnected();
 }
 



More information about the llvm-commits mailing list