[07/11] grp: Implement fgetgrent_r using __nss_fgetent_r

Message ID 87552a25db51905c292fa203e99c2184969e1c52.1594974444.git.fweimer@redhat.com
State Committed
Headers
Series Fix fgetsgent_r data corruption bug (20338) |

Commit Message

Florian Weimer July 17, 2020, 8:30 a.m. UTC
  ---
 grp/fgetgrent_r.c | 54 ++++++-----------------------------------------
 1 file changed, 6 insertions(+), 48 deletions(-)
  

Comments

Carlos O'Donell July 21, 2020, 3:28 a.m. UTC | #1
On 7/17/20 4:30 AM, Florian Weimer via Libc-alpha wrote:

OK for 2.32. Further cleanup of implmenetations using __nss_fgetent_r.

Tested-by: Carlos O'Donell <carlos@redhat.com>
Reviewed-by: Carlos O'Donell <carlos@redhat.com>

> ---
>  grp/fgetgrent_r.c | 54 ++++++-----------------------------------------
>  1 file changed, 6 insertions(+), 48 deletions(-)
> 
> diff --git a/grp/fgetgrent_r.c b/grp/fgetgrent_r.c
> index 03daf4f295..b598584830 100644
> --- a/grp/fgetgrent_r.c
> +++ b/grp/fgetgrent_r.c
> @@ -20,10 +20,6 @@
>  #include <grp.h>
>  #include <stdio.h>
>  
> -#include <libio/iolibio.h>
> -#define flockfile(s) _IO_flockfile (s)
> -#define funlockfile(s) _IO_funlockfile (s)
> -
>  /* Define a line parsing function using the common code
>     used in the nss_files module.  */
>  
> @@ -59,49 +55,11 @@ int
>  __fgetgrent_r (FILE *stream, struct group *resbuf, char *buffer, size_t buflen,
>  	       struct group **result)
>  {
> -  char *p;
> -  int parse_result;
> -
> -  flockfile (stream);
> -  do
> -    {
> -      buffer[buflen - 1] = '\xff';
> -      p = fgets_unlocked (buffer, buflen, stream);
> -      if (__builtin_expect (p == NULL, 0) && feof_unlocked (stream))
> -	{
> -	  funlockfile (stream);
> -	  *result = NULL;
> -	  __set_errno (ENOENT);
> -	  return errno;
> -	}
> -      if (__builtin_expect (p == NULL, 0) || buffer[buflen - 1] != '\xff')
> -	{
> -	  funlockfile (stream);
> -	  *result = NULL;
> -	  __set_errno (ERANGE);
> -	  return errno;
> -	}
> -
> -      /* Skip leading blanks.  */
> -      while (isspace (*p))
> -	++p;
> -    } while (*p == '\0' || *p == '#'	/* Ignore empty and comment lines.  */
> -	     /* Parse the line.  If it is invalid, loop to
> -		get the next line of the file to parse.  */
> -	     || ! (parse_result = parse_line (p, resbuf,
> -					      (void *) buffer, buflen,
> -					      &errno)));
> -
> -  funlockfile (stream);
> -
> -  if (__builtin_expect (parse_result, 0) == -1)
> -    {
> -      /* The parser ran out of space.  */
> -      *result = NULL;
> -      return errno;
> -    }
> -
> -  *result = resbuf;
> -  return 0;
> +  int ret = __nss_fgetent_r (stream, resbuf, buffer, buflen, parse_line);
> +  if (ret == 0)
> +    *result = resbuf;
> +  else
> +    *result = NULL;
> +  return ret;
>  }
>  weak_alias (__fgetgrent_r, fgetgrent_r)
>
  

Patch

diff --git a/grp/fgetgrent_r.c b/grp/fgetgrent_r.c
index 03daf4f295..b598584830 100644
--- a/grp/fgetgrent_r.c
+++ b/grp/fgetgrent_r.c
@@ -20,10 +20,6 @@ 
 #include <grp.h>
 #include <stdio.h>
 
-#include <libio/iolibio.h>
-#define flockfile(s) _IO_flockfile (s)
-#define funlockfile(s) _IO_funlockfile (s)
-
 /* Define a line parsing function using the common code
    used in the nss_files module.  */
 
@@ -59,49 +55,11 @@  int
 __fgetgrent_r (FILE *stream, struct group *resbuf, char *buffer, size_t buflen,
 	       struct group **result)
 {
-  char *p;
-  int parse_result;
-
-  flockfile (stream);
-  do
-    {
-      buffer[buflen - 1] = '\xff';
-      p = fgets_unlocked (buffer, buflen, stream);
-      if (__builtin_expect (p == NULL, 0) && feof_unlocked (stream))
-	{
-	  funlockfile (stream);
-	  *result = NULL;
-	  __set_errno (ENOENT);
-	  return errno;
-	}
-      if (__builtin_expect (p == NULL, 0) || buffer[buflen - 1] != '\xff')
-	{
-	  funlockfile (stream);
-	  *result = NULL;
-	  __set_errno (ERANGE);
-	  return errno;
-	}
-
-      /* Skip leading blanks.  */
-      while (isspace (*p))
-	++p;
-    } while (*p == '\0' || *p == '#'	/* Ignore empty and comment lines.  */
-	     /* Parse the line.  If it is invalid, loop to
-		get the next line of the file to parse.  */
-	     || ! (parse_result = parse_line (p, resbuf,
-					      (void *) buffer, buflen,
-					      &errno)));
-
-  funlockfile (stream);
-
-  if (__builtin_expect (parse_result, 0) == -1)
-    {
-      /* The parser ran out of space.  */
-      *result = NULL;
-      return errno;
-    }
-
-  *result = resbuf;
-  return 0;
+  int ret = __nss_fgetent_r (stream, resbuf, buffer, buflen, parse_line);
+  if (ret == 0)
+    *result = resbuf;
+  else
+    *result = NULL;
+  return ret;
 }
 weak_alias (__fgetgrent_r, fgetgrent_r)