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 86B2B385843B for ; Fri, 9 Feb 2024 15:25:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 86B2B385843B 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 86B2B385843B 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=1707492357; cv=none; b=TpO7Y1XxEMzJxtasz9sQ+gWB/J4gjQoOeqJOi+6vcEuWqKpiqn1QoVN/U8DGLA7nTi8cHjTz0mlrEVL9EC+6S9Gkdwc3sG1hq3zeccFS3P6/EjjlXUq62+urmGslMVndX55DAfe/tWWqp76n2lQEZiQCbz+9BUM03NZkY7UzcUM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492357; c=relaxed/simple; bh=5ujDP55VMDAPYtfyFGavHOxt8YgflKUCg43euVTxtH4=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=lCdw8oojkDiV90KVp7ex4sACuB6aCMLgxIEv+Ac0UNTOSkHfFw2t4fgU2Md5AWdIftPso0i4EsUxo2T17wwCrog42gMYKvZcvGhahFKOChX7pyj6gxFR0EZYPEIrzRGDnFvi8CGl5E+cjN5e7unnxOAu7hg8NjDZaMxfaKvbLFc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707492355; h=from:from: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=gEeT5PZ1acAiIpLF6pnZkj91Z2E+TKJY5xATbCxyFBQ=; b=FF8eULSDTdoeqjRzzh53i2lPI/OK6ituLsKykaeZbdX1XJjpwyxEwgWfbXzSRRpZR9DHvp QjBax+GAC3Mori7bXqttAJzAAcO6qZNX/7oX4ihXOiyXu1/409pLchrAQfaYgORZh+96FP 6qIMLSySa1kCFiusthR4k65sFH3XU3c= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-248-LlvDX5FQM-OcqbaEST9cxA-1; Fri, 09 Feb 2024 10:25:54 -0500 X-MC-Unique: LlvDX5FQM-OcqbaEST9cxA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 A06E185A58B for ; Fri, 9 Feb 2024 15:25:53 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.58]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1D761C08EF7 for ; Fri, 9 Feb 2024 15:25:52 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 09/11] stdio-common: Allow skipping initial bytes in __printf_buffer for %n In-Reply-To: Message-ID: References: X-From-Line: fa1f6a475bd87ff4615ad71c5253257c21cc0816 Mon Sep 17 00:00:00 2001 Date: Fri, 09 Feb 2024 16:25:51 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-10.7 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_H5,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: Making the written field signed allows subtraction of a prefix count in case multiple writes happen to the same buffer. --- include/printf_buffer.h | 8 +++++--- stdio-common/tst-printf_buffer.c | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/printf_buffer.h b/include/printf_buffer.h index a5483e8a97..2400371b94 100644 --- a/include/printf_buffer.h +++ b/include/printf_buffer.h @@ -92,8 +92,10 @@ struct __printf_buffer buffer). Potentially updated on flush. The actual number of written bytes also includes the unflushed-but-written buffer part, write_ptr - write_base. A 64-bit value is used to avoid - the need for overflow checks. */ - uint64_t written; + the need for intermediate overflow checks. Negative values can + be used to ignore leading parts of the buffer for %n computations + (for repeated __printf_buffer calls to the same buffer). */ + int64_t written; /* Identifies the flush callback. */ enum __printf_buffer_mode mode; @@ -225,7 +227,7 @@ struct __wprintf_buffer wchar_t *write_base; wchar_t *write_ptr; wchar_t *write_end; - uint64_t written; + int64_t written; enum __wprintf_buffer_mode mode; }; diff --git a/stdio-common/tst-printf_buffer.c b/stdio-common/tst-printf_buffer.c index d12da8c939..2a3ee511d0 100644 --- a/stdio-common/tst-printf_buffer.c +++ b/stdio-common/tst-printf_buffer.c @@ -132,6 +132,24 @@ do_test (void) TEST_VERIFY (!__printf_buffer_has_failed (&buf.base)); TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1); } + + /* Test %n with initial skip. */ + { + struct __printf_buffer_asprintf buf; + __printf_buffer_asprintf_init (&buf); + __printf_buffer (&buf.base, "abc"); + buf.base.written = -2; + int n = -1; + __printf_buffer (&buf.base, "D%nEF", &n); + TEST_COMPARE (n, 2); /* Two characters were skipped. */ + TEST_VERIFY (buf.base.write_base == buf.direct); + TEST_COMPARE_BLOB (buf.base.write_base, + buf.base.write_ptr - buf.base.write_base, "abcDEF", 6); + TEST_VERIFY (!__printf_buffer_has_failed (&buf.base)); + TEST_COMPARE (__printf_buffer_done (&buf.base), 4); /* Two skipped. */ + TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1); + } + return 0; } -- 2.43.0