summaryrefslogtreecommitdiff
path: root/components/libc/compilers
diff options
context:
space:
mode:
authorMeco Man <[email protected]>2021-09-27 06:51:40 -0400
committerMeco Man <[email protected]>2021-09-27 06:51:40 -0400
commit86bb54fde6f3ed59050d724a7e98defd7c4f47a9 (patch)
tree60a05ee95a35859cc1fd55c5cff2b083d686445c /components/libc/compilers
parentc8c632512aa6c87211871fe0a85e77b72d4ebe06 (diff)
[libc][syscalls] 在标准输入输出前加校验,反正在libc初始化之前调用printf出问题
Diffstat (limited to 'components/libc/compilers')
-rw-r--r--components/libc/compilers/armlibc/syscalls.c14
-rw-r--r--components/libc/compilers/dlib/syscall_read.c9
-rw-r--r--components/libc/compilers/dlib/syscall_write.c9
-rw-r--r--components/libc/compilers/newlib/syscalls.c16
4 files changed, 46 insertions, 2 deletions
diff --git a/components/libc/compilers/armlibc/syscalls.c b/components/libc/compilers/armlibc/syscalls.c
index d942bdb984..a3ba57a280 100644
--- a/components/libc/compilers/armlibc/syscalls.c
+++ b/components/libc/compilers/armlibc/syscalls.c
@@ -25,6 +25,10 @@
#include <dfs_posix.h>
#endif
+#define DBG_TAG "armlibc.syscalls"
+#define DBG_LVL DBG_INFO
+#include <rtdbg.h>
+
#ifdef __CLANG_ARM
__asm(".global __use_no_semihosting\n\t");
#else
@@ -146,6 +150,11 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
if (fh == STDIN)
{
#ifdef RT_USING_POSIX
+ if (libc_stdio_get_console() < 0)
+ {
+ LOG_E("invoke standard output before initializing libc");
+ return -1;
+ }
size = read(STDIN_FILENO, buf, len);
return len - size;
#else
@@ -186,6 +195,11 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
return 0;
#else
#ifdef RT_USING_POSIX
+ if (libc_stdio_get_console() < 0)
+ {
+ LOG_E("invoke standard input before initializing libc");
+ return -1;
+ }
size = write(STDOUT_FILENO, buf, len);
return len - size;
#else
diff --git a/components/libc/compilers/dlib/syscall_read.c b/components/libc/compilers/dlib/syscall_read.c
index e716d0b024..bdc694b7d8 100644
--- a/components/libc/compilers/dlib/syscall_read.c
+++ b/components/libc/compilers/dlib/syscall_read.c
@@ -15,6 +15,10 @@
#include <yfuns.h>
#include "libc.h"
+#define DBG_TAG "dlib.syscall_read"
+#define DBG_LVL DBG_INFO
+#include <rtdbg.h>
+
#pragma module_name = "?__read"
size_t __read(int handle, unsigned char *buf, size_t len)
{
@@ -25,6 +29,11 @@ size_t __read(int handle, unsigned char *buf, size_t len)
if (handle == _LLIO_STDIN)
{
#ifdef RT_USING_POSIX
+ if (libc_stdio_get_console() < 0)
+ {
+ LOG_E("invoke standard input before initializing libc");
+ return _LLIO_ERROR;
+ }
return read(STDIN_FILENO, buf, len);
#else
return _LLIO_ERROR;
diff --git a/components/libc/compilers/dlib/syscall_write.c b/components/libc/compilers/dlib/syscall_write.c
index a5462653bf..bd7cc83d99 100644
--- a/components/libc/compilers/dlib/syscall_write.c
+++ b/components/libc/compilers/dlib/syscall_write.c
@@ -15,6 +15,10 @@
#include <yfuns.h>
#include "libc.h"
+#define DBG_TAG "dlib.syscall_write"
+#define DBG_LVL DBG_INFO
+#include <rtdbg.h>
+
#pragma module_name = "?__write"
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)
#else
#ifdef RT_USING_POSIX
+ if (libc_stdio_get_console() < 0)
+ {
+ LOG_E("invoke standard output before initializing libc");
+ return _LLIO_ERROR;
+ }
return write(STDOUT_FILENO, (void*)buf, len);
#else
rt_device_t console_device;
diff --git a/components/libc/compilers/newlib/syscalls.c b/components/libc/compilers/newlib/syscalls.c
index aeb4d123e7..d46a264408 100644
--- a/components/libc/compilers/newlib/syscalls.c
+++ b/components/libc/compilers/newlib/syscalls.c
@@ -26,6 +26,10 @@
#include <dlmodule.h>
#endif
+#define DBG_TAG "newlib.syscalls"
+#define DBG_LVL DBG_INFO
+#include <rtdbg.h>
+
/* Reentrant versions of system calls. */
#ifndef _REENT_ONLY
@@ -155,7 +159,11 @@ _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
return -1;
#else
_ssize_t rc;
-
+ if (libc_stdio_get_console() < 0 && fd == STDIN_FILENO)
+ {
+ LOG_E("invoke standard input before initializing libc");
+ return -1;
+ }
rc = read(fd, buf, nbytes);
return rc;
#endif
@@ -227,7 +235,11 @@ _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
#endif /*RT_USING_DEVICE*/
#else
_ssize_t rc;
-
+ if (libc_stdio_get_console() < 0 && fd == STDOUT_FILENO)
+ {
+ LOG_E("invoke standard output before initializing libc");
+ return -1;
+ }
rc = write(fd, buf, nbytes);
return rc;
#endif