[PATCH] Use memchr in _IO_new_file_xsputn

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Thu Jun 11 19:06:30 GMT 2026



On 11/06/26 14:42, Bernhard Rosenkränzer wrote:
> Use memchr instead of implementing a bytewise lookup, this should be

I think you meant memrchr here.

> slightly faster and more readable
> 
> Signed-off-by: Bernhard Rosenkränzer <bero@lindev.ch>
> ---
>  libio/fileops.c | 12 ++++--------
>  1 file changed, 4 insertions(+), 8 deletions(-)
> 
> diff --git a/libio/fileops.c b/libio/fileops.c
> index 9348d7c3a1..a614e7c528 100644
> --- a/libio/fileops.c
> +++ b/libio/fileops.c
> @@ -1290,15 +1290,11 @@ _IO_new_file_xsputn (FILE *f, const void *data, size_t n)
>        count = f->_IO_buf_end - f->_IO_write_ptr;
>        if (count >= n)
>  	{
> -	  const char *p;
> -	  for (p = s + n; p > s; )
> +	  const char *p = memrchr (s, '\n', n);

You should use __memrchr within glibc, otherwise you will see a elf/check-localplt
failure.

> +	  if (p != NULL)
>  	    {
> -	      if (*--p == '\n')
> -		{
> -		  count = p - s + 1;
> -		  must_flush = 1;
> -		  break;
> -		}
> + 	      count = p - s + 1;
> +	      must_flush = 1;
>  	    }
>  	}
>      }



More information about the Libc-alpha mailing list