From patchwork Fri Nov 10 16:09:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Yano X-Patchwork-Id: 79570 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BB5133858421 for ; Fri, 10 Nov 2023 16:10:16 +0000 (GMT) X-Original-To: newlib@sourceware.org Delivered-To: newlib@sourceware.org Received: from dmta1020.nifty.com (mta-snd01012.nifty.com [106.153.227.44]) by sourceware.org (Postfix) with ESMTPS id 583623858CD1 for ; Fri, 10 Nov 2023 16:10:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 583623858CD1 Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=nifty.ne.jp Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=nifty.ne.jp ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 583623858CD1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=106.153.227.44 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699632607; cv=none; b=uPjEE77+BXcDc0FcxHK/IIY/7vzQkXzpX3b7bF6vqH9Rdwbr+m5u+JynGqYOyHoMpwRwqww714r6TiMtrgpi89Cx7B7QmVlSbpNaxF99x/qrpjsncAb/MUK44997ti5UOC9upE8FEy5ucDAMBz4XYIwFPrd2LSkXbtBUPIUxjk8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699632607; c=relaxed/simple; bh=6Ep4+4OGGMN4TYeyKF64XQoQ5cjZXzLLLLK7i3UW6qg=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=vgu7Szne7czyVG6VDWi9EpsG+wBeeSEoOye7jCIhwRK0EKwOiTpm9MaovKMjChlEeMAgiiLMO2slE3tyuq5vjQuT+dDIYwU1it85cj4jtKfZ+wqZJuKOQiWEKsr6CUqv1Dkex1pSF/KQRrOFiONu07/n4EW2qUud3sS3iz/zxlA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from localhost.localdomain by dmta1020.nifty.com with ESMTP id <20231110161003269.IYND.131070.localhost.localdomain@nifty.com>; Sat, 11 Nov 2023 01:10:03 +0900 From: Takashi Yano To: newlib@sourceware.org Cc: Takashi Yano , Christophe Lyon , Joel Sherrill , Corinna Vinschen Subject: [PATCH v2] newlib: libc: Fix bugs in the commit 3d94e07c49b5. Date: Sat, 11 Nov 2023 01:09:47 +0900 Message-Id: <20231110160947.1518-1-takashi.yano@nifty.ne.jp> X-Mailer: git-send-email 2.39.0 MIME-Version: 1.0 X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: newlib-bounces+patchwork=sourceware.org@sourceware.org The commit 3d94e07c49b5 has a few bugs which cause testsuite failure in libstdc++. This is due to excess orientation check in __srefill_r() and _ungetc_r(). Further, sscanf() family also calls ssvfscanf() family with fp->_file == -1. This causes undesired orientation set/check for sscanf() family. This patch fixes these problems. Fixes: 3d94e07c49b5 ("newlib: libc: Fix crash on fprintf to a wide-oriented stream.") Reported-by: Christophe Lyon Reported-by: Joel Sherrill Reviewed-by: Corinna Vinschen Signed-off-by: Takashi Yano Reported-by: Christophe Lyon Reported-by: Joel Sherrill Signed-off-by: Takashi Yano Signed-off-by: Corinna Vinschen --- newlib/libc/stdio/local.h | 35 ++++++++++++++++++++--------------- newlib/libc/stdio/refill.c | 3 --- newlib/libc/stdio/ungetc.c | 8 ++------ newlib/libc/stdio/ungetwc.c | 2 ++ 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/newlib/libc/stdio/local.h b/newlib/libc/stdio/local.h index 3b86cf19a..dfb9fbbd0 100644 --- a/newlib/libc/stdio/local.h +++ b/newlib/libc/stdio/local.h @@ -231,21 +231,26 @@ extern _READ_WRITE_RETURN_TYPE __swrite64 (struct _reent *, void *, * Set the orientation for a stream. If o > 0, the stream has wide- * orientation. If o < 0, the stream has byte-orientation. */ -#define ORIENT(fp,ori) \ - ( \ - ( \ - ((fp)->_flags & __SORD) ? \ - 0 \ - : \ - ( \ - ((fp)->_flags |= __SORD), \ - (ori > 0) ? \ - ((fp)->_flags2 |= __SWID) \ - : \ - ((fp)->_flags2 &= ~__SWID) \ - ) \ - ), \ - ((fp)->_flags2 & __SWID) ? 1 : -1 \ +#define ORIENT(fp,ori) \ + ( \ + ((fp)->_file < 0) ? \ + ((ori > 0) ? 1 : -1) \ + : \ + ( \ + ( \ + ((fp)->_flags & __SORD) ? \ + 0 \ + : \ + ( \ + ((fp)->_flags |= __SORD), \ + (ori > 0) ? \ + ((fp)->_flags2 |= __SWID) \ + : \ + ((fp)->_flags2 &= ~__SWID) \ + ) \ + ), \ + ((fp)->_flags2 & __SWID) ? 1 : -1 \ + ) \ ) #else #define ORIENT(fp,ori) (-1) diff --git a/newlib/libc/stdio/refill.c b/newlib/libc/stdio/refill.c index c1ef7e120..cd71ed152 100644 --- a/newlib/libc/stdio/refill.c +++ b/newlib/libc/stdio/refill.c @@ -43,9 +43,6 @@ __srefill_r (struct _reent * ptr, CHECK_INIT (ptr, fp); - if (ORIENT (fp, -1) != -1) - return EOF; - fp->_r = 0; /* largely a convenience for callers */ /* SysV does not make this test; take it out for compatibility */ diff --git a/newlib/libc/stdio/ungetc.c b/newlib/libc/stdio/ungetc.c index 79914af08..5053fd6c4 100644 --- a/newlib/libc/stdio/ungetc.c +++ b/newlib/libc/stdio/ungetc.c @@ -125,12 +125,6 @@ _ungetc_r (struct _reent *rptr, _newlib_flockfile_start (fp); - if (ORIENT (fp, -1) != -1) - { - _newlib_flockfile_exit (fp); - return EOF; - } - /* After ungetc, we won't be at eof anymore */ fp->_flags &= ~__SEOF; @@ -213,6 +207,8 @@ int ungetc (int c, register FILE *fp) { + if (ORIENT (fp, -1) != -1) + return EOF; return _ungetc_r (_REENT, c, fp); } #endif /* !_REENT_ONLY */ diff --git a/newlib/libc/stdio/ungetwc.c b/newlib/libc/stdio/ungetwc.c index 18636d773..002b6292a 100644 --- a/newlib/libc/stdio/ungetwc.c +++ b/newlib/libc/stdio/ungetwc.c @@ -112,5 +112,7 @@ ungetwc (wint_t wc, struct _reent *reent = _REENT; CHECK_INIT (reent, fp); + if (ORIENT (fp, 1) != 1) + return WEOF; return _ungetwc_r (reent, wc, fp); }