From 1ee7028fbb6ce33359d4a70a17a247a4659e6650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Rohl=C3=ADnek?= Date: Thu, 8 Jan 2026 15:09:04 +0100 Subject: [PATCH] fix(storage/esp_libc): Fix incorrect fd checks --- .../esp_libc/src/picolibc/picolibc_init.c | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/components/esp_libc/src/picolibc/picolibc_init.c b/components/esp_libc/src/picolibc/picolibc_init.c index 0756627ef42..2e6731b5bf3 100644 --- a/components/esp_libc/src/picolibc/picolibc_init.c +++ b/components/esp_libc/src/picolibc/picolibc_init.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -138,32 +138,38 @@ void esp_reent_cleanup(void) * Initialize stdin, stdout, and stderr using static memory allocation. * Creating them with fopen() would call malloc() internally. */ -static char write_buf[BUFSIZ]; -static char read_buf[BUFSIZ]; +static char in_buf[BUFSIZ]; +static char out_buf[BUFSIZ]; +static char err_buf[BUFSIZ]; -static struct __file_bufio __stdin = FDEV_SETUP_BUFIO(0, read_buf, BUFSIZ, read, write, lseek, close, __SRD, 0); -static struct __file_bufio __stdout = FDEV_SETUP_BUFIO(1, write_buf, BUFSIZ, read, write, lseek, close, __SWR, __BLBF); +static struct __file_bufio __stdin = FDEV_SETUP_BUFIO(0, in_buf, BUFSIZ, read, write, lseek, close, __SRD, 0); +static struct __file_bufio __stdout = FDEV_SETUP_BUFIO(1, out_buf, BUFSIZ, read, write, lseek, close, __SWR, __BLBF); +static struct __file_bufio __stderr = FDEV_SETUP_BUFIO(2, err_buf, BUFSIZ, read, write, lseek, close, __SWR, __BLBF); FILE *stdin = &__stdin.xfile.cfile.file; FILE *stdout = &__stdout.xfile.cfile.file; -FILE *stderr = &__stdout.xfile.cfile.file; +FILE *stderr = &__stderr.xfile.cfile.file; #if CONFIG_LIBC_PICOLIBC_NEWLIB_COMPATIBILITY __thread FILE* tls_stdin = &__stdin.xfile.cfile.file; __thread FILE* tls_stdout = &__stdout.xfile.cfile.file; -__thread FILE* tls_stderr = &__stdout.xfile.cfile.file; +__thread FILE* tls_stderr = &__stderr.xfile.cfile.file; #endif #if CONFIG_VFS_SUPPORT_IO void esp_libc_init_global_stdio(const char *stdio_dev) { int stdin_fd = open(stdio_dev, O_RDONLY); - assert(stdin_fd > 0); + assert(stdin_fd >= 0); // Should be 0 __stdin.ptr = (void *)(intptr_t)(stdin_fd); int stdout_fd = open(stdio_dev, O_WRONLY); - assert(stdout_fd > 0); + assert(stdout_fd >= 0); // Should be 1 __stdout.ptr = (void *)(intptr_t)(stdout_fd); + + int stderr_fd = open(stdio_dev, O_WRONLY); + assert(stderr_fd >= 0); // Should be 2 + __stderr.ptr = (void *)(intptr_t)(stderr_fd); } #else /* CONFIG_VFS_SUPPORT_IO */ void esp_libc_init_global_stdio(void)