8597553
commit 89f187a40fc0ad4e22838526bfe34d73f758b776
8597553
Author: Florian Weimer <fweimer@redhat.com>
8597553
Date:   Fri Jun 16 20:54:43 2017 +0200
8597553
8597553
    resolv: Use getline for configuration file reading in res_vinit_1
8597553
8597553
diff --git a/resolv/res_init.c b/resolv/res_init.c
8597553
index e604a0212fa13624..ed5a4d4804a792de 100644
8597553
--- a/resolv/res_init.c
8597553
+++ b/resolv/res_init.c
8597553
@@ -126,10 +126,10 @@ is_sort_mask (char ch)
8597553
    deallocation and error handling.  Return true on success, false on
8597553
    failure.  */
8597553
 static bool
8597553
-res_vinit_1 (res_state statp, bool preinit, FILE *fp)
8597553
+res_vinit_1 (res_state statp, bool preinit, FILE *fp, char **buffer)
8597553
 {
8597553
   char *cp, **pp;
8597553
-  char buf[BUFSIZ];
8597553
+  size_t buffer_size = 0;
8597553
   int nserv = 0;    /* Number of nameservers read from file.  */
8597553
   bool have_serv6 = false;
8597553
   bool haveenv = false;
8597553
@@ -197,27 +197,38 @@ res_vinit_1 (res_state statp, bool preinit, FILE *fp)
8597553
     }
8597553
 
8597553
 #define MATCH(line, name)                       \
8597553
-  (!strncmp (line, name, sizeof (name) - 1)     \
8597553
-   && (line[sizeof (name) - 1] == ' '           \
8597553
-       || line[sizeof (name) - 1] == '\t'))
8597553
+  (!strncmp ((line), name, sizeof (name) - 1)     \
8597553
+   && ((line)[sizeof (name) - 1] == ' '           \
8597553
+       || (line)[sizeof (name) - 1] == '\t'))
8597553
 
8597553
   if (fp != NULL)
8597553
     {
8597553
       /* No threads use this stream.  */
8597553
       __fsetlocking (fp, FSETLOCKING_BYCALLER);
8597553
       /* Read the config file.  */
8597553
-      while (__fgets_unlocked (buf, sizeof (buf), fp) != NULL)
8597553
+      while (true)
8597553
         {
8597553
+          {
8597553
+            ssize_t ret = __getline (buffer, &buffer_size, fp);
8597553
+            if (ret <= 0)
8597553
+              {
8597553
+                if (_IO_ferror_unlocked (fp))
8597553
+                  return false;
8597553
+                else
8597553
+                  break;
8597553
+              }
8597553
+          }
8597553
+
8597553
           /* Skip comments.  */
8597553
-          if (*buf == ';' || *buf == '#')
8597553
+          if (**buffer == ';' || **buffer == '#')
8597553
             continue;
8597553
           /* Read default domain name.  */
8597553
-          if (MATCH (buf, "domain"))
8597553
+          if (MATCH (*buffer, "domain"))
8597553
             {
8597553
               if (haveenv)
8597553
                 /* LOCALDOMAIN overrides the configuration file.  */
8597553
                 continue;
8597553
-              cp = buf + sizeof ("domain") - 1;
8597553
+              cp = *buffer + sizeof ("domain") - 1;
8597553
               while (*cp == ' ' || *cp == '\t')
8597553
                 cp++;
8597553
               if ((*cp == '\0') || (*cp == '\n'))
8597553
@@ -230,12 +241,12 @@ res_vinit_1 (res_state statp, bool preinit, FILE *fp)
8597553
               continue;
8597553
             }
8597553
           /* Set search list.  */
8597553
-          if (MATCH (buf, "search"))
8597553
+          if (MATCH (*buffer, "search"))
8597553
             {
8597553
               if (haveenv)
8597553
                 /* LOCALDOMAIN overrides the configuration file.  */
8597553
                 continue;
8597553
-              cp = buf + sizeof ("search") - 1;
8597553
+              cp = *buffer + sizeof ("search") - 1;
8597553
               while (*cp == ' ' || *cp == '\t')
8597553
                 cp++;
8597553
               if ((*cp == '\0') || (*cp == '\n'))
8597553
@@ -271,11 +282,11 @@ res_vinit_1 (res_state statp, bool preinit, FILE *fp)
8597553
               continue;
8597553
             }
8597553
           /* Read nameservers to query.  */
8597553
-          if (MATCH (buf, "nameserver") && nserv < MAXNS)
8597553
+          if (MATCH (*buffer, "nameserver") && nserv < MAXNS)
8597553
             {
8597553
               struct in_addr a;
8597553
 
8597553
-              cp = buf + sizeof ("nameserver") - 1;
8597553
+              cp = *buffer + sizeof ("nameserver") - 1;
8597553
               while (*cp == ' ' || *cp == '\t')
8597553
                 cp++;
8597553
               if ((*cp != '\0') && (*cp != '\n') && __inet_aton (cp, &a))
8597553
@@ -300,7 +311,7 @@ res_vinit_1 (res_state statp, bool preinit, FILE *fp)
8597553
 
8597553
                       sa6 = malloc (sizeof (*sa6));
8597553
                       if (sa6 == NULL)
8597553
-                        return -1;
8597553
+                        return false;
8597553
 
8597553
                       sa6->sin6_family = AF_INET6;
8597553
                       sa6->sin6_port = htons (NAMESERVER_PORT);
8597553
@@ -323,11 +334,11 @@ res_vinit_1 (res_state statp, bool preinit, FILE *fp)
8597553
                 }
8597553
               continue;
8597553
             }
8597553
-          if (MATCH (buf, "sortlist"))
8597553
+          if (MATCH (*buffer, "sortlist"))
8597553
             {
8597553
               struct in_addr a;
8597553
 
8597553
-              cp = buf + sizeof ("sortlist") - 1;
8597553
+              cp = *buffer + sizeof ("sortlist") - 1;
8597553
               while (nsort < MAXRESOLVSORT)
8597553
                 {
8597553
                   while (*cp == ' ' || *cp == '\t')
8597553
@@ -367,9 +378,9 @@ res_vinit_1 (res_state statp, bool preinit, FILE *fp)
8597553
                 }
8597553
               continue;
8597553
             }
8597553
-          if (MATCH (buf, "options"))
8597553
+          if (MATCH (*buffer, "options"))
8597553
             {
8597553
-              res_setoptions (statp, buf + sizeof ("options") - 1, "conf");
8597553
+              res_setoptions (statp, *buffer + sizeof ("options") - 1, "conf");
8597553
               continue;
8597553
             }
8597553
         }
