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.129.124]) by sourceware.org (Postfix) with ESMTPS id 23BDA3858D35 for ; Wed, 26 Jul 2023 13:27:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 23BDA3858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1690378043; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references; bh=K3U/AXtytPIv1UEd0oaXIpglGo2cOOqkh6oErvJQK+g=; b=ZcQcjutx3LsnAcPc00ZkXnb17dEvR1ZWYeVHAvRzWUnbFgQea8SbCyAl/fjZmszXcrm8+4 llPy0/7ELXWZBMgox2j+ANBiq3jouG4RpkIGCgVqIDyzSvaa57c7AarqFXGFsJpbOeTBZH C+nww0XOfgV1oXKE92xZ3xa0HKX7E10= Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-451-W5B4C1ItO8i0H1PzYGS9sw-1; Wed, 26 Jul 2023 09:27:14 -0400 X-MC-Unique: W5B4C1ItO8i0H1PzYGS9sw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 11B35280BC61; Wed, 26 Jul 2023 13:27:00 +0000 (UTC) Received: from calimero.vinschen.de (unknown [10.39.193.241]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C89F5C2C856; Wed, 26 Jul 2023 13:26:59 +0000 (UTC) Received: by calimero.vinschen.de (Postfix, from userid 500) id 6BEAEA80889; Wed, 26 Jul 2023 15:26:58 +0200 (CEST) Date: Wed, 26 Jul 2023 15:26:58 +0200 From: Corinna Vinschen To: newlib@sourceware.org Cc: Jon Turney Subject: Re: bug with printf positional arguments? Message-ID: Reply-To: newlib@sourceware.org Mail-Followup-To: newlib@sourceware.org, Jon Turney References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 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=-5.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,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 Jul 3 15:49, Jon Turney wrote: > On 03/07/2023 12:14, Corinna Vinschen wrote: > > On Jun 28 14:28, Jon Turney wrote: > > > > > > The following trivial program (extracted from a glib testcase) > > > > > > > #include > > > > > > > > int main() > > > > { > > > > printf ("%1$*2$.*3$s", "abc", 5, 2); > > > > } > > > > > > does not produce the expected output of " ab", on 64-bit Cygwin. > > > > > > From a bit of staring at and stepping through get_args() in > > > libc/stdio/vfprintf.c, it looks like the problem is (something like) we > > > don't have the knowledge that the first positional variadic argument should > > > be treated as a pointer, at the point that we store it's value, so it gets > > > treated as an integer (the default), which is going to lead to truncation on > > > LP64 platforms. > > > > > > A straightforward way to fix this eludes me. > > > > Can you point out where in the code this problem occurs, so maybe > > we can discuss the necessary code changes in this thread? > > Sure, I'll try: > > Let's suppose you're executing the STC above. This is what I think > happens... > > We end up in _vprintf_r. > > We hit line 1072, which digests the '1', and on the following '$', knows to > interpret this as a positional argument and stores it. > > The subsequent '*' is processed at line 967 as introducing the width, where > we chomp the '2', which again the following '$' indicates should be treated > as a positional arg. > > We hit GET_ARG for the first time, to retrieve the width, which invokes > get_arg() to do the work (as we haven't yet stored the requested arg in > args[]). > > get_arg() digests the format string (again) using a state machine (to > populate args[] with the positional parameters) > > The state machine moves through START (action NUMBER) -> WDIG (action > GETPOS) -> DONE. > > Stopping here looks like maybe an error in the state machine design? > > The loop starting at line 2310 iterates over the seen args, fetching their > values. Because we haven't processed the 's' at the end in the GETARG state, > we retrieve the first argument using the default case, as an integer (32 > bits, truncating the 64-bit pointer value) (it looks like we're maybe also > lucking out on handling the width properly, as we're never hitting the PWPOS > state to record that it should be treated as an INT). After going a bit cross-eyed looking at the code, I *seriously* wonder, if we shouldn't start with fresh vfprintf_r/vfwprintf_r, using copies of the most recent FreeBSD vfprintf/vfwprintf, and just carefully tweak them to match our build system... Corinna