getdelim: ensure error indicator is set on error (bug 29917)

Message ID mvmzgbjpoll.fsf@suse.de
State Superseded
Headers
Series getdelim: ensure error indicator is set on error (bug 29917) |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Andreas Schwab Dec. 19, 2022, 12:25 p.m. UTC
  POSIX requires that getdelim and getline set the error indicator on the
stream when an error occured, in addition to setting errno.
---
 libio/iogetdelim.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
  

Comments

Carlos O'Donell Dec. 19, 2022, 2:59 p.m. UTC | #1
On 12/19/22 07:25, Andreas Schwab via Libc-alpha wrote:
> POSIX requires that getdelim and getline set the error indicator on the
> stream when an error occured, in addition to setting errno.

May we please write a test case that validates this requirement?

We don't need to test all the error scenarios, but at least one
of them to validate would be helpful.

Thanks.

> ---
>  libio/iogetdelim.c | 16 +++++++++++-----
>  1 file changed, 11 insertions(+), 5 deletions(-)
> 
> diff --git a/libio/iogetdelim.c b/libio/iogetdelim.c
> index b6c4c07b45..591526e9c1 100644
> --- a/libio/iogetdelim.c
> +++ b/libio/iogetdelim.c
> @@ -43,11 +43,6 @@ __getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
>    ssize_t cur_len = 0;
>    ssize_t len;
>  
> -  if (lineptr == NULL || n == NULL)
> -    {
> -      __set_errno (EINVAL);
> -      return -1;
> -    }
>    CHECK_FILE (fp, -1);
>    _IO_acquire_lock (fp);
>    if (_IO_ferror_unlocked (fp))
> @@ -56,12 +51,21 @@ __getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
>        goto unlock_return;
>      }
>  
> +  if (lineptr == NULL || n == NULL)
> +    {
> +      __set_errno (EINVAL);
> +      fseterr_unlocked (fp);
> +      result = -1;
> +      goto unlock_return;
> +    }
> +
>    if (*lineptr == NULL || *n == 0)
>      {
>        *n = 120;
>        *lineptr = (char *) malloc (*n);
>        if (*lineptr == NULL)
>  	{
> +	  fseterr_unlocked (fp);
>  	  result = -1;
>  	  goto unlock_return;
>  	}
> @@ -88,6 +92,7 @@ __getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
>        if (__glibc_unlikely (len >= SSIZE_MAX - cur_len))
>  	{
>  	  __set_errno (EOVERFLOW);
> +	  fseterr_unlocked (fp);
>  	  result = -1;
>  	  goto unlock_return;
>  	}
> @@ -102,6 +107,7 @@ __getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
>  	  new_lineptr = (char *) realloc (*lineptr, needed);
>  	  if (new_lineptr == NULL)
>  	    {
> +	      fseterr_unlocked (fp);
>  	      result = -1;
>  	      goto unlock_return;
>  	    }
  

Patch

diff --git a/libio/iogetdelim.c b/libio/iogetdelim.c
index b6c4c07b45..591526e9c1 100644
--- a/libio/iogetdelim.c
+++ b/libio/iogetdelim.c
@@ -43,11 +43,6 @@  __getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
   ssize_t cur_len = 0;
   ssize_t len;
 
-  if (lineptr == NULL || n == NULL)
-    {
-      __set_errno (EINVAL);
-      return -1;
-    }
   CHECK_FILE (fp, -1);
   _IO_acquire_lock (fp);
   if (_IO_ferror_unlocked (fp))
@@ -56,12 +51,21 @@  __getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
       goto unlock_return;
     }
 
+  if (lineptr == NULL || n == NULL)
+    {
+      __set_errno (EINVAL);
+      fseterr_unlocked (fp);
+      result = -1;
+      goto unlock_return;
+    }
+
   if (*lineptr == NULL || *n == 0)
     {
       *n = 120;
       *lineptr = (char *) malloc (*n);
       if (*lineptr == NULL)
 	{
+	  fseterr_unlocked (fp);
 	  result = -1;
 	  goto unlock_return;
 	}
@@ -88,6 +92,7 @@  __getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
       if (__glibc_unlikely (len >= SSIZE_MAX - cur_len))
 	{
 	  __set_errno (EOVERFLOW);
+	  fseterr_unlocked (fp);
 	  result = -1;
 	  goto unlock_return;
 	}
@@ -102,6 +107,7 @@  __getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
 	  new_lineptr = (char *) realloc (*lineptr, needed);
 	  if (new_lineptr == NULL)
 	    {
+	      fseterr_unlocked (fp);
 	      result = -1;
 	      goto unlock_return;
 	    }