This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC 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]

[v3] Avoid strtok "like the plague"


Hi,

tested x86-linux, approved by Benjamin.

Many thanks to Roland, Ulrich and Wolfgang.

Paolo.

/////////
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]