Skip to content

Commit 86bb54f

Browse files
committed
[libc][syscalls] 在标准输入输出前加校验,反正在libc初始化之前调用printf出问题
1 parent c8c6325 commit 86bb54f

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

components/libc/compilers/armlibc/syscalls.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include <dfs_posix.h>
2626
#endif
2727

28+
#define DBG_TAG "armlibc.syscalls"
29+
#define DBG_LVL DBG_INFO
30+
#include <rtdbg.h>
31+
2832
#ifdef __CLANG_ARM
2933
__asm(".global __use_no_semihosting\n\t");
3034
#else
@@ -146,6 +150,11 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
146150
if (fh == STDIN)
147151
{
148152
#ifdef RT_USING_POSIX
153+
if (libc_stdio_get_console() < 0)
154+
{
155+
LOG_E("invoke standard output before initializing libc");
156+
return -1;
157+
}
149158
size = read(STDIN_FILENO, buf, len);
150159
return len - size;
151160
#else
@@ -186,6 +195,11 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
186195
return 0;
187196
#else
188197
#ifdef RT_USING_POSIX
198+
if (libc_stdio_get_console() < 0)
199+
{
200+
LOG_E("invoke standard input before initializing libc");
201+
return -1;
202+
}
189203
size = write(STDOUT_FILENO, buf, len);
190204
return len - size;
191205
#else

components/libc/compilers/dlib/syscall_read.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#include <yfuns.h>
1616
#include "libc.h"
1717

18+
#define DBG_TAG "dlib.syscall_read"
19+
#define DBG_LVL DBG_INFO
20+
#include <rtdbg.h>
21+
1822
#pragma module_name = "?__read"
1923
size_t __read(int handle, unsigned char *buf, size_t len)
2024
{
@@ -25,6 +29,11 @@ size_t __read(int handle, unsigned char *buf, size_t len)
2529
if (handle == _LLIO_STDIN)
2630
{
2731
#ifdef RT_USING_POSIX
32+
if (libc_stdio_get_console() < 0)
33+
{
34+
LOG_E("invoke standard input before initializing libc");
35+
return _LLIO_ERROR;
36+
}
2837
return read(STDIN_FILENO, buf, len);
2938
#else
3039
return _LLIO_ERROR;

components/libc/compilers/dlib/syscall_write.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#include <yfuns.h>
1616
#include "libc.h"
1717

18+
#define DBG_TAG "dlib.syscall_write"
19+
#define DBG_LVL DBG_INFO
20+
#include <rtdbg.h>
21+
1822
#pragma module_name = "?__write"
1923

2024
size_t __write(int handle, const unsigned char *buf, size_t len)
@@ -30,6 +34,11 @@ size_t __write(int handle, const unsigned char *buf, size_t len)
3034
#else
3135

3236
#ifdef RT_USING_POSIX
37+
if (libc_stdio_get_console() < 0)
38+
{
39+
LOG_E("invoke standard output before initializing libc");
40+
return _LLIO_ERROR;
41+
}
3342
return write(STDOUT_FILENO, (void*)buf, len);
3443
#else
3544
rt_device_t console_device;

components/libc/compilers/newlib/syscalls.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
#include <dlmodule.h>
2727
#endif
2828

29+
#define DBG_TAG "newlib.syscalls"
30+
#define DBG_LVL DBG_INFO
31+
#include <rtdbg.h>
32+
2933
/* Reentrant versions of system calls. */
3034

3135
#ifndef _REENT_ONLY
@@ -155,7 +159,11 @@ _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
155159
return -1;
156160
#else
157161
_ssize_t rc;
158-
162+
if (libc_stdio_get_console() < 0 && fd == STDIN_FILENO)
163+
{
164+
LOG_E("invoke standard input before initializing libc");
165+
return -1;
166+
}
159167
rc = read(fd, buf, nbytes);
160168
return rc;
161169
#endif
@@ -227,7 +235,11 @@ _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
227235
#endif /*RT_USING_DEVICE*/
228236
#else
229237
_ssize_t rc;
230-
238+
if (libc_stdio_get_console() < 0 && fd == STDOUT_FILENO)
239+
{
240+
LOG_E("invoke standard output before initializing libc");
241+
return -1;
242+
}
231243
rc = write(fd, buf, nbytes);
232244
return rc;
233245
#endif

0 commit comments

Comments
 (0)