[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
strsep bug
From: |
Bruno Haible |
Subject: |
strsep bug |
Date: |
Tue, 6 Feb 2007 01:46:55 +0100 |
User-agent: |
KMail/1.5.4 |
Hi Yoann,
The strsep replacement has a bug: when passed an empty set of delimiters,
according to the Linux and BSD manual page the token should be set to
the entire string, and *stringp should be set to NULL (so indicating there
are no more tokens). The implementation in gnulib sets *stringp to the end
of the string, so that strsep will repeatedly, infinitely, yield empty
tokens.
Here is a fix, together with an optimization of the length 1 case.
OK to apply?
2007-02-05 Bruno Haible <address@hidden>
* lib/strsep.c (strsep): Fix actions in case of no delimiters.
Optimize search in case of 1 delimiter.
*** lib/strsep.c 26 Jan 2007 22:16:55 -0000 1.5
--- lib/strsep.c 6 Feb 2007 00:40:38 -0000
***************
*** 29,47 ****
char *start = *stringp;
char *ptr;
! if (!start)
return NULL;
! if (!*delim)
! ptr = start + strlen (start);
else
{
! ptr = strpbrk (start, delim);
! if (!ptr)
! {
! *stringp = NULL;
! return start;
! }
}
*ptr = '\0';
--- 29,54 ----
char *start = *stringp;
char *ptr;
! if (start == NULL)
return NULL;
! /* Optimize the case of no delimiters. */
! if (delim[0] == '\0')
! {
! *stringp = NULL;
! return start;
! }
!
! /* Optimize the case of one delimiter. */
! if (delim[1] == '\0')
! ptr = strchr (start, delim[0]);
else
+ /* The general case. */
+ ptr = strpbrk (start, delim);
+ if (ptr == NULL)
{
! *stringp = NULL;
! return start;
}
*ptr = '\0';
- strsep bug,
Bruno Haible <=