{"id":808733,"url":"http://patchwork.ozlabs.org/api/patches/808733/?format=json","web_url":"http://patchwork.ozlabs.org/project/glibc/patch/20170901154353.52A27439942E3@oldenburg.str.redhat.com/","project":{"id":41,"url":"http://patchwork.ozlabs.org/api/projects/41/?format=json","name":"GNU C Library","link_name":"glibc","list_id":"libc-alpha.sourceware.org","list_email":"libc-alpha@sourceware.org","web_url":"","scm_url":"","webscm_url":"","list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20170901154353.52A27439942E3@oldenburg.str.redhat.com>","list_archive_url":null,"date":"2017-09-01T15:43:53","name":"nss_files: Fix re-reading of long lines [BZ #18991]","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"114347971490ba33b7a24f15a7b2535cd54c96cb","submitter":{"id":14312,"url":"http://patchwork.ozlabs.org/api/people/14312/?format=json","name":"Florian Weimer","email":"fweimer@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/glibc/patch/20170901154353.52A27439942E3@oldenburg.str.redhat.com/mbox/","series":[{"id":1052,"url":"http://patchwork.ozlabs.org/api/series/1052/?format=json","web_url":"http://patchwork.ozlabs.org/project/glibc/list/?series=1052","date":"2017-09-01T15:43:53","name":"nss_files: Fix re-reading of long lines [BZ #18991]","version":1,"mbox":"http://patchwork.ozlabs.org/series/1052/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/808733/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/808733/checks/","tags":{},"related":[],"headers":{"Return-Path":"<libc-alpha-return-84026-incoming=patchwork.ozlabs.org@sourceware.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":["patchwork-incoming@bilbo.ozlabs.org","mailing list libc-alpha@sourceware.org"],"Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=sourceware.org\n\t(client-ip=209.132.180.131; helo=sourceware.org;\n\tenvelope-from=libc-alpha-return-84026-incoming=patchwork.ozlabs.org@sourceware.org;\n\treceiver=<UNKNOWN>)","ozlabs.org; dkim=pass (1024-bit key;\n\tsecure) header.d=sourceware.org header.i=@sourceware.org\n\theader.b=\"EsmRTuXg\"; dkim-atps=neutral","sourceware.org; auth=none","ext-mx10.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx10.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=fweimer@redhat.com"],"Received":["from sourceware.org (server1.sourceware.org [209.132.180.131])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256\n\tbits)) (No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xkNny1jbCz9t1t\n\tfor <incoming@patchwork.ozlabs.org>;\n\tSat,  2 Sep 2017 01:44:06 +1000 (AEST)","(qmail 116911 invoked by alias); 1 Sep 2017 15:43:58 -0000","(qmail 116901 invoked by uid 89); 1 Sep 2017 15:43:57 -0000"],"DomainKey-Signature":"a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id\n\t:list-unsubscribe:list-subscribe:list-archive:list-post\n\t:list-help:sender:date:to:subject:mime-version:content-type\n\t:content-transfer-encoding:message-id:from; q=dns; s=default; b=\n\tFZl1vX2C1OWIFyj6Vk8rUqugQbsmwTrBcIspZP+jYEHuQrOAhoxlNcp5Dc/iwCWl\n\tREl8yQp7d8WIXPFf64CBRNrDeQQxU5G/eSoJzF6tTWcriAW7JIMc79yhOFT6jMNX\n\tdcWyOMxwx7rbxdTgYcgvKQFVLmROZoQPDZuXcf7U3+A=","DKIM-Signature":"v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id\n\t:list-unsubscribe:list-subscribe:list-archive:list-post\n\t:list-help:sender:date:to:subject:mime-version:content-type\n\t:content-transfer-encoding:message-id:from; s=default; bh=G+X9n6\n\tiYESdWUyHG2EtKDWLSniU=; b=EsmRTuXgoJL+XCmUtUY2OZGtwK9AzwvU6JgWij\n\tQuf7xqRiIuEb0eJHsc7cOSbVJbuIKrjrpRiDOfRklOM04jLZKX7FgkgL/JJG4YaK\n\tNDngc08AhTUQ/3z1er/BM0FUmwwAZA/NZZfkQpll9/K+tqA4/knAzLfiGQEVn40P\n\t37QmU=","Mailing-List":"contact libc-alpha-help@sourceware.org; run by ezmlm","Precedence":"bulk","List-Id":"<libc-alpha.sourceware.org>","List-Unsubscribe":"<mailto:libc-alpha-unsubscribe-incoming=patchwork.ozlabs.org@sourceware.org>","List-Subscribe":"<mailto:libc-alpha-subscribe@sourceware.org>","List-Archive":"<http://sourceware.org/ml/libc-alpha/>","List-Post":"<mailto:libc-alpha@sourceware.org>","List-Help":"<mailto:libc-alpha-help@sourceware.org>,\n\t<http://sourceware.org/ml/#faqs>","Sender":"libc-alpha-owner@sourceware.org","X-Virus-Found":"No","X-Spam-SWARE-Status":"No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0,\n\tGIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RP_MATCHES_RCVD,\n\tSPF_HELO_PASS autolearn=ham version=3.3.2 spammy=192021, 12000","X-HELO":"mx1.redhat.com","DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 663A16148A","Date":"Fri, 01 Sep 2017 17:43:53 +0200","To":"libc-alpha@sourceware.org","Subject":"[PATCH] nss_files: Fix re-reading of long lines [BZ #18991]","User-Agent":"Heirloom mailx 12.5 7/5/10","MIME-Version":"1.0","Content-Type":"text/plain; charset=us-ascii","Content-Transfer-Encoding":"7bit","Message-Id":"<20170901154353.52A27439942E3@oldenburg.str.redhat.com>","From":"fweimer@redhat.com (Florian Weimer)"},"content":"Use the new __libc_readline_unlocked function to pick up\nreading at the same line in case the buffer needs to be enlarged.\n\n2017-09-01  Florian Weimer  <fweimer@redhat.com>\n\n\t[BZ #18991]\n\t* nss/nss_files/files-XXX.c (internal_getent): Use\n\t__libc_readline_unlocked.\n\t(get_contents_ret, get_contents): Remove.\n\t* nss/tst-nss-files-hosts-getent.c: New file.\n\t* nss/Makefile (tests): Add tst-nss-files-hosts-getent.\n\t(tst-nss-files-hosts-getent): Link with -ldl.","diff":"diff --git a/nss/Makefile b/nss/Makefile\nindex c9a5200f96..ac16e48d43 100644\n--- a/nss/Makefile\n+++ b/nss/Makefile\n@@ -63,6 +63,7 @@ xtests\t\t\t= bug-erange\n # Tests which need libdl\n ifeq (yes,$(build-shared))\n tests += tst-nss-files-hosts-erange\n+tests += tst-nss-files-hosts-getent\n endif\n \n # If we have a thread library then we can test cancellation against\n@@ -163,3 +164,4 @@ $(objpfx)tst-cancel-getpwuid_r: $(shared-thread-library)\n endif\n \n $(objpfx)tst-nss-files-hosts-erange: $(libdl)\n+$(objpfx)tst-nss-files-hosts-getent: $(libdl)\ndiff --git a/nss/nss_files/files-XXX.c b/nss/nss_files/files-XXX.c\nindex 265331ef21..8b912aef1d 100644\n--- a/nss/nss_files/files-XXX.c\n+++ b/nss/nss_files/files-XXX.c\n@@ -128,51 +128,6 @@ CONCAT(_nss_files_end,ENTNAME) (void)\n }\n \f\n \n-typedef enum\n-{\n-  gcr_ok = 0,\n-  gcr_error = -1,\n-  gcr_overflow = -2\n-} get_contents_ret;\n-\n-/* Hack around the fact that fgets only accepts int sizes.  */\n-static get_contents_ret\n-get_contents (char *linebuf, size_t len, FILE *stream)\n-{\n-  size_t remaining_len = len;\n-  char *curbuf = linebuf;\n-\n-  do\n-    {\n-      int curlen = ((remaining_len > (size_t) INT_MAX) ? INT_MAX\n-\t\t    : remaining_len);\n-\n-      /* Terminate the line so that we can test for overflow.  */\n-      ((unsigned char *) curbuf)[curlen - 1] = 0xff;\n-\n-      char *p = fgets_unlocked (curbuf, curlen, stream);\n-\n-      /* EOF or read error.  */\n-      if (p == NULL)\n-        return gcr_error;\n-\n-      /* Done reading in the line.  */\n-      if (((unsigned char *) curbuf)[curlen - 1] == 0xff)\n-        return gcr_ok;\n-\n-      /* Drop the terminating '\\0'.  */\n-      remaining_len -= curlen - 1;\n-      curbuf += curlen - 1;\n-    }\n-  /* fgets copies one less than the input length.  Our last iteration is of\n-     REMAINING_LEN and once that is done, REMAINING_LEN is decremented by\n-     REMAINING_LEN - 1, leaving the result as 1.  */\n-  while (remaining_len > 1);\n-\n-  /* This means that the current buffer was not large enough.  */\n-  return gcr_overflow;\n-}\n-\n /* Parsing the database file into `struct STRUCTURE' data structures.  */\n static enum nss_status\n internal_getent (FILE *stream, struct STRUCTURE *result,\n@@ -193,24 +148,26 @@ internal_getent (FILE *stream, struct STRUCTURE *result,\n \n   do\n     {\n-      get_contents_ret r = get_contents (data->linebuffer, linebuflen, stream);\n-\n-      if (r == gcr_error)\n+      ssize_t r = __libc_readline_unlocked\n+\t(stream, data->linebuffer, linebuflen);\n+      if (r < 0)\n \t{\n-\t  /* End of file or read error.  */\n+\t  *errnop = errno;\n+\t  H_ERRNO_SET (NETDB_INTERNAL);\n+\t  if (*errnop == ERANGE)\n+\t    /* Request larger buffer.  */\n+\t    return NSS_STATUS_TRYAGAIN;\n+\t  else\n+\t    /* Other read failure.  */\n+\t    return NSS_STATUS_UNAVAIL;\n+\t}\n+      else if (r == 0)\n+\t{\n+\t  /* End of file.  */\n \t  H_ERRNO_SET (HOST_NOT_FOUND);\n \t  return NSS_STATUS_NOTFOUND;\n \t}\n \n-      if (r == gcr_overflow)\n-\t{\n-\t  /* The line is too long.  Give the user the opportunity to\n-\t     enlarge the buffer.  */\n-\t  *errnop = ERANGE;\n-\t  H_ERRNO_SET (NETDB_INTERNAL);\n-\t  return NSS_STATUS_TRYAGAIN;\n-\t}\n-\n       /* Everything OK.  Now skip leading blanks.  */\n       p = data->linebuffer;\n       while (isspace (*p))\ndiff --git a/nss/tst-nss-files-hosts-getent.c b/nss/tst-nss-files-hosts-getent.c\nnew file mode 100644\nindex 0000000000..7ffdad40b8\n--- /dev/null\n+++ b/nss/tst-nss-files-hosts-getent.c\n@@ -0,0 +1,223 @@\n+/* Enumerate /etc/hosts with a long line (bug 18991).\n+   Copyright (C) 2017 Free Software Foundation, Inc.\n+   This file is part of the GNU C Library.\n+\n+   The GNU C Library is free software; you can redistribute it and/or\n+   modify it under the terms of the GNU Lesser General Public\n+   License as published by the Free Software Foundation; either\n+   version 2.1 of the License, or (at your option) any later version.\n+\n+   The GNU C Library is distributed in the hope that it will be useful,\n+   but WITHOUT ANY WARRANTY; without even the implied warranty of\n+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n+   Lesser General Public License for more details.\n+\n+   You should have received a copy of the GNU Lesser General Public\n+   License along with the GNU C Library; if not, see\n+   <http://www.gnu.org/licenses/>.  */\n+\n+\n+#include <dlfcn.h>\n+#include <errno.h>\n+#include <gnu/lib-names.h>\n+#include <netdb.h>\n+#include <nss.h>\n+#include <stdlib.h>\n+#include <stdio.h>\n+#include <support/check.h>\n+#include <support/check_nss.h>\n+#include <support/namespace.h>\n+#include <support/support.h>\n+#include <support/test-driver.h>\n+#include <support/xmemstream.h>\n+#include <support/xunistd.h>\n+\n+struct support_chroot *chroot_env;\n+\n+/* Number of alias names in the long line.  This results in an overall\n+   line length of around 12000 bytes.  */\n+enum { name_count = 600 };\n+\n+const char *host1_expected =\n+  \"name: localhost\\n\"\n+  \"alias: localhost.localdomain\\n\"\n+  \"address: 127.0.0.1\\n\";\n+const char *host2_expected =\n+  \"name: host2.example.com\\n\"\n+  \"address: 192.0.2.2\\n\";\n+const char *host4_expected =\n+  \"name: www.example.com\\n\"\n+  \"address: 192.0.2.80\\n\";\n+const char *host5_expected =\n+  \"name: host5.example.com\\n\"\n+  \"address: 192.0.2.5\\n\";\n+const char *host6_expected =\n+  \"name: www1.example.com\\n\"\n+  \"address: 192.0.2.81\\n\";\n+\n+static void\n+prepare (int argc, char **argv)\n+{\n+  struct xmemstream hosts;\n+  xopen_memstream (&hosts);\n+  fputs (\"127.0.0.1   localhost localhost.localdomain\\n\", hosts.out);\n+  fputs (\"192.0.2.2 host2.example.com\\n\", hosts.out);\n+  fputs (\"192.0.2.1\", hosts.out);\n+  for (int i = 0; i < name_count; ++i)\n+    fprintf (hosts.out, \" host%d.example.com\", i);\n+  fputs (\"\\n192.0.2.80 www.example.com\\n\"\n+         \"192.0.2.5 host5.example.com\\n\"\n+         \"192.0.2.81 www1.example.com\\n\", hosts.out);\n+  xfclose_memstream (&hosts);\n+\n+  chroot_env = support_chroot_create\n+    ((struct support_chroot_configuration)\n+     {\n+       .resolv_conf = \"\",\n+       .hosts = hosts.buffer,\n+       .host_conf = \"multi on\\n\",\n+     });\n+\n+  free (hosts.buffer);\n+}\n+\n+/* If -1, no sethostent call.  Otherwise, pass do_stayopen as the\n+   sethostent argument.  */\n+static int do_stayopen;\n+\n+/* If non-zero, perform an endostent call.  */\n+static int do_endent;\n+\n+static void\n+subprocess_getent (void *closure)\n+{\n+  xchroot (chroot_env->path_chroot);\n+\n+  errno = 0;\n+  if (do_stayopen >= 0)\n+    sethostent (do_stayopen);\n+  TEST_VERIFY (errno == 0);\n+\n+  int i = 0;\n+  while (true)\n+    {\n+      struct xmemstream expected;\n+      xopen_memstream (&expected);\n+      switch (++i)\n+        {\n+        case 1:\n+          fputs (host1_expected, expected.out);\n+          break;\n+        case 2:\n+          fputs (host2_expected, expected.out);\n+          break;\n+        case 3:\n+          fputs (\"name: host0.example.com\\n\", expected.out);\n+          for (int j = 1; j < name_count; ++j)\n+            fprintf (expected.out, \"alias: host%d.example.com\\n\", j);\n+          fputs (\"address: 192.0.2.1\\n\", expected.out);\n+          break;\n+        case 4:\n+          fputs (host4_expected, expected.out);\n+          break;\n+        case 5:\n+          fputs (host5_expected, expected.out);\n+          break;\n+        case 6:\n+          fputs (host6_expected, expected.out);\n+          break;\n+        default:\n+          fprintf (expected.out, \"*** unexpected host %d ***\\n\", i);\n+          break;\n+        }\n+      xfclose_memstream (&expected);\n+      char *context = xasprintf (\"do_stayopen=%d host=%d\", do_stayopen, i);\n+\n+      errno = 0;\n+      struct hostent *e = gethostent ();\n+      if (e == NULL)\n+        {\n+          TEST_VERIFY (errno == 0);\n+          break;\n+        }\n+      check_hostent (context, e, expected.buffer);\n+      free (context);\n+      free (expected.buffer);\n+    }\n+\n+  errno = 0;\n+  if (do_endent)\n+    endhostent ();\n+  TEST_VERIFY (errno == 0);\n+\n+  /* Exercise process termination.   */\n+  exit (0);\n+}\n+\n+static void\n+subprocess_gethost (void *closure)\n+{\n+  xchroot (chroot_env->path_chroot);\n+\n+  /* This tests enlarging the read buffer in the multi case.  */\n+  struct xmemstream expected;\n+  xopen_memstream (&expected);\n+  fputs (\"name: host2.example.com\\n\", expected.out);\n+  for (int j = 1; j < name_count; ++j)\n+    /* NB: host2 is duplicated in the alias list.  */\n+    fprintf (expected.out, \"alias: host%d.example.com\\n\", j);\n+  fputs (\"alias: host0.example.com\\n\"\n+         \"address: 192.0.2.2\\n\"\n+         \"address: 192.0.2.1\\n\",\n+         expected.out);\n+  xfclose_memstream (&expected);\n+  check_hostent (\"host2.example.com\",\n+                 gethostbyname (\"host2.example.com\"),\n+                 expected.buffer);\n+  free (expected.buffer);\n+\n+  /* Similarly, but with a different order in the /etc/hosts file.  */\n+  xopen_memstream (&expected);\n+  fputs (\"name: host0.example.com\\n\", expected.out);\n+  for (int j = 1; j < name_count; ++j)\n+    fprintf (expected.out, \"alias: host%d.example.com\\n\", j);\n+  /* NB: host5 is duplicated in the alias list.  */\n+  fputs (\"alias: host5.example.com\\n\"\n+         \"address: 192.0.2.1\\n\"\n+         \"address: 192.0.2.5\\n\",\n+         expected.out);\n+  xfclose_memstream (&expected);\n+  check_hostent (\"host5.example.com\",\n+                 gethostbyname (\"host5.example.com\"),\n+                 expected.buffer);\n+  free (expected.buffer);\n+}\n+\n+static int\n+do_test (void)\n+{\n+  support_become_root ();\n+  if (!support_can_chroot ())\n+    return EXIT_UNSUPPORTED;\n+\n+  __nss_configure_lookup (\"hosts\", \"files\");\n+  if (dlopen (LIBNSS_FILES_SO, RTLD_LAZY) == NULL)\n+    FAIL_EXIT1 (\"could not load \" LIBNSS_DNS_SO \": %s\", dlerror ());\n+\n+  for (do_stayopen = -1; do_stayopen < 2; ++do_stayopen)\n+    for (do_endent = 0; do_endent < 2; ++do_endent)\n+      {\n+        if (test_verbose > 0)\n+          printf (\"info: do_stayopen=%d do_endent=%d\\n\",\n+                  do_stayopen, do_endent);\n+        support_isolate_in_subprocess (subprocess_getent, NULL);\n+      }\n+\n+  support_isolate_in_subprocess (subprocess_gethost, NULL);\n+\n+  support_chroot_free (chroot_env);\n+  return 0;\n+}\n+\n+#define PREPARE prepare\n+#include <support/test-driver.c>\n","prefixes":[]}