From 7ee04bc6264b847f15300c121faab5d137d4e80b Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Mon, 15 Apr 2019 16:49:29 +0000 Subject: [PATCH] CID 337783: Avoid passing -1 to read --- src/magic.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/magic.c b/src/magic.c index 2207f083..0796a753 100644 --- a/src/magic.c +++ b/src/magic.c @@ -33,7 +33,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: magic.c,v 1.109 2019/02/20 02:35:27 christos Exp $") +FILE_RCSID("@(#)$File: magic.c,v 1.110 2019/04/15 16:49:29 christos Exp $") #endif /* lint */ #include "magic.h" @@ -472,12 +472,14 @@ file_or_fd(struct magic_set *ms, const char *inname, int fd) * try looking at the first ms->bytes_max bytes */ if (ispipe) { - ssize_t r = 0; + if (fd != -1) { + ssize_t r = 0; - while ((r = sread(fd, RCAST(void *, &buf[nbytes]), - CAST(size_t, ms->bytes_max - nbytes), 1)) > 0) { - nbytes += r; - if (r < PIPE_BUF) break; + while ((r = sread(fd, RCAST(void *, &buf[nbytes]), + CAST(size_t, ms->bytes_max - nbytes), 1)) > 0) { + nbytes += r; + if (r < PIPE_BUF) break; + } } if (nbytes == 0 && inname) { @@ -488,13 +490,13 @@ file_or_fd(struct magic_set *ms, const char *inname, int fd) goto done; } - } else { + } else if (fd != -1) { /* Windows refuses to read from a big console buffer. */ size_t howmany = #if defined(WIN32) - _isatty(fd) ? 8 * 1024 : + _isatty(fd) ? 8 * 1024 : #endif - ms->bytes_max; + ms->bytes_max; if ((nbytes = read(fd, RCAST(void *, buf), howmany)) == -1) { if (inname == NULL && fd != STDIN_FILENO) file_error(ms, errno, "cannot read fd %d", fd); -- 2.40.0