[clang] [clang][bytecode] Create fewer pointers in __builtin_nan() (PR #187990)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 23 00:57:40 PDT 2026


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/187990

Check the elements directly for initialization state and keep track of whether we found a NUL byte.

>From b05cdeb3beee8e224d19017539a076823ffd4aa0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Mon, 23 Mar 2026 08:52:34 +0100
Subject: [PATCH] [clang][bytecode] Create fewer pointers in __builtin_nan()

Check the elements directly for initialization state and keep track of
whether we found a NUL byte.
---
 clang/lib/AST/ByteCode/InterpBuiltin.cpp | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index fcd11ee9089c0..257d4c8c41c31 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -419,19 +419,23 @@ static bool interp__builtin_nan(InterpState &S, CodePtr OpPC,
   // Convert the given string to an integer using StringRef's API.
   llvm::APInt Fill;
   std::string Str;
-  assert(Arg.getNumElems() >= 1);
-  for (unsigned I = 0;; ++I) {
-    const Pointer &Elem = Arg.atIndex(I);
-
-    if (!CheckLoad(S, OpPC, Elem))
+  unsigned ArgLength = Arg.getNumElems();
+  bool FoundZero = false;
+  for (unsigned I = 0; I != ArgLength; ++I) {
+    if (!Arg.isElementInitialized(I))
       return false;
 
-    if (Elem.deref<int8_t>() == 0)
+    if (Arg.elem<int8_t>(I) == 0) {
+      FoundZero = true;
       break;
-
-    Str += Elem.deref<char>();
+    }
+    Str += Arg.elem<char>(I);
   }
 
+  // If we didn't find a NUL byte, diagnose as a one-past-the-end read.
+  if (!FoundZero)
+    return CheckRange(S, OpPC, Arg.atIndex(ArgLength), AK_Read);
+
   // Treat empty strings as if they were zero.
   if (Str.empty())
     Fill = llvm::APInt(32, 0);



More information about the cfe-commits mailing list