From patchwork Tue Aug 6 22:53:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 1969716 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=jEHzj6R9; 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 4WdpXZ4s7lz1ybS for ; Wed, 7 Aug 2024 08:54:38 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5CC7B385700F for ; Tue, 6 Aug 2024 22:54:36 +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 B14F43858C56 for ; Tue, 6 Aug 2024 22:54:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B14F43858C56 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 B14F43858C56 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=1722984859; cv=none; b=dislCkGVcdO0ATZZPU++O79+xywRhgac7EjoZw+yc7d+GDXONLEW1L+hxqcptkyCs1bu9RBe8dcEGTvE6L6sipLX7axNKOBbS0XOsNKaz9h9/46IMT9RT2U0DY2Dxo6RuMA7gOxClQfKCQ53zcoWAkoHKXhkbqKh51Sj641mPdY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722984859; c=relaxed/simple; bh=3WMpMuhr3yNYkSd9SMnpe22bj3ibUjT+JV5xlzXohUI=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=FrSPYZy8pSpFrfQXjl4KD4t3dbwv/+HUD1YXPKoMnNBHwY1xtWyY0HS2qVu1kJU+4iSnzs9wJahg3aCATiLmCahNDcTomLupG2pAX19vRvza6yJk8Uu5DcwmyMEMuFq8ol8NEw65ltYPgMEsd4urbG0zF/snFiCuX+1+tTBmPgM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722984856; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=hN9lXkyj629Cc4CtCoUJMb+wiuUJ6B2ogo+aztKGBuY=; b=jEHzj6R9iQgRCSwekoxdNx8jHZyYOe1Z7HX0jHzrjcYHVYRDcfio/Ldl7iO8ooJAYfv2ja Ms/At/pqFNa5rVtgcqSoMiqwZE3QpfDLdjfIMwKbf/HTlTDYSd5WWc2ejLCe+F27BvnSCh d+wpUvrp3CByTJmA09YLhYdQ8GrY7Vo= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-423-naiwZOU6OYihTRXCCRhWCQ-1; Tue, 06 Aug 2024 18:54:15 -0400 X-MC-Unique: naiwZOU6OYihTRXCCRhWCQ-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-42808685ef0so6680435e9.2 for ; Tue, 06 Aug 2024 15:54:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722984853; x=1723589653; h=mime-version:message-id:subject:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=hN9lXkyj629Cc4CtCoUJMb+wiuUJ6B2ogo+aztKGBuY=; b=OuHAt3uOVoT6SfD3wv0hy/KUSoSHKK1F8dVg3la2KosSDq1R0eB3YxgBzDsyT/Z1tW iF6H+VlvMP0nd2JCg1VoY0VHjS161Se6lCi5Pv7fau5cp6uTuwolnah8yL8n10uunyLJ SZxcregRvyvsG9a4h8gUXzHrebn9YJ7cG07M3PnEKkcNniiTF2HIpFH3i34+7eUeDP8g pFOtGNqzAioraJbBSBqeslddFL5n4ACuc9Tsz+JkqyDRPflKDl1F7uV6JfiRXC1JQsws Fgwl0SXsgWNNAyVC1QMjMh2r1aAqtEKip1kVju3s6U6sdYhIm8eKgtWvA1bNtGL5I3vR Xitg== X-Gm-Message-State: AOJu0YwHumyJB/+3hLhtaVI+LF+uukNfS4HzHf7xhfGD0yHswJk7pynu e2q+p35X8uxzLW0CybFhXF/Gr6Eht6Jz/Fe5TQFloPolI9x8TBMqXfNBIsq8z6PjnkxYlvUzaiz Cvkm6vRjBG53L4B+ik9ACz6aFhhQzawPs1WpUDZ7rmFYLULblNsfI1THZjYjREhVbxvwCmX4bal gocvDuaP1harG6jrEudMXOsd8zcxW4bFDJZl7ouAfdnA== X-Received: by 2002:a05:600c:3c99:b0:428:1310:b6b5 with SMTP id 5b1f17b1804b1-428e6b93acfmr117242475e9.34.1722984853316; Tue, 06 Aug 2024 15:54:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHvpedF2CvCoELZ3soJbdvR3X2Jw2YPLbPCnjAvN9ATOAMmt7Gm0KXs8OYvCiEo6f//sh40zA== X-Received: by 2002:a05:600c:3c99:b0:428:1310:b6b5 with SMTP id 5b1f17b1804b1-428e6b93acfmr117242255e9.34.1722984852450; Tue, 06 Aug 2024 15:54:12 -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-429059e0082sm874845e9.48.2024.08.06.15.54.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Aug 2024 15:54:12 -0700 (PDT) Received: from jsm28 (helo=localhost) by digraph.polyomino.org.uk with local-esmtp (Exim 4.95) (envelope-from ) id 1sbT3j-00CQLA-Jg for libc-alpha@sourceware.org; Tue, 06 Aug 2024 22:53:39 +0000 Date: Tue, 6 Aug 2024 22:53:39 +0000 (UTC) From: Joseph Myers To: libc-alpha@sourceware.org Subject: Add more tests of getline Message-ID: <8dc9cb-fdf9-f0da-6e3c-87a9b91d17bd@redhat.com> 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 There is very little test coverage for getline (only a minimal stdio-common/tstgetln.c which doesn't verify anything about the results of the getline calls). Add some more thorough tests (generally using fopencookie for convenience in testing various cases for what the input and possible errors / EOF in the file read might look like). Note the following regarding testing of error cases: * Nothing is said in the specifications about what if anything might be written into the buffer, and whether it might be reallocated, in error cases. The expectation of the tests (required to avoid memory leaks on error) is that at least on error cases, the invariant that lineptr points to at least n bytes is maintained. * The optional EOVERFLOW error case specified in POSIX, "The number of bytes to be written into the buffer, including the delimiter character (if encountered), would exceed {SSIZE_MAX}.", doesn't seem practically testable, as any case reading so many characters (half the address space) would also be liable to run into allocation failure along (ENOMEM) the way. * If a read error occurs part way through reading an input line, it seems unclear whether a partial line should be returned by getline (avoid input getting lost), which is what glibc does at least in the fopencookie case used in this test, or whether getline should return -1 (error) (so avoiding the program misbehaving by processing a truncated line as if it were complete). (There was a short, inconclusive discussion about this on the Austin Group list on 9-10 November 2014.) * The POSIX specification of getline inherits errors from fgetc. I didn't try to cover fgetc errors systematically, just one example of such an error. Tested for x86_64 and x86. diff --git a/stdio-common/Makefile b/stdio-common/Makefile index e4f0146d2c..d16b6dc8bc 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -218,6 +218,8 @@ tests := \ tst-fphex-wide \ tst-fseek \ tst-fwrite \ + tst-getline \ + tst-getline-enomem \ tst-gets \ tst-grouping \ tst-grouping2 \ @@ -320,6 +322,8 @@ tests-special += \ # tests-special generated += \ + tst-getline-enomem.mtrace \ + tst-getline.mtrace \ tst-printf-bz18872-mem.out \ tst-printf-bz18872.c \ tst-printf-bz18872.mtrace \ @@ -424,6 +428,12 @@ tst-printf-fp-leak-ENV = \ tst-scanf-bz27650-ENV = \ MALLOC_TRACE=$(objpfx)tst-scanf-bz27650.mtrace \ LD_PRELOAD=$(common-objpfx)malloc/libc_malloc_debug.so +tst-scanf-getline-ENV = \ + MALLOC_TRACE=$(objpfx)tst-getline.mtrace \ + LD_PRELOAD=$(common-objpfx)malloc/libc_malloc_debug.so +tst-scanf-getline-enomem-ENV = \ + MALLOC_TRACE=$(objpfx)tst-getline-enomem.mtrace \ + LD_PRELOAD=$(common-objpfx)malloc/libc_malloc_debug.so $(objpfx)tst-unbputc.out: tst-unbputc.sh $(objpfx)tst-unbputc $(SHELL) $< $(common-objpfx) '$(test-program-prefix)'; \ diff --git a/stdio-common/tst-getline-enomem.c b/stdio-common/tst-getline-enomem.c new file mode 100644 index 0000000000..7fc70ea9b5 --- /dev/null +++ b/stdio-common/tst-getline-enomem.c @@ -0,0 +1,78 @@ +/* Test getline: ENOMEM on allocation failure. + 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 + +/* Produce a stream of test data based on data in COOKIE (ignored), + storing up to SIZE bytes in BUF. */ + +static ssize_t +io_read (void *cookie, char *buf, size_t size) +{ + memset (buf, 'x', size); + return size; +} + +/* Set up a test stream with fopencookie. */ + +static FILE * +open_test_stream (void) +{ + static cookie_io_functions_t io_funcs = { .read = io_read }; + static int cookie; + FILE *fp = fopencookie (&cookie, "r", io_funcs); + TEST_VERIFY_EXIT (fp != NULL); + return fp; +} + +int +do_test (void) +{ + FILE *fp; + char *lineptr = NULL; + size_t size = 0; + ssize_t ret; + mtrace (); + /* Test ENOMEM (and error indicator for stream set) for memory + allocation failure. */ + verbose_printf ("Testing memory allocation failure\n"); + fp = open_test_stream (); + struct rlimit limit; + TEST_VERIFY_EXIT (getrlimit (RLIMIT_AS, &limit) == 0); + limit.rlim_cur = 32 * 1024 * 1024; + TEST_VERIFY_EXIT (setrlimit (RLIMIT_AS, &limit) == 0); + errno = 0; + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, -1); + TEST_COMPARE (errno, ENOMEM); + TEST_COMPARE (!!ferror (fp), 1); + TEST_COMPARE (feof (fp), 0); + free (lineptr); + fclose (fp); + return 0; +} + +#include diff --git a/stdio-common/tst-getline.c b/stdio-common/tst-getline.c new file mode 100644 index 0000000000..dd530e0204 --- /dev/null +++ b/stdio-common/tst-getline.c @@ -0,0 +1,387 @@ +/* Test getline. + 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 + +static struct test_data +{ + /* Input test data for fopencookie stream. */ + const char *in_data; + + /* The amount of test data left. */ + size_t in_data_left; + + /* Error number for forcing an error on next read. */ + int in_error; + + /* Error number for forcing an error (rather than EOF) after all + bytes read. */ + int in_error_after; +} the_cookie; + +/* Produce a stream of test data based on data in COOKIE, storing up + to SIZE bytes in BUF. */ + +static ssize_t +io_read (void *cookie, char *buf, size_t size) +{ + struct test_data *p = cookie; + if (p->in_error) + { + errno = p->in_error; + return -1; + } + if (size > p->in_data_left) + size = p->in_data_left; + memcpy (buf, p->in_data, size); + p->in_data += size; + p->in_data_left -= size; + if (p->in_data_left == 0) + p->in_error = p->in_error_after; + return size; +} + +/* Set up a test stream with fopencookie. */ + +static FILE * +open_test_stream (const char *in_data, size_t size) +{ + static cookie_io_functions_t io_funcs = { .read = io_read }; + the_cookie.in_data = in_data; + the_cookie.in_data_left = size; + the_cookie.in_error = 0; + the_cookie.in_error_after = 0; + FILE *fp = fopencookie (&the_cookie, "r", io_funcs); + TEST_VERIFY_EXIT (fp != NULL); + return fp; +} + +/* Set up a test stream with fopencookie, using data from a string + literal. */ +#define OPEN_TEST_STREAM(IN_DATA) open_test_stream (IN_DATA, sizeof (IN_DATA)) + +int +do_test (void) +{ + FILE *fp; + char *lineptr = NULL; + size_t size = 0; + ssize_t ret; + mtrace (); + /* Test failure with EINVAL (and error indicator for stream set) if + lineptr is a null pointer. */ + verbose_printf ("Testing lineptr == NULL\n"); + fp = OPEN_TEST_STREAM ("test"); + errno = 0; + ret = getline (NULL, &size, fp); + TEST_COMPARE (ret, -1); + TEST_COMPARE (errno, EINVAL); + TEST_COMPARE (!!ferror (fp), 1); + TEST_COMPARE (feof (fp), 0); + fclose (fp); + /* Test failure with EINVAL (and error indicator for stream set) if + n is a null pointer. */ + verbose_printf ("Testing n == NULL\n"); + fp = OPEN_TEST_STREAM ("test"); + errno = 0; + ret = getline (&lineptr, NULL, fp); + TEST_COMPARE (ret, -1); + TEST_COMPARE (errno, EINVAL); + TEST_COMPARE (!!ferror (fp), 1); + TEST_COMPARE (feof (fp), 0); + fclose (fp); + /* Test failure with EINVAL (and error indicator for stream set) if + both lineptr and n are null pointers. */ + verbose_printf ("Testing lineptr == NULL and n == NULL\n"); + fp = OPEN_TEST_STREAM ("test"); + errno = 0; + ret = getline (NULL, NULL, fp); + TEST_COMPARE (ret, -1); + TEST_COMPARE (errno, EINVAL); + TEST_COMPARE (!!ferror (fp), 1); + TEST_COMPARE (feof (fp), 0); + fclose (fp); + /* Test normal line, fitting in available space (including case with + null bytes). */ + verbose_printf ("Testing normal nonempty input\n"); + lineptr = xmalloc (10); + size = 10; + fp = OPEN_TEST_STREAM ("foo\nbar\0\n\0baz\nte\0st\n"); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 4); + TEST_COMPARE_BLOB (lineptr, 5, "foo\n", 5); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 5); + TEST_COMPARE_BLOB (lineptr, 6, "bar\0\n", 6); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 5); + TEST_COMPARE_BLOB (lineptr, 6, "\0baz\n", 6); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 6); + TEST_COMPARE_BLOB (lineptr, 7, "te\0st\n", 7); + fclose (fp); + /* Test normal line, with reallocation (including case with null bytes). */ + verbose_printf ("Testing normal nonempty input with reallocation\n"); + free (lineptr); + lineptr = NULL; + size = 0; + fp = OPEN_TEST_STREAM ("foo\nbar\0\n\0baz\nte\0st\n" + "foo\nbar\0\n\0baz\nte\0st\n"); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 4); + TEST_COMPARE_BLOB (lineptr, 5, "foo\n", 5); + free (lineptr); + lineptr = NULL; + size = 0; + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 5); + TEST_COMPARE_BLOB (lineptr, 6, "bar\0\n", 6); + free (lineptr); + lineptr = NULL; + size = 0; + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 5); + TEST_COMPARE_BLOB (lineptr, 6, "\0baz\n", 6); + free (lineptr); + lineptr = NULL; + size = 0; + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 6); + TEST_COMPARE_BLOB (lineptr, 7, "te\0st\n", 7); + free (lineptr); + lineptr = xmalloc (1); + size = 1; + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 4); + TEST_COMPARE_BLOB (lineptr, 5, "foo\n", 5); + free (lineptr); + lineptr = xmalloc (1); + size = 1; + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 5); + TEST_COMPARE_BLOB (lineptr, 6, "bar\0\n", 6); + free (lineptr); + lineptr = xmalloc (1); + size = 1; + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 5); + TEST_COMPARE_BLOB (lineptr, 6, "\0baz\n", 6); + free (lineptr); + lineptr = xmalloc (1); + size = 1; + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 6); + TEST_COMPARE_BLOB (lineptr, 7, "te\0st\n", 7); + fclose (fp); + /* Test EOF before delimiter but after some bytes read, fitting in + available space (including case with null bytes). */ + verbose_printf ("Testing EOF before delimiter\n"); + free (lineptr); + lineptr = xmalloc (10); + size = 10; + fp = open_test_stream ("foo", 3); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 3); + TEST_COMPARE_BLOB (lineptr, 4, "foo", 4); + fclose (fp); + free (lineptr); + lineptr = xmalloc (10); + size = 10; + fp = open_test_stream ("bar\0", 4); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 4); + TEST_COMPARE_BLOB (lineptr, 5, "bar\0", 5); + fclose (fp); + free (lineptr); + lineptr = xmalloc (10); + size = 10; + fp = open_test_stream ("\0baz", 4); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 4); + TEST_COMPARE_BLOB (lineptr, 5, "\0baz", 5); + fclose (fp); + free (lineptr); + lineptr = xmalloc (10); + size = 10; + fp = open_test_stream ("te\0st", 5); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 5); + TEST_COMPARE_BLOB (lineptr, 6, "te\0st", 6); + fclose (fp); + /* Test EOF before delimiter but after some bytes read, with + reallocation (including case with null bytes). */ + verbose_printf ("Testing EOF before delimiter with reallocation\n"); + free (lineptr); + lineptr = NULL; + size = 0; + fp = open_test_stream ("foo", 3); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 3); + TEST_COMPARE_BLOB (lineptr, 4, "foo", 4); + fclose (fp); + free (lineptr); + lineptr = NULL; + size = 0; + fp = open_test_stream ("bar\0", 4); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 4); + TEST_COMPARE_BLOB (lineptr, 5, "bar\0", 5); + fclose (fp); + free (lineptr); + lineptr = NULL; + size = 0; + fp = open_test_stream ("\0baz", 4); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 4); + TEST_COMPARE_BLOB (lineptr, 5, "\0baz", 5); + fclose (fp); + free (lineptr); + lineptr = NULL; + size = 0; + fp = open_test_stream ("te\0st", 5); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 5); + TEST_COMPARE_BLOB (lineptr, 6, "te\0st", 6); + fclose (fp); + free (lineptr); + lineptr = xmalloc (1); + size = 1; + fp = open_test_stream ("foo", 3); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 3); + TEST_COMPARE_BLOB (lineptr, 4, "foo", 4); + fclose (fp); + free (lineptr); + lineptr = xmalloc (1); + size = 1; + fp = open_test_stream ("bar\0", 4); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 4); + TEST_COMPARE_BLOB (lineptr, 5, "bar\0", 5); + fclose (fp); + free (lineptr); + lineptr = xmalloc (1); + size = 1; + fp = open_test_stream ("\0baz", 4); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 4); + TEST_COMPARE_BLOB (lineptr, 5, "\0baz", 5); + fclose (fp); + free (lineptr); + lineptr = xmalloc (1); + size = 1; + fp = open_test_stream ("te\0st", 5); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 5); + TEST_COMPARE_BLOB (lineptr, 6, "te\0st", 6); + fclose (fp); + /* Test EOF with no bytes read (nothing is specified about anything + written to the buffer), including EOF again when already at end + of file. */ + verbose_printf ("Testing EOF with no bytes read\n"); + free (lineptr); + lineptr = NULL; + size = 0; + fp = open_test_stream ("", 0); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, -1); + TEST_COMPARE (ferror (fp), 0); + TEST_COMPARE (!!feof (fp), 1); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, -1); + TEST_COMPARE (ferror (fp), 0); + TEST_COMPARE (!!feof (fp), 1); + fclose (fp); + free (lineptr); + lineptr = xmalloc (1); + size = 1; + fp = open_test_stream ("", 0); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, -1); + TEST_COMPARE (ferror (fp), 0); + TEST_COMPARE (!!feof (fp), 1); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, -1); + TEST_COMPARE (ferror (fp), 0); + TEST_COMPARE (!!feof (fp), 1); + fclose (fp); + /* Test error occurring with no bytes read, including calling + getline again while the file is in error state. */ + verbose_printf ("Testing error with no bytes read\n"); + free (lineptr); + lineptr = NULL; + size = 0; + fp = open_test_stream ("", 0); + the_cookie.in_error = EINVAL; + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, -1); + TEST_COMPARE (errno, EINVAL); + TEST_COMPARE (!!ferror (fp), 1); + TEST_COMPARE (feof (fp), 0); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, -1); + TEST_COMPARE (errno, EINVAL); + TEST_COMPARE (!!ferror (fp), 1); + TEST_COMPARE (feof (fp), 0); + fclose (fp); + /* Test error occurring after some bytes read. Specifications are + ambiguous here; at least in the fopencookie case used for + testing, glibc returns the partial line (but with the error + indicator on the stream set). */ + verbose_printf ("Testing error after some bytes read\n"); + free (lineptr); + lineptr = NULL; + size = 0; + fp = open_test_stream ("foo", 3); + the_cookie.in_error_after = EINVAL; + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, 3); + TEST_COMPARE_BLOB (lineptr, 4, "foo", 4); + TEST_COMPARE (errno, EINVAL); + TEST_COMPARE (!!ferror (fp), 1); + TEST_COMPARE (feof (fp), 0); + fclose (fp); + /* Test EBADF error as a representative example of an fgetc error + resulting in an error from getline. We don't try to cover all + error cases for fgetc here. */ + free (lineptr); + lineptr = NULL; + size = 0; + fp = xfopen ("/dev/null", "r"); + xclose (fileno (fp)); + ret = getline (&lineptr, &size, fp); + TEST_COMPARE (ret, -1); + TEST_COMPARE (errno, EBADF); + TEST_COMPARE (!!ferror (fp), 1); + TEST_COMPARE (feof (fp), 0); + fclose (fp); + free (lineptr); + return 0; +} + +#include