From patchwork Fri Feb 9 15:25:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 85530 X-Patchwork-Delegate: azanella@linux.vnet.ibm.com 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 96B1D385800B for ; Fri, 9 Feb 2024 15:26:48 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org 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 64045385840E for ; Fri, 9 Feb 2024 15:26:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 64045385840E 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 64045385840E 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=1707492362; cv=none; b=s64cr6fOwPYCSGNeZpVtGKwPb1dEVk22LWYy9UuU+LPjjJWECSqwpPfm0gvBoMXxt5vjZK58UIBOx39n3lBqHVfXSEQLolTGWUNgOsdyEICHmIjYotvAA4mec9UOKH1ZOPUtkoXfnNcfBiO0h4gd/ouqIaGQ4nojsT/juo5EHSI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492362; c=relaxed/simple; bh=7s+dOT5f+NbpWsjKyFwoLuF0ADKqiYTSTTiEEmkeWqM=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=MGeXn00QWVN9Gcbw4qRZvjZq0nLYg9joWAIU0NEtDU53FjCJJ9FlhT/ZzXzO8xv8Sy8iEUpDnClENZiEMhQKOu4yjpbiNeX67nVLxQMzNNy+TJlY1ovtGWSnxK60ISDbctKFKhgNqqVqXwDoJC0LD5NtPHcZ/X/m7VjcgvKhYwU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707492360; 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=r83C3hQL7sdsTCWEBNG6C7vDS62UqREJfsPHEp6fvSI=; b=DW4FxgIHpej9Hpd1/srjvsQHoieujMrhFOAC0/iWSJDtGNMdBOGOBPfOFmbZxsR8B8d0nU KSGje5rB6za+bJKIxuhR938QrAwh956Netp7DbOUm9/KW4nQMEXJL9sWugXMV7trzLLE9P TGTYCBjrwOmmR1RiZZBmggreQkggLhc= 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-512-JCGqwgKzOlmEHDL7tY0d6g-1; Fri, 09 Feb 2024 10:25:58 -0500 X-MC-Unique: JCGqwgKzOlmEHDL7tY0d6g-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (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 A42603C1E9D0 for ; Fri, 9 Feb 2024 15:25:58 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.58]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 10B9140C9444 for ; Fri, 9 Feb 2024 15:25:57 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 10/11] stdio-common: Support large offsets with %lln In-Reply-To: Message-ID: References: X-From-Line: d25f0a580fde5721d594492cbb8a37d38b53c702 Mon Sep 17 00:00:00 2001 Date: Fri, 09 Feb 2024 16:25:55 +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.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Use of Xprintf_buffer_done is unnecessary because it performs overflow detection against int and sets errno. --- stdio-common/Makefile | 1 + stdio-common/tst-printf-large-n.c | 81 +++++++++++++++++++++++++++++ stdio-common/vfprintf-process-arg.c | 2 +- 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 stdio-common/tst-printf-large-n.c diff --git a/stdio-common/Makefile b/stdio-common/Makefile index d610ed67e6..482d841f96 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -234,6 +234,7 @@ tests := \ tst-printf-fp-free \ tst-printf-fp-leak \ tst-printf-intn \ + tst-printf-large-n \ tst-printf-oct \ tst-printf-round \ tst-printfsz \ diff --git a/stdio-common/tst-printf-large-n.c b/stdio-common/tst-printf-large-n.c new file mode 100644 index 0000000000..4504a693d2 --- /dev/null +++ b/stdio-common/tst-printf-large-n.c @@ -0,0 +1,81 @@ +/* Test that %n can report values larger than INT_MAX. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +#include +#include +#include + +static int +do_test (void) +{ + enum { string_length = 1 << 28 }; + + FILE *fp = xfopen ("/dev/null", "w"); + + /* Byte-oriented streams. */ + { + struct support_blob_repeat repeat + = support_blob_repeat_allocate ("a", 1, string_length + 1); + char *string = repeat.start; + if (string == NULL) + FAIL_UNSUPPORTED ("cannot allocate large string"); + string[string_length] = '\0'; + + long long int n; + TEST_COMPARE (fprintf (fp, "%s %s %s %s %s %s %s %s %lln", + string, string, string, string, string, string, + string, string, &n), -1); + TEST_COMPARE (errno, EOVERFLOW); + TEST_COMPARE (n, 8 * (string_length + 1LL)); + TEST_VERIFY (n > INT_MAX); + + support_blob_repeat_free (&repeat); + } + + /* Wide-oriented streams. */ + { + struct support_blob_repeat repeat + = support_blob_repeat_allocate (L"a", sizeof (wchar_t), + string_length + 1); + wchar_t *string = repeat.start; + if (string == NULL) + FAIL_UNSUPPORTED ("cannot allocate large wide string"); + string[string_length] = '\0'; + + long long int n; + TEST_COMPARE (fwprintf (fp, L"%Ls %Ls %Ls %Ls %Ls %Ls %Ls %Ls %lln", + string, string, string, string, string, string, + string, string, &n), -1); + TEST_COMPARE (errno, EOVERFLOW); + TEST_COMPARE (n, 8 * (string_length + 1LL)); + TEST_VERIFY (n > INT_MAX); + + support_blob_repeat_free (&repeat); + } + + xfclose (fp); + + return 0; +} + +#include diff --git a/stdio-common/vfprintf-process-arg.c b/stdio-common/vfprintf-process-arg.c index af6b570ea9..dbc78d4942 100644 --- a/stdio-common/vfprintf-process-arg.c +++ b/stdio-common/vfprintf-process-arg.c @@ -337,7 +337,7 @@ LABEL (form_number): } /* Answer the count of characters written. */ void *ptrptr = process_arg_pointer (); - unsigned int written = Xprintf_buffer_done (buf); + uint64_t written = buf->written + (buf->write_ptr - buf->write_base); if (is_longlong) *(long long int *) ptrptr = written; else if (is_long_num)