From patchwork Wed Nov 6 16:03:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Berat X-Patchwork-Id: 2007612 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=Ew72q6Zy; 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 4Xk95265lwz1xyS for ; Thu, 7 Nov 2024 03:04:38 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 03CD63858C48 for ; Wed, 6 Nov 2024 16:04:37 +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.129.124]) by sourceware.org (Postfix) with ESMTP id 97BD13858D20 for ; Wed, 6 Nov 2024 16:04:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 97BD13858D20 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 97BD13858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730909050; cv=none; b=M8tSS2w3wxM4jVkTgABYVqaQJe1XxIgZsegH51cclLz/5e8izGNpmb7Nme04zrBDoCH9TwkFD0KWdfQlE+sFzgeK7JxPWVgVtOR47Qf2FblHhSt0xbzz1NTim0jZaQtpFq1yAbO0WOzguTv98hucps5spmvUA2qAMBX0HKAjUsc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730909050; c=relaxed/simple; bh=bB7apsEIS3j90ftmUwmmu7vqdbnYwMDc3iMJh8/lIVc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=cOiNHQifYNo5S1SVjo2wu1tF427g1TWH/vBS/weGDf5NCAfMEGjCJpdoNwOI53v1YsEqhgw00wQVGoWQVURs3V4Cw6or6lxhI7jg+yJP/egRSHNxkT4y0xlongVRTMTuubJ4Fgn7v+uaX8Tc14+G06d9ku50Q8UUavHEx/FPznU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730909043; 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: content-transfer-encoding:content-transfer-encoding; bh=sQnrVYnjGNmXalcoTyVM7G/i0enn2yRpcqZZ49cJkW8=; b=Ew72q6Zyj2RdOkLUtPO0HXuKUWMmU6RevRknlj8aDkrLxa9E5DCo201CguWxwvg+uRXVUY 8bBbUmJiDCrnpgqGVPq2i/w8cXvgrNAvYd6g3pFNavfis8WFD/A2Q4jOQdIlgjrLD/M7Bb gHKJAviKD69ZG2Wn2N3+AzucGAY44i0= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-519-_9Lx4UJXP5mvDe5b9Hi_6Q-1; Wed, 06 Nov 2024 11:04:02 -0500 X-MC-Unique: _9Lx4UJXP5mvDe5b9Hi_6Q-1 X-Mimecast-MFC-AGG-ID: _9Lx4UJXP5mvDe5b9Hi_6Q Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BD7511955D80 for ; Wed, 6 Nov 2024 16:04:00 +0000 (UTC) Received: from Nymeria-redhat.redhat.com (unknown [10.39.192.36]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C7C741955F41; Wed, 6 Nov 2024 16:03:59 +0000 (UTC) From: =?utf-8?b?RnLDqWTDqXJpYyBCw6lyYXQ=?= To: libc-alpha@sourceware.org Cc: fweimer@redhat.com Subject: [PATCH v2] libio: Add test case for fflush Date: Wed, 6 Nov 2024 17:03:55 +0100 Message-ID: <20241106160355.2626947-1-fberat@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: i5s_SHIMMAkeGdH-8XsMTxZ4j3vAqtDoVxosn1do9FM_1730909041 X-Mimecast-Originator: redhat.com 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 Hello, This patch adds a test to verify that `fflush (FILE)` and `fflush (NULL)` are semantically equivalent from the FILE perspective, which currently fails if the file is opened with read mode. Based on my experiment, I'm confused by the behavior of the stream. In the current implementation, in order to test the `read` case, I perform the following operations (assuming file content is "N:M", where N and M are digits): char c = fgetc (FILE); c = fgetc (FILE); ungetc (c, FILE); fflush (FILE); pos = lseek (SEEK_CUR); Which gives a `pos` value of 1. I also tested the following: char s[3] = ""; fgets (s, 2, FILE); ungetc (s[1], FILE); fflush (FILE); pos = lseek (SEEK_CUR); Which gives a `pos` value of 2. While there can be argumentation around POSIX interpretation on whether file position needs to be reset on fflush if ungetc hasn't been read, I would expect both use cases to give the same result. Any comment is welcome. Fred. Changes since v1: - Add a test specifically for a stream opened with read mode. - Split test in 2, and mark the `fflush (NULL)` as XFAIL (see comment in the patch). --- 8< --- Subject: [PATCH] libio: Add test case for fflush Since one path uses _IO_SYNC and the other _IO_OVERFLOW, the newly added test cases verifies that `fflush (FILE)` and `fflush (NULL)` are semantically equivalent from the FILE perspective. These tests show a discrepancy if the file stream is opened with read mode, hence `fflush (NULL)` is currently marked as XFAIL until this is fixed. --- libio/Makefile | 5 + libio/tst-fflush-NULL.c | 23 +++++ libio/tst-fflush-skeleton.c | 196 ++++++++++++++++++++++++++++++++++++ libio/tst-fflush.c | 20 ++++ 4 files changed, 244 insertions(+) create mode 100644 libio/tst-fflush-NULL.c create mode 100644 libio/tst-fflush-skeleton.c create mode 100644 libio/tst-fflush.c diff --git a/libio/Makefile b/libio/Makefile index 4370152964..27534b9675 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -100,6 +100,8 @@ tests = \ tst-fclose-unopened \ tst-fclose-unopened2 \ tst-fdopen-seek-failure \ + tst-fflush \ + tst-fflush-NULL \ tst-fgetc-after-eof \ tst-fgetwc \ tst-fgetws \ @@ -146,6 +148,9 @@ tests = \ tst_wscanf \ # tests +# tst-fflush-NULL as XFAIL until read stream bug is fixed +test-xfail-tst-fflush-NULL = yes + $(objpfx)tst-popen-fork: $(shared-thread-library) tests-internal = tst-vtables tst-vtables-interposed diff --git a/libio/tst-fflush-NULL.c b/libio/tst-fflush-NULL.c new file mode 100644 index 0000000000..23c741e0dc --- /dev/null +++ b/libio/tst-fflush-NULL.c @@ -0,0 +1,23 @@ +/* Test that fflush (FILE) and fflush (NULL) are semantically equivalent. + + 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 + . */ + +#define FILE_FLUSH_TYPE 1 +#define S_FLUSH_TYPE "NULL" + +#include "tst-fflush-skeleton.c" diff --git a/libio/tst-fflush-skeleton.c b/libio/tst-fflush-skeleton.c new file mode 100644 index 0000000000..5db9fc96da --- /dev/null +++ b/libio/tst-fflush-skeleton.c @@ -0,0 +1,196 @@ +/* Test that fflush (FILE) and fflush (NULL) are semantically equivalent. + + 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 + . */ + +/* A success on this test doesn't imply the effectiveness of fflush as + we can't ensure that the file wasn't already in the expected state + before the call of the function. It only ensures that, if the test + fails, fflush is broken. */ + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +#define CONTENT_SZ_MAX 32 +#define TEST_FILE_COUNT 10 + +#define FILE_FLUSH 0 +#define GLOBAL_FLUSH 1 + +#ifndef FILE_FLUSH_TYPE +# define FILE_FLUSH_TYPE FILE_FLUSH +# define S_FLUSH_TYPE "FILE" +#endif + +#define FILE_UNCHANGED 0 +#define FILE_CHANGED 1 + +struct +{ + FILE *file; + char *name; + int fd; + char *mfile; +} files[TEST_FILE_COUNT]; + +static void +base_init (int file) +{ + files[file].file = NULL; + files[file].fd = -1; + files[file].name = NULL; + files[file].mfile = NULL; +} + +static void +file_init (int file) +{ + int fd = -1; + + base_init (file); + + if (file >= TEST_FILE_COUNT) + return; + + xclose (create_temp_file ("tst-fflush", &files[file].name)); + + fd = xopen (files[file].name, O_RDONLY, 0); + files[file].mfile = (char *) xmmap (NULL, CONTENT_SZ_MAX, PROT_READ, + MAP_SHARED, fd); + xclose (fd); +} + +static void +file_cleanup (int file) +{ + free (files[file].name); + xmunmap (files[file].mfile, CONTENT_SZ_MAX); + + base_init (file); +} + +static int +file_changed (int to_check, int global_flush, const char *mode) +{ + struct stat stats = { }; + bool content_matches = 0; + char expected[CONTENT_SZ_MAX] = { }; + + verbose_printf ("Check that %s (%d) exactly contains the data we put in\n", + files[to_check].name, to_check); + + /* File should contain "N:M" where both N and M are one digit exactly. */ + snprintf (expected, sizeof (expected), "%d:%d", global_flush, to_check); + content_matches + = (strncmp (files[to_check].mfile, expected, sizeof (expected)) == 0); + + TEST_VERIFY_EXIT (content_matches != 0); + + TEST_VERIFY_EXIT (fstat (files[to_check].fd, &stats) >= 0); + TEST_VERIFY_EXIT (stats.st_size == 3); + if (strncmp(mode, "r", 1) == 0) + TEST_VERIFY_EXIT (lseek (files[to_check].fd, 0, SEEK_CUR) == 1); + else + TEST_VERIFY_EXIT (lseek (files[to_check].fd, 0, SEEK_CUR) == 3); + + + /* Not reached if the data doesn't match. */ + return FILE_CHANGED; +} + +static void +file_flush (int global_flush, const char *mode) +{ + for (int i = 0; i < TEST_FILE_COUNT; i++) { + files[i].file = xfopen (files[i].name, mode); + TEST_VERIFY_EXIT (files[i].file != NULL); + files[i].fd = fileno (files[i].file); + } + + /* Print a unique identifier in each file, that is not too long nor contain + new line to not trigger _IO_OVERFLOW/_IO_SYNC. */ + for (int i = 0; i < TEST_FILE_COUNT; i++) { + if (strncmp(mode, "r", 1) == 0) + { + char c = (char) fgetc (files[i].file); + c = (char) fgetc (files[i].file); + ungetc (c, files[i].file); + } + else + { + fprintf (files[i].file, "%d:%d", global_flush, i); + } + } + + if (global_flush) + { + fflush (NULL); + } + else + { + for (int i = 0; i < TEST_FILE_COUNT; i++) + fflush (files[i].file); + } + + for (int i = 0; i < TEST_FILE_COUNT; i++) + { + int changed = file_changed (i, global_flush, mode); + + verbose_printf ("Check that file %s has been modified after fflush\n", + files[i].name); + verbose_printf ("File %s; changed %d; global fflush %d\n", + files[i].name, changed, !!global_flush); + TEST_VERIFY_EXIT (changed != FILE_UNCHANGED); + } + + for (int i = 0; i < TEST_FILE_COUNT; i++) + xfclose (files[i].file); +} + +static int +do_test (void) +{ + for (int i = 0; i < TEST_FILE_COUNT; i++) + file_init (i); + + verbose_printf ("Checking fflush(" S_FLUSH_TYPE "), WRITE mode\n"); + file_flush (FILE_FLUSH_TYPE, "w"); + + verbose_printf ("Checking fflush(" S_FLUSH_TYPE "), READWRITE mode\n"); + file_flush (FILE_FLUSH_TYPE, "r+"); + + for (int i = 0; i < TEST_FILE_COUNT; i++) + file_cleanup (i); + + return 0; +} + +#include diff --git a/libio/tst-fflush.c b/libio/tst-fflush.c new file mode 100644 index 0000000000..525ed0f1f2 --- /dev/null +++ b/libio/tst-fflush.c @@ -0,0 +1,20 @@ +/* Test that fflush (FILE) and fflush (NULL) are semantically equivalent. + + 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 "tst-fflush-skeleton.c"