[Lldb-commits] [lldb] [LLDB] Make sure FILE* is valid before trying to flush it. (PR #169088)

via lldb-commits lldb-commits at lists.llvm.org
Fri Nov 21 11:35:41 PST 2025


https://github.com/cmtice created https://github.com/llvm/llvm-project/pull/169088

PR/167764 makes sure the access mode for newly created Native files is writable. This uncovered a bug in NativeFile::Close where it tries to flush a writable file without first checking to make sure the file hasn't already been closed. This triggers a bug in some of our code, where it closes a file by ovewriting the fields with nonsense values rather than deleting the pointer. This PR now checks to make sure this has not been done before trying to flush it.

>From 30e7fc31387dddb649dddb607d413faec4aaa31e Mon Sep 17 00:00:00 2001
From: Caroline Tice <cmtice at google.com>
Date: Fri, 21 Nov 2025 11:34:39 -0800
Subject: [PATCH] [LLDB] Make sure FILE* is valid before trying to flush it.

PR/167764 makes sure the access mode for newly created Native files
is writable. This uncovered a bug in NativeFile::Close where it tries
to flush a writable file without first checking to make sure the file
hasn't already been closed. This triggers a bug in some of our code, where
it closes a file by ovewriting the fields with nonsense values rather
than deleting the pointer. This PR now checks to make sure this has not
been done before trying to flush it.
---
 lldb/source/Host/common/File.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/lldb/source/Host/common/File.cpp b/lldb/source/Host/common/File.cpp
index 4fad93fca9ea3..64504421a4d0b 100644
--- a/lldb/source/Host/common/File.cpp
+++ b/lldb/source/Host/common/File.cpp
@@ -378,7 +378,10 @@ Status NativeFile::Close() {
           m_options & (File::eOpenOptionReadOnly | File::eOpenOptionWriteOnly |
                        File::eOpenOptionReadWrite);
 
-      if (rw == eOpenOptionWriteOnly || rw == eOpenOptionReadWrite) {
+      // If the stream is writable, and has not already been closed, flush
+      // it.
+      if ((rw == eOpenOptionWriteOnly || rw == eOpenOptionReadWrite) &&
+          (m_stream->_flags != m_stream->_fileno)) {
         if (::fflush(m_stream) == EOF)
           error = Status::FromErrno();
       }



More information about the lldb-commits mailing list