8597553
@@ -387,10 +398,13 @@ res_vinit_1 (res_state statp, bool preinit, FILE *fp)
8597553
       statp->nsaddr.sin_port = htons (NAMESERVER_PORT);
8597553
       statp->nscount = 1;
8597553
     }
8597553
-  if (statp->defdname[0] == 0
8597553
-      && __gethostname (buf, sizeof (statp->defdname) - 1) == 0
8597553
-      && (cp = strchr (buf, '.')) != NULL)
8597553
-    strcpy (statp->defdname, cp + 1);
8597553
+  if (statp->defdname[0] == 0)
8597553
+    {
8597553
+      char buf[sizeof (statp->defdname)];
8597553
+      if (__gethostname (buf, sizeof (statp->defdname) - 1) == 0
8597553
+          && (cp = strchr (buf, '.')) != NULL)
8597553
+        strcpy (statp->defdname, cp + 1);
8597553
+    }
8597553
 
8597553
   /* Find components of local domain that might be searched.  */
8597553
   if (!havesearch)
8597553
@@ -404,7 +418,7 @@ res_vinit_1 (res_state statp, bool preinit, FILE *fp)
8597553
   if ((cp = getenv ("RES_OPTIONS")) != NULL)
8597553
     res_setoptions (statp, cp, "env");
8597553
   statp->options |= RES_INIT;
8597553
-  return 0;
8597553
+  return true;
8597553
 }
8597553
 
8597553
 /* Set up default settings.  If the /etc/resolv.conf configuration
8597553
@@ -434,7 +448,12 @@ __res_vinit (res_state statp, int preinit)
8597553
            need to be handled by the application.  */
8597553
         return -1;
8597553
       }
8597553
-  if (!res_vinit_1 (statp, preinit, fp))
8597553
+
8597553
+  char *buffer = NULL;
8597553
+  bool ok = res_vinit_1 (statp, preinit, fp, &buffer);
8597553
+  free (buffer);
8597553
+
8597553
+  if (!ok)
8597553
     {
8597553
       /* Deallocate the name server addresses which have been
8597553
          allocated.  */