e354a5
From ec2f1fddf29053957d061dfe310f106388472a4f Mon Sep 17 00:00:00 2001
e354a5
From: Florian Weimer <fweimer@redhat.com>
e354a5
Date: Wed, 15 Jul 2020 12:37:01 +0200
e354a5
Subject: [PATCH 11/11] libio: Remove __libc_readline_unlocked
e354a5
e354a5
__nss_readline supersedes it.  This reverts part of commit
e354a5
3f5e3f5d066dcffb80af48ae2cf35a01a85a8f10 ("libio: Implement
e354a5
internal function __libc_readline_unlocked").  The internal
e354a5
aliases __fseeko64 and __ftello64 are preserved because
e354a5
they are needed by __nss_readline as well.
e354a5
e354a5
Tested-by: Carlos O'Donell <carlos@redhat.com>
e354a5
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
e354a5
---
e354a5
 include/stdio.h      |  13 ---
e354a5
 libio/Makefile       |   4 +-
e354a5
 libio/Versions       |   1 -
e354a5
 libio/readline.c     | 170 ------------------------------------
e354a5
 libio/tst-readline.c | 237 ---------------------------------------------------
e354a5
 5 files changed, 2 insertions(+), 423 deletions(-)
e354a5
 delete mode 100644 libio/readline.c
e354a5
 delete mode 100644 libio/tst-readline.c
e354a5
e354a5
diff -rupN a/include/stdio.h b/include/stdio.h
e354a5
--- a/include/stdio.h	2020-09-14 17:48:49.359699532 -0400
e354a5
+++ b/include/stdio.h	2020-09-14 19:20:51.204429471 -0400
e354a5
@@ -135,19 +135,6 @@ extern int __fxprintf (FILE *__fp, const
e354a5
 extern int __fxprintf_nocancel (FILE *__fp, const char *__fmt, ...)
e354a5
      __attribute__ ((__format__ (__printf__, 2, 3))) attribute_hidden;
e354a5
 
e354a5
-/* Read the next line from FP into BUFFER, of LENGTH bytes.  LINE will
e354a5
-   include the line terminator and a NUL terminator.  On success,
e354a5
-   return the length of the line, including the line terminator, but
e354a5
-   excluding the NUL termintor.  On EOF, return zero and write a NUL
e354a5
-   terminator.  On error, return -1 and set errno.  If the total byte
e354a5
-   count (line and both terminators) exceeds LENGTH, return -1 and set
e354a5
-   errno to ERANGE (but do not mark the stream as failed).
e354a5
-
e354a5
-   The behavior is undefined if FP is not seekable, or if the stream
e354a5
-   is already in an error state.  */
e354a5
-ssize_t __libc_readline_unlocked (FILE *fp, char *buffer, size_t length);
e354a5
-libc_hidden_proto (__libc_readline_unlocked);
e354a5
-
e354a5
 extern const char *const _sys_errlist_internal[] attribute_hidden;
e354a5
 extern int _sys_nerr_internal attribute_hidden;
e354a5
 
e354a5
diff -rupN a/libio/Makefile b/libio/Makefile
e354a5
--- a/libio/Makefile	2020-09-14 17:48:45.722562485 -0400
e354a5
+++ b/libio/Makefile	2020-09-14 19:20:51.211429735 -0400
e354a5
@@ -49,7 +49,7 @@ routines	:=							      \
e354a5
 	__fbufsize __freading __fwriting __freadable __fwritable __flbf	      \
e354a5
 	__fpurge __fpending __fsetlocking				      \
e354a5
 									      \
e354a5
-	libc_fatal fmemopen oldfmemopen vtables readline
e354a5
+	libc_fatal fmemopen oldfmemopen vtables
e354a5
 
e354a5
 tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc   \
e354a5
 	tst_wprintf2 tst-widetext test-fmemopen tst-ext tst-ext2 \
e354a5
@@ -67,7 +67,7 @@ tests = tst_swprintf tst_wprintf tst_sws
e354a5
 	tst-ftell-append tst-fputws tst-bz22415 tst-fgetc-after-eof \
e354a5
 	tst-wfile-sync
e354a5
 
e354a5
-tests-internal = tst-vtables tst-vtables-interposed tst-readline
e354a5
+tests-internal = tst-vtables tst-vtables-interposed
e354a5
 
e354a5
 ifeq (yes,$(build-shared))
e354a5
 # Add test-fopenloc only if shared library is enabled since it depends on
e354a5
diff -rupN a/libio/Versions b/libio/Versions
e354a5
--- a/libio/Versions	2018-08-01 01:10:47.000000000 -0400
e354a5
+++ b/libio/Versions	2020-09-14 19:20:51.217429962 -0400
e354a5
@@ -161,6 +161,5 @@ libc {
e354a5
 
e354a5
     __fseeko64;
e354a5
     __ftello64;
e354a5
-    __libc_readline_unlocked;
e354a5
   }
e354a5
 }
e354a5
diff -rupN a/libio/readline.c b/libio/readline.c
e354a5
--- a/libio/readline.c	2018-08-01 01:10:47.000000000 -0400
e354a5
+++ b/libio/readline.c	1969-12-31 19:00:00.000000000 -0500
e354a5
@@ -1,170 +0,0 @@
e354a5
-/* fgets with ERANGE error reporting and size_t buffer length.
e354a5
-   Copyright (C) 2018 Free Software Foundation, Inc.
e354a5
-   This file is part of the GNU C Library.
e354a5
-
e354a5
-   The GNU C Library is free software; you can redistribute it and/or
e354a5
-   modify it under the terms of the GNU Lesser General Public
e354a5
-   License as published by the Free Software Foundation; either
e354a5
-   version 2.1 of the License, or (at your option) any later version.
e354a5
-
e354a5
-   The GNU C Library is distributed in the hope that it will be useful,
e354a5
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
e354a5
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e354a5
-   Lesser General Public License for more details.
e354a5
-
e354a5
-   You should have received a copy of the GNU Lesser General Public
e354a5
-   License along with the GNU C Library; if not, see
e354a5
-   <http://www.gnu.org/licenses/>.  */
e354a5
-
e354a5
-#include <assert.h>
e354a5
-#include <errno.h>
e354a5
-#include <stdio.h>
e354a5
-#include <string.h>
e354a5
-
e354a5
-#include "libioP.h"
e354a5
-
e354a5
-/* Return -1 and set errno to EINVAL if it is ERANGE.  */
e354a5
-static ssize_t
e354a5
-fail_no_erange (void)
e354a5
-{
e354a5
-  if (errno == ERANGE)
e354a5
-    __set_errno (EINVAL);
e354a5
-  return -1;
e354a5
-}
e354a5
-
e354a5
-/* Slow path for reading the line.  Called with no data in the stream
e354a5
-   read buffer.  Write data to [BUFFER, BUFFER_END).  */
e354a5
-static ssize_t
e354a5
-readline_slow (FILE *fp, char *buffer, char *buffer_end)
e354a5
-{
e354a5
-  char *start = buffer;
e354a5
-
e354a5
-  while (buffer < buffer_end)
e354a5
-    {
e354a5
-      if (__underflow (fp) == EOF)
e354a5
-        {
e354a5
-          if (_IO_ferror_unlocked (fp))
e354a5
-            /* If the EOF was caused by a read error, report it.  */
e354a5
-            return fail_no_erange ();
e354a5
-          *buffer = '\0';
e354a5
-          /* Do not include the null terminator.  */
e354a5
-          return buffer - start;
e354a5
-        }
e354a5
-
e354a5
-      /* __underflow has filled the buffer.  */
e354a5
-      char *readptr = fp->_IO_read_ptr;
e354a5
-      ssize_t readlen = fp->_IO_read_end - readptr;
e354a5
-      /* Make sure that __underflow really has acquired some data.  */
e354a5
-      assert (readlen > 0);
e354a5
-      char *pnl = memchr (readptr, '\n', readlen);
e354a5
-      if (pnl != NULL)
e354a5
-        {
e354a5
-          /* We found the terminator.  */
e354a5
-          size_t line_length = pnl - readptr;
e354a5
-          if (line_length + 2 > buffer_end - buffer)
e354a5
-            /* Not enough room in the caller-supplied buffer.  */
e354a5
-            break;
e354a5
-          memcpy (buffer, readptr, line_length + 1);
e354a5
-          buffer[line_length + 1] = '\0';
e354a5
-          fp->_IO_read_ptr = pnl + 1;
e354a5
-          /* Do not include the null terminator.  */
e354a5
-          return buffer - start + line_length + 1;
e354a5
-        }
e354a5
-
e354a5
-      if (readlen >= buffer_end - buffer)
e354a5
-        /* Not enough room in the caller-supplied buffer.  */
e354a5
-        break;
e354a5
-
e354a5
-      /* Save and consume the stream buffer.  */
e354a5
-      memcpy (buffer, readptr, readlen);
e354a5
-      fp->_IO_read_ptr = fp->_IO_read_end;
e354a5
-      buffer += readlen;
e354a5
-    }
e354a5
-
e354a5
-  /* The line does not fit into the buffer.  */
e354a5
-  __set_errno (ERANGE);
e354a5
-  return -1;
e354a5
-}
e354a5
-
e354a5
-ssize_t
e354a5
-__libc_readline_unlocked (FILE *fp, char *buffer, size_t buffer_length)
e354a5
-{
e354a5
-  char *buffer_end = buffer + buffer_length;
e354a5
-
e354a5
-  /* Orient the stream.  */
e354a5
-  if (__builtin_expect (fp->_mode, -1) == 0)
e354a5
-    _IO_fwide (fp, -1);
e354a5
-
e354a5
-  /* Fast path: The line terminator is found in the buffer.  */
e354a5
-  char *readptr = fp->_IO_read_ptr;
e354a5
-  ssize_t readlen = fp->_IO_read_end - readptr;
e354a5
-  off64_t start_offset;         /* File offset before reading anything.  */
e354a5
-  if (readlen > 0)
e354a5
-    {
e354a5
-      char *pnl = memchr (readptr, '\n', readlen);
e354a5
-      if (pnl != NULL)
e354a5
-        {
e354a5
-          size_t line_length = pnl - readptr;
e354a5
-          /* Account for line and null terminators.  */
e354a5
-          if (line_length + 2 > buffer_length)
e354a5
-            {
e354a5
-              __set_errno (ERANGE);
e354a5
-              return -1;
e354a5
-            }
e354a5
-          memcpy (buffer, readptr, line_length + 1);
e354a5
-          buffer[line_length + 1] = '\0';
e354a5
-          /* Consume the entire line.  */
e354a5
-          fp->_IO_read_ptr = pnl + 1;
e354a5
-          return line_length + 1;
e354a5
-        }
e354a5
-
e354a5
-      /* If the buffer does not have enough space for what is pending
e354a5
-         in the stream (plus a NUL terminator), the buffer is too
e354a5
-         small.  */
e354a5
-      if (readlen + 1 > buffer_length)
e354a5
-        {
e354a5
-          __set_errno (ERANGE);
e354a5
-          return -1;
e354a5
-        }
e354a5
-
e354a5
-      /* End of line not found.  We need all the buffered data.  Fall
e354a5
-         through to the slow path.  */
e354a5
-      memcpy (buffer, readptr, readlen);
e354a5
-      buffer += readlen;
e354a5
-      /* The original length is invalid after this point.  Use
e354a5
-         buffer_end instead.  */
e354a5
-#pragma GCC poison buffer_length
e354a5
-      /* Read the old offset before updating the read pointer.  */
e354a5
-      start_offset = __ftello64 (fp);
e354a5
-      fp->_IO_read_ptr = fp->_IO_read_end;
e354a5
-    }
e354a5
-  else
e354a5
-    {
e354a5
-      readlen = 0;
e354a5
-      start_offset = __ftello64 (fp);
e354a5
-    }
e354a5
-
e354a5
-  /* Slow path: Read more data from the underlying file.  We need to
e354a5
-     restore the file pointer if the buffer is too small.  First,
e354a5
-     check if the __ftello64 call above failed.  */
e354a5
-  if (start_offset < 0)
e354a5
-    return fail_no_erange ();
e354a5
-
e354a5
-  ssize_t result = readline_slow (fp, buffer, buffer_end);
e354a5
-  if (result < 0)
e354a5
-    {
e354a5
-      if (errno == ERANGE)
e354a5
-        {
e354a5
-          /* Restore the file pointer so that the caller may read the
e354a5
-             same line again.  */
e354a5
-          if (__fseeko64 (fp, start_offset, SEEK_SET) < 0)
e354a5
-            return fail_no_erange ();
e354a5
-          __set_errno (ERANGE);
e354a5
-        }
e354a5
-      /* Do not restore the file position on other errors; it is
e354a5
-         likely that the __fseeko64 call would fail, too.  */
e354a5
-      return -1;
e354a5
-    }
e354a5
-  return readlen + result;
e354a5
-}
e354a5
-libc_hidden_def (__libc_readline_unlocked)
e354a5
diff -rupN a/libio/tst-readline.c b/libio/tst-readline.c
e354a5
--- a/libio/tst-readline.c	2020-09-14 17:48:41.969421067 -0400
e354a5
+++ b/libio/tst-readline.c	1969-12-31 19:00:00.000000000 -0500
e354a5
@@ -1,237 +0,0 @@
e354a5
-/* Test the __libc_readline_unlocked function.
e354a5
-   Copyright (C) 2018 Free Software Foundation, Inc.
e354a5
-   This file is part of the GNU C Library.
e354a5
-
e354a5
-   The GNU C Library is free software; you can redistribute it and/or
e354a5
-   modify it under the terms of the GNU Lesser General Public
e354a5
-   License as published by the Free Software Foundation; either
e354a5
-   version 2.1 of the License, or (at your option) any later version.
e354a5
-
e354a5
-   The GNU C Library is distributed in the hope that it will be useful,
e354a5
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
e354a5
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e354a5
-   Lesser General Public License for more details.
e354a5
-
e354a5
-   You should have received a copy of the GNU Lesser General Public
e354a5
-   License along with the GNU C Library; if not, see
e354a5
-   <http://www.gnu.org/licenses/>.  */
e354a5
-
e354a5
-/* Exercise __libc_readline_unlocked with various combinations of line
e354a5
-   lengths, stdio buffer sizes, and line read buffer sizes.  */
e354a5
-
e354a5
-#include <errno.h>
e354a5
-#include <stdbool.h>
e354a5
-#include <stdio.h>
e354a5
-#include <string.h>
e354a5
-#include <support/check.h>
e354a5
-#include <support/support.h>
e354a5
-#include <support/temp_file.h>
e354a5
-#include <support/test-driver.h>
e354a5
-#include <support/xmemstream.h>
e354a5
-#include <support/xstdio.h>
e354a5
-#include <support/xunistd.h>
e354a5
-
e354a5
-enum
e354a5
-  {
e354a5
-    maximum_line_length = 7,
e354a5
-    number_of_lines = 3,
e354a5
-  };
e354a5
-
e354a5
-/* -1: Do not set buffer size.  0: unbuffered.  Otherwise, use this as
e354a5
-   the size of the buffer.  */
e354a5
-static int buffer_size;
e354a5
-
e354a5
-/* These size of the buffer used for reading.  Must be at least 2.  */
e354a5
-static int read_size;
e354a5
-
e354a5
-/* If a read files with ERANGE, increase the buffer size by this
e354a5
-   amount.  Must be positive.  */
e354a5
-static int read_size_increment;
e354a5
-
e354a5
-/* If non-zero, do not reset the read size after an ERANGE error.  */
e354a5
-static int read_size_preserve;
e354a5
-
e354a5
-/* If non-zero, no '\n' at the end of the file.  */
e354a5
-static int no_newline_at_eof;
e354a5
-
e354a5
-/* Length of the line, or -1 if the line is not present.  */
e354a5
-static int line_lengths[number_of_lines];
e354a5
-
e354a5
-/* The name of the test file.  */
e354a5
-static char *test_file_path;
e354a5
-
e354a5
-/* The contents of the test file.  */
e354a5
-static char expected_contents[(maximum_line_length + 2) * number_of_lines + 1];
e354a5
-static size_t expected_length;
e354a5
-
e354a5
-/* Returns a random byte which is not zero or the line terminator.  */
e354a5
-static char
e354a5
-random_char (void)
e354a5
-{
e354a5
-  static unsigned int rand_state = 1;
e354a5
-  while (true)
e354a5
-    {
e354a5
-      char result = rand_r (&rand_state) >> 16;
e354a5
-      if (result != 0 && result != '\n')
e354a5
-        return result;
e354a5
-    }
e354a5
-}
e354a5
-
e354a5
-/* Create the test file.  */
e354a5
-static void
e354a5
-prepare (int argc, char **argv)
e354a5
-{
e354a5
-  int fd = create_temp_file ("tst-readline-", &test_file_path);
e354a5
-  TEST_VERIFY_EXIT (fd >= 0);
e354a5
-  xclose (fd);
e354a5
-}
e354a5
-
e354a5
-/* Prepare the test file.  Return false if the test parameters are
e354a5
-   incongruent and the test should be skipped.  */
e354a5
-static bool
e354a5
-write_test_file (void)
e354a5
-{
e354a5
-  expected_length = 0;
e354a5
-  char *p = expected_contents;
e354a5
-  for (int lineno = 0; lineno < number_of_lines; ++lineno)
e354a5
-    for (int i = 0; i < line_lengths[lineno]; ++i)
e354a5
-      *p++ = random_char ();
e354a5
-  expected_length = p - &expected_contents[0];
e354a5
-  if (no_newline_at_eof)
e354a5
-    {
e354a5
-      if (expected_length == 0)
e354a5
-        return false;
e354a5
-      --expected_length;
e354a5
-      --p;
e354a5
-    }
e354a5
-  if (test_verbose > 0)
e354a5
-    {
e354a5
-      printf ("info: writing test file of %zu bytes:\n", expected_length);
e354a5
-      for (int i = 0; i < number_of_lines; ++i)
e354a5
-        printf (" line %d: %d\n", i, line_lengths[i]);
e354a5
-      if (no_newline_at_eof)
e354a5
-        puts ("  (no newline at EOF)");
e354a5
-    }
e354a5
-  TEST_VERIFY_EXIT (expected_length < sizeof (expected_contents));
e354a5
-  *p++ = '\0';
e354a5
-  support_write_file_string (test_file_path, expected_contents);
e354a5
-  return true;
e354a5
-}
e354a5
-
e354a5
-/* Run a single test (a combination of a test file and read
e354a5
-   parameters).  */
e354a5
-static void
e354a5
-run_test (void)
e354a5
-{
e354a5
-  TEST_VERIFY_EXIT (read_size_increment > 0);
e354a5
-  if (test_verbose > 0)
e354a5
-    {
e354a5
-      printf ("info: running test: buffer_size=%d read_size=%d\n"
e354a5
-              "  read_size_increment=%d read_size_preserve=%d\n",
e354a5
-              buffer_size, read_size, read_size_increment, read_size_preserve);
e354a5
-    }
e354a5
-
e354a5
-  struct xmemstream result;
e354a5
-  xopen_memstream (&result);
e354a5
-
e354a5
-  FILE *fp = xfopen (test_file_path, "rce");
e354a5
-  char *fp_buffer = NULL;
e354a5
-  if (buffer_size == 0)
e354a5
-    TEST_VERIFY_EXIT (setvbuf (fp, NULL, _IONBF, 0) == 0);
e354a5
-  if (buffer_size > 0)
e354a5
-    {
e354a5
-      fp_buffer = xmalloc (buffer_size);
e354a5
-      TEST_VERIFY_EXIT (setvbuf (fp, fp_buffer, _IOFBF, buffer_size) == 0);
e354a5
-    }
e354a5
-
e354a5
-  char *line_buffer = xmalloc (read_size);
e354a5
-  size_t line_buffer_size = read_size;
e354a5
-
e354a5
-  while (true)
e354a5
-    {
e354a5
-      ssize_t ret = __libc_readline_unlocked
e354a5
-        (fp, line_buffer, line_buffer_size);
e354a5
-      if (ret < 0)
e354a5
-        {
e354a5
-          TEST_VERIFY (ret == -1);
e354a5
-          if (errno != ERANGE)
e354a5
-            FAIL_EXIT1 ("__libc_readline_unlocked: %m");
e354a5
-          line_buffer_size += read_size_increment;
e354a5
-          free (line_buffer);
e354a5
-          line_buffer = xmalloc (line_buffer_size);
e354a5
-          /* Try reading this line again.  */
e354a5
-        }
e354a5
-      else if (ret == 0)
e354a5
-        break;
e354a5
-      else
e354a5
-        {
e354a5
-          /* A line has been read.  Save it.  */
e354a5
-          TEST_VERIFY (ret == strlen (line_buffer));
e354a5
-          const char *pnl = strchr (line_buffer, '\n');
e354a5
-          /* If there is a \n, it must be at the end.  */
e354a5
-          TEST_VERIFY (pnl == NULL || pnl == line_buffer + ret - 1);
e354a5
-          fputs (line_buffer, result.out);
e354a5
-
e354a5
-          /* Restore the original read size if required.  */
e354a5
-          if (line_buffer_size > read_size && !read_size_preserve)
e354a5
-            {
e354a5
-              line_buffer_size = read_size;
e354a5
-              free (line_buffer);
e354a5
-              line_buffer = xmalloc (line_buffer_size);
e354a5
-            }
e354a5
-        }
e354a5
-    }
e354a5
-
e354a5
-  xfclose (fp);
e354a5
-  free (fp_buffer);
e354a5
-  free (line_buffer);
e354a5
-
e354a5
-  xfclose_memstream (&result);
e354a5
-  TEST_VERIFY (result.length == expected_length);
e354a5
-  TEST_VERIFY (strcmp (result.buffer, expected_contents) == 0);
e354a5
-  if (test_verbose > 0)
e354a5
-    {
e354a5
-      printf ("info: expected (%zu): [[%s]]\n",
e354a5
-              expected_length, expected_contents);
e354a5
-      printf ("info:   actual (%zu): [[%s]]\n", result.length, result.buffer);
e354a5
-    }
e354a5
-  free (result.buffer);
e354a5
-}
e354a5
-
e354a5
-/* Test one test file with multiple read parameters.  */
e354a5
-static void
e354a5
-test_one_file (void)
e354a5
-{
e354a5
-  for (buffer_size = -1; buffer_size <= maximum_line_length + 1; ++buffer_size)
e354a5
-    for (read_size = 2; read_size <= maximum_line_length + 2; ++read_size)
e354a5
-      for (read_size_increment = 1; read_size_increment <= 4;
e354a5
-           ++read_size_increment)
e354a5
-        for (read_size_preserve = 0; read_size_preserve < 2;
e354a5
-             ++read_size_preserve)
e354a5
-          run_test ();
e354a5
-}
e354a5
-
e354a5
-
e354a5
-static int
e354a5
-do_test (void)
e354a5
-{
e354a5
-  /* Set up the test file contents.  */
e354a5
-  for (line_lengths[0] = -1; line_lengths[0] <= maximum_line_length;
e354a5
-       ++line_lengths[0])
e354a5
-    for (line_lengths[1] = -1; line_lengths[1] <= maximum_line_length;
e354a5
-         ++line_lengths[1])
e354a5
-      for (line_lengths[2] = -1; line_lengths[2] <= maximum_line_length;
e354a5
-           ++line_lengths[2])
e354a5
-        for (no_newline_at_eof = 0; no_newline_at_eof < 2; ++no_newline_at_eof)
e354a5
-          {
e354a5
-            if (!write_test_file ())
e354a5
-              continue;
e354a5
-            test_one_file ();
e354a5
-          }
e354a5
-  free (test_file_path);
e354a5
-  return 0;
e354a5
-}
e354a5
-
e354a5
-#define TIMEOUT 100
e354a5
-#define PREPARE prepare
e354a5
-#include <support/test-driver.c>