From: Liu Jie <liujie1@huawei.com>
To: <musl@lists.openwall.com>
Cc: <yunlong.song@huawei.com>, <liujie1@huawei.com>
Subject: [PATCH] coresignt: stdio: modify the fdopen interface
Date: Wed, 25 Dec 2019 18:31:50 +0800 [thread overview]
Message-ID: <20191225103150.23251-1-liujie1@huawei.com> (raw)
For fdopen function, its RETURN VALUE is defined in posix as "Upon
successful completion, fdopen() shall return a pointer to a stream;
otherwise, a null pointer shall be returned and errno set to
indicate the error.". This patch made some changes in __fdopen.c
for error types: EBADF, EINVAL, EMFILE and ENOMEM.
Signed-off-by: Liu Jie <liujie1@huawei.com>
---
src/stdio/__fdopen.c | 40 ++++++++++++++++++++++++++++++++++++----
1 file changed, 36 insertions(+), 4 deletions(-)
diff --git a/src/stdio/__fdopen.c b/src/stdio/__fdopen.c
index 116e78e5..c9469b07 100644
--- a/src/stdio/__fdopen.c
+++ b/src/stdio/__fdopen.c
@@ -5,11 +5,13 @@
#include <errno.h>
#include <string.h>
#include "libc.h"
+#include <stdio.h>
FILE *__fdopen(int fd, const char *mode)
{
FILE *f;
struct winsize wsz;
+ int err;
/* Check for valid initial mode character */
if (!strchr("rwa", *mode)) {
@@ -27,13 +29,35 @@ FILE *__fdopen(int fd, const char *mode)
if (!strchr(mode, '+')) f->flags = (*mode == 'r') ? F_NOWR : F_NORD;
/* Apply close-on-exec flag */
- if (strchr(mode, 'e')) __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
+ if (strchr(mode, 'e')) {
+ err = __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
+ if (err == -EBADF || err == -EINVAL ||
+ err == -EMFILE || err == -ENOMEM) {
+ free(f);
+ errno = -err;
+ return NULL;
+ }
+ }
/* Set append mode on fd if opened for append */
if (*mode == 'a') {
int flags = __syscall(SYS_fcntl, fd, F_GETFL);
- if (!(flags & O_APPEND))
- __syscall(SYS_fcntl, fd, F_SETFL, flags | O_APPEND);
+ err = flags;
+ if (err == -EBADF || err == -EINVAL ||
+ err == -EMFILE || err == -ENOMEM) {
+ free(f);
+ errno = -err;
+ return NULL;
+ }
+ if (!(flags & O_APPEND)) {
+ err = __syscall(SYS_fcntl, fd, F_SETFL, flags | O_APPEND);
+ if (err == -EBADF || err == -EINVAL ||
+ err == -EMFILE || err == -ENOMEM) {
+ free(f);
+ errno = -err;
+ return NULL;
+ }
+ }
f->flags |= F_APP;
}
@@ -43,7 +67,15 @@ FILE *__fdopen(int fd, const char *mode)
/* Activate line buffered mode for terminals */
f->lbf = EOF;
- if (!(f->flags & F_NOWR) && !__syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz))
+ err = __syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz);
+ if (err == -EBADF || err == -EINVAL ||
+ err == -EMFILE || err == -ENOMEM) {
+ free(f);
+ errno = -err;
+ return NULL;
+ }
+
+ if (!(f->flags & F_NOWR) && !err)
f->lbf = '\n';
/* Initialize op ptrs. No problem if some are unneeded. */
--
2.17.1
next reply other threads:[~2019-12-25 10:31 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-25 10:31 Liu Jie [this message]
2019-12-25 19:19 ` Rich Felker
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191225103150.23251-1-liujie1@huawei.com \
--to=liujie1@huawei.com \
--cc=musl@lists.openwall.com \
--cc=yunlong.song@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).