Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » [FIXED] String::Replace(empty string,) => Out of memory!
[FIXED] String::Replace(empty string,) => Out of memory! [message #43074] Fri, 02 May 2014 16:16 Go to next message
omari is currently offline  omari
Messages: 264
Registered: March 2010
Experienced Member
Hi,

a call to
String::Replace(f, r)
crash if f.GetCount is equal to 0, with message:

Out of memory!
Request size : 905969755 B
U++ allocated memory: 442520 KB


I found that is because Replace use t_find (StringFind.hpp line 30):

template <int step> // Template parameter to be a constant
int t_find(const char *ptr, int slen, const char *p, int len, int from)


and t_find returns 0 if slen is equal to 0.

		else
		if(len == 1) {
			char p0 = p[0];
			while(s <= e) {
				if(*s == p0)
					return (int)(s - ptr);
				s += step;
			}
		}
		else
			return 0;   <---- (line 87)

if t_find return -1 instead of 0, it resolve the issu.

Regards
omari.



regards
omari.

[Updated on: Sat, 18 March 2017 16:05]

Report message to a moderator

Re: [BUG] String::Replace(empty string,) => Out of memory! [message #43075 is a reply to message #43074] Fri, 02 May 2014 18:27 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Well, this is an interesting issue. While obviously it should not crash, I somehow doubt that returning -1 is correct here.

More specifically, I believe that

String h("xxx");
h.Find("");

should return 0, because there IS empty substring at position 0 after all. In any case, such behaviour is consistent with e.g. STL (and I suspect all other libraries).

CONSOLE_APP_MAIN {
	std::string s = "xxxxxx";
	DDUMP(s.find(""));
}


Means, specifying empty string as string to be replaced is sort of illegal. Anyway, I have added a check to Replace. Thanks for pointing that out...

Mirek
Re: [BUG] String::Replace(empty string,) => Out of memory! [message #43076 is a reply to message #43075] Fri, 02 May 2014 19:03 Go to previous messageGo to next message
omari is currently offline  omari
Messages: 264
Registered: March 2010
Experienced Member
thanks Mirek, problem solved.

i am agree any string has the empty substring, but how many times?
- 1 at position 0
- or 1 at position 0 and 1 at the end
- or infinity?

if there is more than one, i think t_find can returns (from).

what do you think?



regards
omari.

[Updated on: Fri, 02 May 2014 19:19]

Report message to a moderator

Re: [BUG] String::Replace(empty string,) => Out of memory! [message #43079 is a reply to message #43076] Sat, 03 May 2014 15:52 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
omari wrote on Fri, 02 May 2014 19:03
thanks Mirek, problem solved.

i am agree any string has the empty substring, but how many times?
- 1 at position 0
- or 1 at position 0 and 1 at the end
- or infinity?

if there is more than one, i think t_find can returns (from).

what do you think?



Right! Sorry, somehow I forgot to account for "from".

Mirek
Previous Topic: Surprising behavior of CParser
Next Topic: [FEATURE REQUEST] bool AMap::HasKey(K key) ;
Goto Forum:
  


Current Time: Thu Apr 18 19:27:35 CEST 2024

Total time taken to generate the page: 0.01911 seconds