From patchwork Tue Sep 24 14:08:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 1988983 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=cKPzT4Re; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XChYQ6DXKz1xsn for ; Wed, 25 Sep 2024 00:08:58 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 910DD3858D33 for ; Tue, 24 Sep 2024 14:08:52 +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 ESMTP id 136AF3858D20 for ; Tue, 24 Sep 2024 14:08:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 136AF3858D20 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 136AF3858D20 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=1727186912; cv=none; b=NoHK9LOcyUgTcI+ysZ8S6aj7p/V+5ieyn+UnuMK/468Lr+N1mq2SYtH+f0Ms4Bgc6X9GotgZn0H6vaDv9jwbts+J+uwcXlNL/fufyanGEwkES2oCEPLQmzTgyOsBbD5b6V8T6pPM22miZNB3Rlw+VUEApaeVUg0n2nAcQTXzlMs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727186912; c=relaxed/simple; bh=L3Su2XRTh/9vNuTxvDYIltBUiGt/ovCeqQnIKiSJSc4=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=rXf+owNi2ONDOfTLE7TG+FFkAXynAD1LtWDp9SVRSLYruenixJP/XDTjPN0nmHeKxaUYQ6+9TCF7u4wiHOcJcpPqSCArx0PmgMgVTXqwTsADyuPLwJ53606KKQ7mfVhC0oOAT9qbt8tb1a9oY0Y7pefmY69OGN8Agm94uaV7tZ8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727186908; h=from:from: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=w9NwCDrWFEPBBAjeQZuGsWuLxwwi7H73drwq6BbKA7A=; b=cKPzT4ReE5ZvxzCfxHMr7O45dvTTETJd/gxrAchLKwIfDG+ytih7r+zyUj8oah0vjNAbB4 b8pU14iJMJcjO2zmVjZuQWLYT0SS+4orYw13TGH6EWOZf1hcpLiFGkVSAifJRSOid0CfOY 4/ZciJv6rSy6lm1lgob0ZtM/4cJCTkg= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-656-0FPmztdIMKiqmtohTIW6Fw-1; Tue, 24 Sep 2024 10:08:27 -0400 X-MC-Unique: 0FPmztdIMKiqmtohTIW6Fw-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-42cbcf60722so43518275e9.1 for ; Tue, 24 Sep 2024 07:08:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727186906; x=1727791706; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=w9NwCDrWFEPBBAjeQZuGsWuLxwwi7H73drwq6BbKA7A=; b=RMn+2iPqxb05ocXalgJGGLZJVhLZ2a2TLunQzZHlG6U8JD9+wSyXM76Nkm5DWXfEu1 xynWIndNFTWEDxeXUu1nDesglaMB25IeyIjBcNKo9qvHl4mWDpAw6sBAJM2IHXPLiqsP Q+ztTwgEncytxV4kLMfAJC/PGYtohl2LEvFJ9/xIboXWLVEgSbHfBVcERZAzvUAgP57W +fZylnKIhzl9zZqdPkEzl1GEFjInAKmMvrM7cNG2u+DAkMjuaFs66Ld9SJ0ARTGTseIc tk3b6Mrxb/9mt4Ojt0qaWA3BZKpU+s6Y5bIWMD84KJZn/vdyPOA8Hjv6jvgmLSzjIsXf uVjg== X-Gm-Message-State: AOJu0YwXgPgMcky9VY2YXpxfb/5pvK4e0k2LLSlFJ2MNmYXL1FnJOLO1 wgCWBk4gy59nxHBFt44Cl9ow3eCOdLKjoyyMIbJizaxEvtJEhtsunhZKA/1Doiu80gdTWhVKcad iyY7XQc/hTPFgx6OUBSlxoxh5Gx1ImOsMqPQRd9FOYgl2fWOzxCaCdE8cvHS/vunIlg== X-Received: by 2002:a05:600c:1913:b0:42b:8a35:1acf with SMTP id 5b1f17b1804b1-42e7c19bba1mr116273405e9.25.1727186905959; Tue, 24 Sep 2024 07:08:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF5C0fTSi7qvZM8+3kC51Zb2F4n8uwkbX9fi/eNHj2ay91DWScegSLEru7fOkGZwYIVzvsK7w== X-Received: by 2002:a05:600c:1913:b0:42b:8a35:1acf with SMTP id 5b1f17b1804b1-42e7c19bba1mr116273245e9.25.1727186905523; Tue, 24 Sep 2024 07:08:25 -0700 (PDT) Received: from digraph.polyomino.org.uk (digraph.polyomino.org.uk. [2001:8b0:bf73:93f7::51bb:e332]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42e754a7ae2sm161880925e9.32.2024.09.24.07.08.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Sep 2024 07:08:25 -0700 (PDT) Received: from jsm28 (helo=localhost) by digraph.polyomino.org.uk with local-esmtp (Exim 4.97) (envelope-from ) id 1st6DH-000000014Ro-2lG0; Tue, 24 Sep 2024 14:08:23 +0000 Date: Tue, 24 Sep 2024 14:08:23 +0000 (UTC) From: Joseph Myers To: DJ Delorie cc: libc-alpha@sourceware.org Subject: [PATCH v2] Add tests of fread [committed] In-Reply-To: Message-ID: <075533ad-4deb-a154-845a-9eb51671e589@redhat.com> References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-9.1 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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~incoming=patchwork.ozlabs.org@sourceware.org On Mon, 23 Sep 2024, DJ Delorie wrote: > There are three includes you could do without, but leaving them in won't > hurt either. LGTM. Removed. Committed revised patch in the form below after retesting. > I wonder if it would be useful to have a test that makes sure > fread(x,-1,-1,y) doesn't blindly multply -1*-1 and read one > byte... (yes, I know they're unsigned). Testing overflow like that is problematic. The handling of overflow is arguably buggy, but apparently the Linux kernel expects a buffer as large as the passed size even if in fact you know there will be EOF long before then. See bug 19165 and the past discussion referenced therein. Add tests of fread There seem to be no glibc tests specifically for the fread function. Add basic tests of that function. Tested for x86_64. --- Changed in v2: removed unnecessary includes. diff --git a/stdio-common/Makefile b/stdio-common/Makefile index 62f8b99b06..e3e5f634c5 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -217,6 +217,7 @@ tests := \ tst-fmemopen4 \ tst-fphex \ tst-fphex-wide \ + tst-fread \ tst-freopen2 \ tst-freopen3 \ tst-freopen4 \ diff --git a/stdio-common/tst-fread.c b/stdio-common/tst-fread.c new file mode 100644 index 0000000000..4d9a7895f6 --- /dev/null +++ b/stdio-common/tst-fread.c @@ -0,0 +1,134 @@ +/* Test fread. + 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 +#include +#include + +int +do_test (void) +{ + char *temp_dir = support_create_temp_directory ("tst-fread"); + char *file1 = xasprintf ("%s/file1", temp_dir); + support_write_file_string (file1, "file1"); + add_temp_file (file1); + FILE *fp; + size_t ret; + char buf[1024]; + + verbose_printf ("test single-byte reads\n"); + fp = xfopen (file1, "r"); + memset (buf, 0, sizeof buf); + ret = fread (buf, 1, 2, fp); + TEST_COMPARE (ret, 2); + TEST_COMPARE (buf[0], 'f'); + TEST_COMPARE (buf[1], 'i'); + TEST_COMPARE (feof (fp), 0); + TEST_COMPARE (ftell (fp), 2); + memset (buf, 0, sizeof buf); + ret = fread (buf, 1, 3, fp); + TEST_COMPARE (ret, 3); + TEST_COMPARE (buf[0], 'l'); + TEST_COMPARE (buf[1], 'e'); + TEST_COMPARE (buf[2], '1'); + TEST_COMPARE (ftell (fp), 5); + TEST_COMPARE (feof (fp), 0); + memset (buf, 0, sizeof buf); + ret = fread (buf, 1, 1, fp); + TEST_COMPARE (ret, 0); + TEST_COMPARE (!!feof (fp), 1); + TEST_COMPARE (ferror (fp), 0); + TEST_COMPARE (ftell (fp), 5); + xfclose (fp); + + verbose_printf ("test single-byte reads, EOF part way through\n"); + fp = xfopen (file1, "r"); + memset (buf, 0, sizeof buf); + ret = fread (buf, 1, sizeof buf, fp); + TEST_COMPARE (ret, 5); + TEST_COMPARE (buf[0], 'f'); + TEST_COMPARE (buf[1], 'i'); + TEST_COMPARE (buf[2], 'l'); + TEST_COMPARE (buf[3], 'e'); + TEST_COMPARE (buf[4], '1'); + TEST_COMPARE (!!feof (fp), 1); + TEST_COMPARE (ferror (fp), 0); + TEST_COMPARE (ftell (fp), 5); + xfclose (fp); + + verbose_printf ("test multi-byte reads\n"); + fp = xfopen (file1, "r"); + memset (buf, 0, sizeof buf); + ret = fread (buf, 2, 2, fp); + TEST_COMPARE (ret, 2); + TEST_COMPARE (buf[0], 'f'); + TEST_COMPARE (buf[1], 'i'); + TEST_COMPARE (buf[2], 'l'); + TEST_COMPARE (buf[3], 'e'); + TEST_COMPARE (feof (fp), 0); + TEST_COMPARE (ftell (fp), 4); + memset (buf, 0, sizeof buf); + ret = fread (buf, 3, 3, fp); + TEST_COMPARE (ret, 0); + /* The bytes written for a partial element read are unspecified. */ + TEST_COMPARE (!!feof (fp), 1); + TEST_COMPARE (ferror (fp), 0); + TEST_COMPARE (ftell (fp), 5); + xfclose (fp); + + verbose_printf ("test read error\n"); + fp = xfopen (file1, "r"); + xclose (fileno (fp)); + memset (buf, 0, sizeof buf); + ret = fread (buf, 1, sizeof buf, fp); + TEST_COMPARE (ret, 0); + TEST_COMPARE (feof (fp), 0); + TEST_COMPARE (!!ferror (fp), 1); + fclose (fp); + + verbose_printf ("test zero size\n"); + fp = xfopen (file1, "r"); + ret = fread (buf, 0, SIZE_MAX, fp); + TEST_COMPARE (ret, 0); + TEST_COMPARE (feof (fp), 0); + TEST_COMPARE (ferror (fp), 0); + TEST_COMPARE (ftell (fp), 0); + xfclose (fp); + + verbose_printf ("test zero items\n"); + fp = xfopen (file1, "r"); + ret = fread (buf, SIZE_MAX, 0, fp); + TEST_COMPARE (ret, 0); + TEST_COMPARE (feof (fp), 0); + TEST_COMPARE (ferror (fp), 0); + TEST_COMPARE (ftell (fp), 0); + xfclose (fp); + + free (temp_dir); + free (file1); + return 0; +} + +#include