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