From patchwork Wed Sep 4 03:00:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: DJ Delorie X-Patchwork-Id: 1980427 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=et55zEce; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4Wz6h52lDxz1yg3 for ; Wed, 4 Sep 2024 13:01:09 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 977143860778 for ; Wed, 4 Sep 2024 03:01:07 +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 44320385C6D1 for ; Wed, 4 Sep 2024 03:00:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 44320385C6D1 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 44320385C6D1 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=1725418851; cv=none; b=GNof9fqXGBU7ZqS6lqb3/OJJHgbYVk8y3pPXqczFYEfu+CFFxuPBu7FMSXWyjihErjZHU/AzdBUyTCwDLDFcipRxvyGkQaJEixf78YLNSmRFpOJtTHiTeTr57FPB9DyZhDGxgiIiwbTrkIA+S2zZm2weJEMs1pdnnlbZvqGTNqg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725418851; c=relaxed/simple; bh=MVzZVDXGZqhYZhCt4Iy6sNpopDqSB/6YPGOUI1+UogA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=FnzfS5q4+XUAFWrcwTaqSwhKs+CnvzM4vEFaOUWWXp4zTaCF3d+GqmnVIN0hT0QADNwPIPeLhSTlsmqmb5hze3Jj4p+/3/Jzrq2vbnPSMiG18MxR24nOXDZf8eq1pY7KCnuj+ALiBFxw7FWK4/9AgcGDNXczaExteWFgy5j34JA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725418843; 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; bh=bM3N17QyjczUijGFRAWwZb7uPYY8EYeQgoGdARaDcWQ=; b=et55zEcetMsaWlA+n6ggzsenF/cNevjA8mocrPGv5t2gyifSo1qQ1O2E/ts5o355/BXVGL +r2D6wbAoHGYIC+dv/NNpqlSf7w/U9uVwlGFCCrix84a2MbNhGjMHS5rkWFlaUM/z5S4zr Uwss3JnsRwBQSG9TaADzHGYtT9dJvYc= Received: from mx-prod-mc-04.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-623-ExAPR323NRK-FHNkMtMDfQ-1; Tue, 03 Sep 2024 23:00:42 -0400 X-MC-Unique: ExAPR323NRK-FHNkMtMDfQ-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E96941955D4A for ; Wed, 4 Sep 2024 03:00:41 +0000 (UTC) Received: from greed.delorie.com (unknown [10.22.9.113]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A66171955F1B; Wed, 4 Sep 2024 03:00:41 +0000 (UTC) Received: from greed.delorie.com.redhat.com (localhost [127.0.0.1]) by greed.delorie.com (8.16.1/8.16.1) with ESMTP id 48430esn041576; Tue, 3 Sep 2024 23:00:40 -0400 From: DJ Delorie To: libc-alpha@sourceware.org Cc: Florian Weimer Subject: [patch v2] fgets: more tests Date: Tue, 03 Sep 2024 23:00:40 -0400 Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 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, RCVD_IN_SBL_CSS, 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~incoming=patchwork.ozlabs.org@sourceware.org Add more tests for unusual situations fgets() might see: * zero size file * zero sized buffer * NULL buffer * NUL data * writable stream * closed stream Reviewed-by: Florian Weimer diff --git a/stdio-common/Makefile b/stdio-common/Makefile index 948d960ccc..2eec76c88b 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -209,6 +209,7 @@ tests := \ tst-fdopen \ tst-ferror \ tst-fgets \ + tst-fgets2 \ tst-fileno \ tst-fmemopen \ tst-fmemopen2 \ diff --git a/stdio-common/tst-fgets2.c b/stdio-common/tst-fgets2.c new file mode 100644 index 0000000000..c11fd6687c --- /dev/null +++ b/stdio-common/tst-fgets2.c @@ -0,0 +1,253 @@ +/* Test for additional fgets error handling. + 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 +#include +#include +#include +#include + +#include +#include + +/* This avoids compiler warnings about passing NULL where a valid + pointer is expected. */ +static void *volatile null = NULL; + +/* Implementation of our FILE stream backend. */ + +static int bytes_read; +static int cookie_valid = 0; +struct Cookie { + const char *buffer; + int bufptr; + int bufsz; +}; + +#define VALIDATE_COOKIE() if (! cookie_valid) { \ + FAIL ("call to %s after file closed", __FUNCTION__); \ + return -1; \ + } + +static ssize_t +io_read (void *vcookie, char *buf, size_t size) +{ + struct Cookie *cookie = (struct Cookie *) vcookie; + + VALIDATE_COOKIE (); + + if (size > cookie->bufsz - cookie->bufptr) + size = cookie->bufsz - cookie->bufptr; + + memcpy (buf, cookie->buffer + cookie->bufptr, size); + cookie->bufptr += size; + bytes_read += size; + return size; +} + +static ssize_t +io_write (void *vcookie, const char *buf, size_t size) +{ + VALIDATE_COOKIE (); + FAIL_EXIT1 ("io_write called"); +} + +static int +io_seek (void *vcookie, off64_t *position, int whence) +{ + VALIDATE_COOKIE (); + FAIL_EXIT1 ("io_seek called"); +} + +static int +io_clean (void *vcookie) +{ + struct Cookie *cookie = (struct Cookie *) vcookie; + + VALIDATE_COOKIE (); + + cookie->buffer = NULL; + cookie->bufsz = 0; + cookie->bufptr = 0; + + cookie_valid = 0; + free (cookie); + return 0; +} + +cookie_io_functions_t io_funcs = { + .read = io_read, + .write = io_write, + .seek = io_seek, + .close = io_clean +}; + +FILE * +io_open (const char *buffer, int buflen, const char *mode, void **vcookie) +{ + FILE *f; + struct Cookie *cookie; + + cookie = (struct Cookie *) xcalloc (1, sizeof (struct Cookie)); + *vcookie = cookie; + cookie_valid = 1; + + cookie->buffer = buffer; + cookie->bufsz = buflen; + bytes_read = 0; + + f = fopencookie (cookie, mode, io_funcs); + if (f == NULL) + FAIL_EXIT1 ("fopencookie failed"); + + clearerr (f); + return f; +} + +/* The test cases. */ + +#define my_open(s,l,m) io_open (s, l, m, (void *) &cookie) + +#define TEST_COMPARE_0x11(buf, len) \ + TEST_COMPARE_BLOB (buf + (len), sizeof(buf) - (len), \ + buf2, sizeof(buf) - (len)); + +#define check_flags(f, expected_eof, expected_err) \ + { \ + if (expected_eof) \ + TEST_VERIFY (feof (f) != 0); \ + else \ + TEST_VERIFY (feof (f) == 0); \ + if (expected_err) \ + TEST_VERIFY (ferror (f) != 0); \ + else \ + TEST_VERIFY (ferror (f) == 0); \ + } + +static int +do_test (void) +{ + FILE *f; + struct Cookie *cookie; + char buf [10]; + char buf2 [10]; + char *returned_string; + + memset (buf2, 0x11, sizeof (buf2)); + + printf ("testing base operation...\n"); + f = my_open ("hello\n", 6, "r"); + memset (buf, 0x11, sizeof (buf)); + returned_string = fgets (buf, sizeof(buf) - 1, f); + TEST_VERIFY (returned_string == buf); + TEST_COMPARE_BLOB (buf, bytes_read + 1, "hello\n\0", 7); + TEST_COMPARE_0x11 (buf, bytes_read + 1); + check_flags (f, 0, 0); + + fclose (f); + + printf ("testing zero size file...\n"); + f = my_open ("hello\n", 0, "r"); + memset (buf, 0x11, sizeof (buf)); + returned_string = fgets (buf, sizeof(buf) - 1, f); + TEST_VERIFY (returned_string == NULL); + TEST_VERIFY (bytes_read == 0); + check_flags (f, 1, 0); + fclose (f); + + printf ("testing zero size buffer...\n"); + f = my_open ("hello\n", 6, "r"); + memset (buf, 0x11, sizeof (buf)); + returned_string = fgets (buf, 0, f); + TEST_VERIFY (returned_string == NULL); + TEST_VERIFY (bytes_read == 0); + check_flags (f, 0, 0); + fclose (f); + + printf ("testing NULL buffer with empty stream...\n"); + f = my_open ("hello\n", 0, "r"); + memset (buf, 0x11, sizeof (buf)); + + returned_string = fgets (null, sizeof(buf), f); + + TEST_VERIFY (returned_string == NULL); + TEST_VERIFY (bytes_read == 0); + check_flags (f, 1, 0); + fclose (f); + + printf ("testing embedded NUL...\n"); + f = my_open ("hel\0lo\n", 7, "r"); + memset (buf, 0x11, sizeof (buf)); + returned_string = fgets (buf, sizeof(buf) - 1, f); + TEST_VERIFY (returned_string == buf); + TEST_COMPARE_BLOB (buf, bytes_read + 1, "hel\0lo\n\0", 8); + TEST_COMPARE_0x11 (buf, bytes_read + 1); + check_flags (f, 0, 0); + fclose (f); + + printf ("testing writable stream...\n"); + f = my_open ("hel\0lo\n", 7, "w"); + memset (buf, 0x11, sizeof (buf)); + returned_string = fgets (buf, sizeof(buf) - 1, f); + TEST_VERIFY (returned_string == NULL); + TEST_VERIFY (bytes_read == 0); + check_flags (f, 0, 1); + fclose (f); + + printf ("testing closed fd stream...\n"); + int fd = open ("/dev/null", O_RDONLY); + f = fdopen (fd, "r"); + close (fd); + memset (buf, 0x11, sizeof (buf)); + returned_string = fgets (buf, sizeof(buf) - 1, f); + TEST_VERIFY (returned_string == NULL); + TEST_VERIFY (bytes_read == 0); + check_flags (f, 0, 1); + fclose (f); + +#ifdef IO_DEBUG + /* These tests only pass if glibc is built with -DIO_DEBUG, but are + included for reference. */ + + printf ("testing NULL descriptor...\n"); + memset (buf, 0x11, sizeof (buf)); + returned_string = fgets (buf, sizeof(buf) - 1, null); + TEST_VERIFY (returned_string == NULL); + TEST_VERIFY (bytes_read == 0); + + printf ("testing closed descriptor...\n"); + f = my_open ("hello\n", 7, "r"); + fclose (f); + memset (buf, 0x11, sizeof (buf)); + returned_string = fgets (buf, sizeof(buf) - 1, f); + TEST_VERIFY (returned_string == NULL); + TEST_VERIFY (bytes_read == 0); +#endif + + return 0; +} + +#include