From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 99F093858C52 for ; Mon, 13 Nov 2023 13:05:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 99F093858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 99F093858C52 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699880742; cv=none; b=klSYDRU0jZkK5PjURhxRBSRUsXk4bbxWpFb9pAXy4sb8W+cnqJPZsh+cVMblc5/hft+iLYf13rosGxMy8v6rWKRKWYHBz071Umku9820t7voqhQ5bbuHUg+fRrogvAk6Xsc5fGXxM5ED7XnfclcjXGMvehu/U9MdmxmzOwl3aw4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699880742; c=relaxed/simple; bh=48Rr+gfVGcAyT+GtrK7c1EZm/rbncQIVvetMGsKZde4=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=XyA5rkwFDEGQDD9++142p6V1NgKPQ7kUVCVXHW7HBOzVcRSOXwNaPYb1/DD6GcqTSSwSjp+NsYgz5bWhnieM5BEZfdvsmabRZEac0ULg3nTIoKV1/SWQOKLOUjHMVVulM56afNwDz3+4dO6sA+AWdIhjfnLOrkNgF5KbF46fAD0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699880741; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references; bh=tI9AFtV0xeIfxPHvL+IfbRPYCTeOwSnhIQq8fAWA0As=; b=a7Ucr8x3lXNPg9IprFvCjTG0CXHnOHLLvmh50GHS2WU9GIkHnNU8S093YOMyX2nVofTtOt vc12w59VpIM375GHAWrK6C0gqXrz9AgttHTP82ldRUlDH064J9wDu0xH+bOisvX/1We0XW rh1jF0Rljkp5a7D2ikNlmpgvRCinSuI= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-532-NAID_a3GNjantCaoVtfaew-1; Mon, 13 Nov 2023 08:05:39 -0500 X-MC-Unique: NAID_a3GNjantCaoVtfaew-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8D0103C40B41 for ; Mon, 13 Nov 2023 13:05:39 +0000 (UTC) Received: from calimero.vinschen.de (unknown [10.39.194.184]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6A49436EE for ; Mon, 13 Nov 2023 13:05:39 +0000 (UTC) Received: by calimero.vinschen.de (Postfix, from userid 500) id 40DB0A80A3D; Mon, 13 Nov 2023 14:05:38 +0100 (CET) Date: Mon, 13 Nov 2023 14:05:38 +0100 From: Corinna Vinschen To: newlib@sourceware.org Subject: Re: [PATCH] newlib: libc: Fix bugs in the commit 3d94e07c49b5. Message-ID: Reply-To: newlib@sourceware.org Mail-Followup-To: newlib@sourceware.org References: <20231110113436.2022-1-takashi.yano@nifty.ne.jp> <20231111013348.254fd41ffa12e375bfe70f52@nifty.ne.jp> MIME-Version: 1.0 In-Reply-To: <20231111013348.254fd41ffa12e375bfe70f52@nifty.ne.jp> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,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 List-Id: On Nov 11 01:33, Takashi Yano wrote: > On Fri, 10 Nov 2023 17:08:40 +0100 > Corinna Vinschen wrote: > > On Nov 10 16:17, Corinna Vinschen wrote: > > > Didn't you forget ungetwc? > > > > > > But then again, I checked GLibC, and there's something weird: > > > > > > ungetc does not at all set or test the orientation. > > > > > > ungetwc sets the orientation to 1, but doesn't check it. > > > > > > Puzzeling. I wonder about the reasoning behind this. > > > > Apparently, ungetwc has been added only later. ungetc, OTOH, was > > defined in a way which allowed to use it on wide-char oriented streams > > as well. > > > > So the stance in GLibC is, for backward compatibility reasons, ungetc > > can't and must not check or set the orientation at all. > > > > The fact that ungetwc doesn't test the orientation might be a bug in > > glibc. We can follow suit (being "bug-compatible" :)), or we can test > > the orientation. Given that using the correctly oriented functions is > > ultimately the responsibility of the application, both ways to handle > > this should be fine. > > I noticed that getchar/getwchar/gets etc. in newlib do not set > orientation. For example, both getwchar() and getchar() success > in the following code. > > #include > #include > > int main() > { > wchar_t w; > char c; > w = getwchar(); > ungetwc(w, stdin); > c = getchar(); > printf("%lc,%c\n", w, c); > return 0; > } Just for the sake of clarity, this requires your not yet applied patch "newlib: libc: Fix bugs in the commit 3d94e07c49b5". Otherwise getwchar() always returns WEOF. > Hmmmmm... In GLibC, getchar returns EOF in this case. Their macros never check for the orientation, only the underlying functions do that. In case of getchar that's the __uflow function, which is hidden under a pile of other macros and functions. Our underlying functions of getchar are _getc_r, __srget_r and __srefill_r. As already noticed, __srefill_r is called from wide-char code as well, but __srget_r isn't, so that sounds like a good place to set and check the orientation. I tried exactly that, and getchar() still returns the ungetwc'ed character. Our pile of macros and function is BSD based and works slightly differently than GLibC's. __srget_r is only called if no char is in the buffer, but ungetwc filled something into the buffer. So we either let this slip, or we have to add the ORIENT checks to getc and _getc_r: diff --git a/newlib/libc/stdio/getc.c b/newlib/libc/stdio/getc.c index c8781320e603..12bffaf8f79a 100644 --- a/newlib/libc/stdio/getc.c +++ b/newlib/libc/stdio/getc.c @@ -81,6 +81,9 @@ _getc_r (struct _reent *ptr, { int result; CHECK_INIT (ptr, fp); + if (ORIENT (fp, -1) != -1) + return EOF; + _newlib_flockfile_start (fp); result = __sgetc_r (ptr, fp); _newlib_flockfile_end (fp); @@ -96,6 +99,9 @@ getc (register FILE *fp) struct _reent *reent = _REENT; CHECK_INIT (reent, fp); + if (ORIENT (fp, -1) != -1) + return EOF; + _newlib_flockfile_start (fp); result = __sgetc_r (reent, fp); _newlib_flockfile_end (fp); Corinna