From patchwork Tue Aug 20 02:04:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: DJ Delorie X-Patchwork-Id: 96111 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 A5F21384A47D for ; Tue, 20 Aug 2024 02:06:18 +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 36D03384A452 for ; Tue, 20 Aug 2024 02:04:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 36D03384A452 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 36D03384A452 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=1724119531; cv=none; b=STXVs2/L20QG7g3kdO6CtApTwfhL1ItOmmPy0gz2g4+dlmIaJtPtZcfiC3Y59QntVE9kkd08tJASwJxnLhqyr888hL6F1pmVrOKzr07DYaLtbPng80a6t42fFrB4IB1XWRrpafdvSR19/7d8KEvPo4XG3QwMszW7Ytnfl5d0dcY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724119531; c=relaxed/simple; bh=dMZD2nfR+Qeeuje4NbNVH9/id1e+Jdb0TEA0MnXBUYk=; h=DKIM-Signature:Date:Message-Id:From:To:Subject; b=GO/yngNd59yGd/qDOD63Z6QXl0PH8P2iZPfTVA1xs+lj8qGY//z9Izt1juSMUxBHhKqs3zuXpB0j2DHfi3XlQzR+DHXj5Ye+TsqntNzZJ9nvRixwxwElOdub0CgdFVjxxHPu0Pv3kpA5VxA3RB88B0HDqR2gIEQQ7gdJSFu2zP4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724119483; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:content-type:content-type; bh=bQwB/vGtLvWWzXPq5fF3sLkGnJvM0fTNYOfdFf24oQM=; b=A352MdH2ht9JNcEAG45WpRRviLJmtMXURIPi6Q+iE1CO/7EBktckjm/8zcznrC5JaTrSvt GPOH7q5wAx7gBEgKIfaTAQRbw+feuCUy+H9c+Imk7JeqsGu744XJZYGX3iCHJBH3+D3ZTV LuWan5IKVURB/E4NcYPQcnNDC0zej60= Received: from mx-prod-mc-05.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-388-BkBDAfSvO3C6Aq69qxiPHw-1; Mon, 19 Aug 2024 22:04:39 -0400 X-MC-Unique: BkBDAfSvO3C6Aq69qxiPHw-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3C2EC19560A6 for ; Tue, 20 Aug 2024 02:04:38 +0000 (UTC) Received: from greed.delorie.com (unknown [10.22.8.15]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A4D4F1955BFE for ; Tue, 20 Aug 2024 02:04:37 +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 47K24ZPf2373528 for ; Mon, 19 Aug 2024 22:04:35 -0400 Date: Mon, 19 Aug 2024 22:04:35 -0400 Message-Id: From: DJ Delorie To: libc-alpha@sourceware.org Subject: [patch v2] fgets: more tests X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_ASCII_DIVIDERS, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, 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 changes since v1: * wrapped includes in DIAG_* * a few printf->FAIL * reformat FAIL messages * macroize PASSes Add more tests for unusual situations fgets() might see: * zero size file * zero sized buffer * NULL buffer * NUL data * writable stream * closed stream diff --git a/stdio-common/Makefile b/stdio-common/Makefile index a91754f52d..30c9637e72 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..e0127317bb --- /dev/null +++ b/stdio-common/tst-fgets2.c @@ -0,0 +1,360 @@ +/* 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 + . */ + +#define _GNU_SOURCE 1 + +#include +DIAG_PUSH_NEEDS_COMMENT; +/* We're intentionally passing an invalid size and/or NULL later. */ +DIAG_IGNORE_NEEDS_COMMENT (7, "-Wnonnull"); +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +DIAG_POP_NEEDS_COMMENT; + +#include +#include + +#define PASS() printf("ok\n") + +/*------------------------------------------------------------*/ +/* Implementation of our FILE stream backend. */ + +static int bytes_read; +static int bytes_written; +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 (); + + bytes_written += size; + return -1; +} + +static int +io_seek (void *vcookie, off64_t *position, int whence) +{ + VALIDATE_COOKIE (); + return -1; +} + +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; + bytes_written = 0; + + f = fopencookie (cookie, mode, io_funcs); + if (f == NULL) + { + perror ("fopencookie"); + exit (1); + } + + return f; +} + +/*------------------------------------------------------------*/ +/* Convenience functions. */ + +static char * +hex (const char *b, int s) +{ + static int bi = 0; + static char buf[3][100]; + static char digit[] = "0123456789ABCDEF"; + char *bp; + if (s > 30) + exit (1); + bi = (bi+1)%3; + bp = buf[bi]; + while (s > 0) + { + if (isgraph (*b)) + *bp ++ = *b; + else if (*b == '\n') + { + *bp ++ = '\\'; + *bp ++ = 'n'; + } + else if (*b == '\0') + { + *bp ++ = '\\'; + *bp ++ = '0'; + } + else + { + *bp ++ = 'x'; + *bp ++ = digit[(*b >> 4) & 0x0f]; + *bp ++ = digit[*b & 0x0f]; + } + b ++; + s --; + } + *bp = 0; + return buf[bi]; +} + +#define my_open(s,l,m) io_open (s, l, m, (void *) &cookie) + +/*------------------------------------------------------------*/ +/* The test cases. */ + +static int +do_test (void) +{ + FILE *f; + struct Cookie *cookie; + char buf [100]; + char *str; + + printf ("testing base operation..."); + f = my_open ("hello\n", 6, "r"); + memset (buf, 0x11, sizeof (buf)); + str = fgets (buf, 100, f); + if (str == NULL) + { + FAIL ("returned NULL"); + } + else if (memcmp (str, "hello\n\0", 7) != 0) + { + FAIL ("returned %s instead of %s", hex (str, 7), hex ("hello\n\0", 7)); + } + else if (bytes_read != 6) + { + FAIL ("%d bytes read instead of %d", bytes_read, 6); + } + else + PASS (); + fclose (f); + + printf ("testing zero size file..."); + f = my_open ("hello\n", 0, "r"); + memset (buf, 0x11, sizeof (buf)); + str = fgets (buf, 100, f); + if (str != NULL) + { + FAIL ("returned %s instead of NULL", hex (str, strlen (str))); + } + else if (bytes_read != 0) + { + FAIL ("%d bytes read instead of %d", bytes_read, 0); + } + else + PASS (); + fclose (f); + + printf ("testing zero size buffer..."); + f = my_open ("hello\n", 6, "r"); + memset (buf, 0x11, sizeof (buf)); + str = fgets (buf, 0, f); + if (str != NULL) + { + FAIL ("returned %s instead of NULL", hex (str, strlen (str))); + } + else if (bytes_read != 0) + { + FAIL ("%d bytes read instead of %d", bytes_read, 0); + } + else + PASS (); + fclose (f); + + printf ("testing NULL buffer..."); + f = my_open ("hello\n", 0, "r"); + memset (buf, 0x11, sizeof (buf)); + + DIAG_PUSH_NEEDS_COMMENT; + /* We're intentionally passing an invalid size here. */ + DIAG_IGNORE_NEEDS_COMMENT (7, "-Wnonnull"); + str = fgets (NULL, 100, f); + DIAG_POP_NEEDS_COMMENT; + + if (str != NULL) + { + FAIL ("returned %s instead of NULL", hex (str, strlen (str))); + } + else if (bytes_read != 0) + { + FAIL ("%d bytes read instead of %d", bytes_read, 0); + } + else + PASS (); + fclose (f); + + printf ("testing embedded NUL..."); + f = my_open ("hel\0lo\n", 7, "r"); + memset (buf, 0x11, sizeof (buf)); + str = fgets (buf, 100, f); + if (str == NULL) + { + FAIL ("returned NULL"); + } + else if (memcmp (str, "hel\0lo\n\0", 8) != 0) + { + FAIL ("returned %s instead of %s", hex (str, 8), hex ("hel\0lo\n\0", 8)); + } + else if (bytes_read != 7) + { + FAIL ("%d bytes read instead of %d", bytes_read, 7); + } + else + PASS (); + fclose (f); + + printf ("testing writable stream..."); + f = my_open ("hel\0lo\n", 7, "w"); + memset (buf, 0x11, sizeof (buf)); + str = fgets (buf, 100, f); + if (str != NULL) + { + FAIL ("returned %s instead of NULL", hex (str, strlen (str))); + } + else if (bytes_read != 0) + { + FAIL ("%d bytes read instead of %d", bytes_read, 0); + } + else + PASS (); + fclose (f); + + printf ("testing closed fd stream..."); + int fd = open ("/dev/null", O_RDONLY); + f = fdopen (fd, "r"); + close (fd); + memset (buf, 0x11, sizeof (buf)); + str = fgets (buf, 100, f); + if (str != NULL) + { + FAIL ("returned %s instead of NULL", hex (str, strlen (str))); + } + else if (bytes_read != 0) + { + FAIL ("%d bytes read instead of %d", bytes_read, 0); + } + else + PASS (); + fclose (f); + +#ifdef IO_DEBUG + /* These tests only pass if glibc is built with -DIO_DEBUG. */ + + printf ("testing NULL descriptor..."); + memset (buf, 0x11, sizeof (buf)); + str = fgets (buf, 100, NULL); + if (str != NULL) + { + FAIL ("returned %s instead of NULL", hex (str, strlen (str))); + } + else if (bytes_read != 0) + { + FAIL ("%d bytes read instead of %d", bytes_read, 0); + } + else + PASS (); + + printf ("testing closed descriptor..."); + f = my_open ("hello\n", 7, "r"); + fclose (f); + memset (buf, 0x11, sizeof (buf)); + str = fgets (buf, 100, f); + if (str != NULL) + { + FAIL ("returned %s instead of NULL", hex (str, strlen (str))); + } + else if (bytes_read != 0) + { + FAIL ("%d bytes read instead of %d", bytes_read, 0); + } + else + PASS (); +#endif + + return 0; +} + +#include