This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Avoid strtok for thread safety


On Tue, Nov 26, 2002 at 01:24:20AM +0100, Paolo Carlini wrote:
> This is what I have prepared following Nathan's advice. Notice that
> we don't use std::string here (Benjamin told me privately that
> there is a long story behind that...), therefore strpbrk turned out
> to be more practical than strcspn.
> 
> What else can I add? In fact, no strdup, no autoconf, a final result
> only very slightly longer. Great!
> 
> Tested x86-linux. Ok?

strpbrk() returns NULL if it doesn't find any of the characters
searched for.  What happens then?

Nathan Myers
ncm-nospam@cantrip.org
p.s. sorry to be such a pest.
 
> /////////

> 2002-11-25  Paolo Carlini  <pcarlini@unitus.it>
> 	    Nathan Myers  <ncm@cantrip.org>
> 
> 	* src/localename.cc
> 	(locale::_Impl::_Impl(const char*, size_t)):
> 	Avoid strtok for thread safety.

> *** localename.cc.~1.31.~	Fri Oct 11 09:09:36 2002
> --- localename.cc	Tue Nov 26 01:06:54 2002
> *************** namespace std
> *** 141,149 ****
>         }
>   
>       // Name all the categories.
>       if (!strchr(__s, ';'))
>         {
> - 	size_t __len = strlen(__s) + 1;
>   	for (size_t __i = 0; 
>   	     __i < _S_categories_size + _S_extra_categories_size; ++__i)
>   	  {
> --- 141,149 ----
>         }
>   
>       // Name all the categories.
> +     size_t __len = strlen(__s) + 1;
>       if (!strchr(__s, ';'))
>         {
>   	for (size_t __i = 0; 
>   	     __i < _S_categories_size + _S_extra_categories_size; ++__i)
>   	  {
> *************** namespace std
> *** 152,178 ****
>   	  }
>         }
>       else
> !       {
> ! 	char* __tmp = strdup(__s);
> ! 	__tmp[strlen(__tmp)] = ';';
> ! 	strtok(__tmp, "=;");
>   	for (size_t __i = 0; 
>   	     __i < _S_categories_size + _S_extra_categories_size - 1; ++__i)
>   	  {
> ! 	    char* __src = strtok(NULL, "=;");
> ! 	    char* __new = new char[strlen(__src) + 1];
> ! 	    strcpy(__new, __src);
>   	    _M_names[__i] = __new;
> ! 	    strtok(NULL, "=;");
>   	  }
> ! 	char* __src = strtok(NULL, "=;");
> ! 	char* __new = new char[strlen(__src) + 1];
> ! 	strcpy(__new, __src);
>   	_M_names[_S_categories_size + _S_extra_categories_size - 1] = __new;
> - 
> - 	free(__tmp);
>         }
> !       
>       // Construct all standard facets and add them to _M_facets.  
>       _M_init_facet(new std::ctype<char>(__cloc, 0, false));
>       _M_init_facet(new codecvt<char, char, mbstate_t>(__cloc));
> --- 152,179 ----
>   	  }
>         }
>       else
> !      {
> ! 	char* __new;
> ! 	const char* __save = __s;
> ! 	char* __next = strpbrk(__save, "=;");
> ! 	__save = __next + 1;
>   	for (size_t __i = 0; 
>   	     __i < _S_categories_size + _S_extra_categories_size - 1; ++__i)
>   	  {
> ! 	    __next = strpbrk(__save, "=;");
> ! 	    __new = new char[__next - __save + 1];
> ! 	    memcpy(__new, __save, __next - __save);
> ! 	    __new[__next - __save] = '\0';
>   	    _M_names[__i] = __new;
> ! 	    __save = __next + 1;
> ! 	    __next = strpbrk(__save, "=;");
> ! 	    __save = __next + 1;
>   	  }
> ! 	__new = new char[__s + __len - __save];
> ! 	memcpy(__new, __save, __s + __len - __save);
>   	_M_names[_S_categories_size + _S_extra_categories_size - 1] = __new;
>         }
> ! 
>       // Construct all standard facets and add them to _M_facets.  
>       _M_init_facet(new std::ctype<char>(__cloc, 0, false));
>       _M_init_facet(new codecvt<char, char, mbstate_t>(__cloc));


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]