From 8f3f1c30058e13987833525a6c7d0f33ce070480 Mon Sep 17 00:00:00 2001
From: Dev <9416650+aoinoikaz@users.noreply.github.com>
Date: Tue, 3 Nov 2026 18:11:01 -0500
Subject: [PATCH] lib: windows: _read: Call NtReadFile, rather than read.
lib/windows/_read.c answered with read, which kaem.windows.run builds
from lib/windows/read.c, the bare NtReadFile, beside the stub
__buffered_read. configure-lib.sh's lists build read from
lib/posix/buffered-read.c instead, whose __buffered_read fills its buffer
with _read, so read, __buffered_read and _read called one another until
the stack ran out. Mes compiled by MesCC died with STATUS_STACK_OVERFLOW
once it had opened boot-5.scm, having printed nothing. _read is now the
bare NtReadFile, as lib/linux/_read.c is the bare SYS_read, and read.c,
which only the build without the buffer uses, calls it.
* lib/windows/_read.c (_read): Call NtReadFile, moved here from read.c.
* lib/windows/read.c (read): Call _read.
* kaem.windows.run: Build lib/windows/_read.c ahead of read.c.
---
kaem.windows.run | 1 +
lib/windows/_read.c | 32 +++++++++++++++++++++++++-------
lib/windows/read.c | 26 +++++---------------------
3 files changed, 31 insertions(+), 28 deletions(-)
diff --git a/kaem.windows.run b/kaem.windows.run
index e18c8176..3caa60e5 100755
--- a/kaem.windows.run
+++ b/kaem.windows.run
@@ -84,6 +84,7 @@ M2-Planet \
-f ${srcdest}lib/linux/malloc.c \
-f ${srcdest}lib/windows/ntdll.c \
-f ${srcdest}lib/string/memset.c \
+ -f ${srcdest}lib/windows/_read.c \
-f ${srcdest}lib/windows/read.c \
-f ${srcdest}lib/mes/fdgetc.c \
-f ${srcdest}lib/stdio/getchar.c \
diff --git a/lib/windows/_read.c b/lib/windows/_read.c
index fd411091..97596fc6 100644
--- a/lib/windows/_read.c
+++ b/lib/windows/_read.c
@@ -16,16 +16,34 @@
* You should have received a copy of the GNU General Public License
* along with GNU Mes. If not, see .
*/
-
-/* Stands in for lib/linux/_read.c. On Linux _read is the bare SYS_read and
- * read is the same call; here read (lib/windows/read.c) is already the bare
- * NtReadFile with nothing between, so this is that. */
-
+#include
+#include
#include
-#include
+#include
+
+/* Stands in for lib/linux/_read.c: the bare read, with nothing above it.
+ * configure-lib.sh's lists build read from lib/posix/buffered-read.c, whose
+ * __buffered_read fills its buffer with this, so this must not call read.
+ * NtReadFile is NtWriteFile's twin, and an NTSTATUS below zero is how the end
+ * of the file arrives as much as how a failure does -- both mean nothing more
+ * was read, which is what a caller of read wants to hear as 0. */
ssize_t
_read (int filedes, void *buffer, size_t size)
{
- return read (filedes, buffer, size);
+ int NtReadFile;
+ int *iosb;
+ int handle;
+ int rc;
+
+ NtReadFile = __ntdll_resolve ("NtReadFile");
+ iosb = __iosb ();
+ iosb[0] = 0;
+ iosb[1] = 0;
+ handle = __handle (filedes);
+
+ rc = __ntcall9 (NtReadFile, handle, 0, 0, 0, iosb, buffer, size, 0, 0);
+ if (rc < 0)
+ return 0;
+ return iosb[1];
}
diff --git a/lib/windows/read.c b/lib/windows/read.c
index 06d0c1e9..375256c5 100644
--- a/lib/windows/read.c
+++ b/lib/windows/read.c
@@ -16,32 +16,16 @@
* You should have received a copy of the GNU General Public License
* along with GNU Mes. If not, see .
*/
-#include
-#include
#include
#include
-/* Stands in for lib/linux/read.c. NtReadFile is NtWriteFile's twin, and an
- * NTSTATUS below zero is how the end of the file arrives as much as how a
- * failure does -- both mean nothing more was read, which is what a caller of
- * read wants to hear as 0. */
+/* Stands in for lib/linux/read.c, for a build that buffers nothing, as
+ * kaem.windows.run's does: there read is lib/windows/_read.c's bare
+ * NtReadFile. configure-lib.sh's lists take read from
+ * lib/posix/buffered-read.c instead, and do not build this file. */
ssize_t
read (int filedes, void *buffer, size_t size)
{
- int NtReadFile;
- int *iosb;
- int handle;
- int rc;
-
- NtReadFile = __ntdll_resolve ("NtReadFile");
- iosb = __iosb ();
- iosb[0] = 0;
- iosb[1] = 0;
- handle = __handle (filedes);
-
- rc = __ntcall9 (NtReadFile, handle, 0, 0, 0, iosb, buffer, size, 0, 0);
- if (rc < 0)
- return 0;
- return iosb[1];
+ return _read (filedes, buffer, size);
}