[libc-commits] [libc] [libc] Implement freopen (PR #207837)

Jeff Bailey via libc-commits libc-commits at lists.llvm.org
Fri Jul 24 07:28:05 PDT 2026


================
@@ -0,0 +1,44 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of freopen.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/stdio/freopen.h"
+#include "src/__support/File/file.h"
+
+#include "hdr/types/FILE.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(::FILE *, freopen,
+                   (const char *__restrict filename,
+                    const char *__restrict mode, ::FILE *__restrict stream)) {
+  LIBC_CRASH_ON_NULLPTR(stream);
+
----------------
kaladron wrote:

freopen says that mode is handled like fopen - and it looks like open fopen might need hardening here too.

fopen's page https://pubs.opengroup.org/onlinepubs/9799919799/functions/fopen.html doesn't have any provisions for mode being null.  It looks like it gets handed all the way in to File::mode_flags, which will dereference the null pointer.  I think we probably want LIBC_CRASH_ON_NULLPTR(mode) here and it should be added to fopen too.

https://github.com/llvm/llvm-project/pull/207837


More information about the libc-commits mailing list