Re: copy_string will never terminate - recursive function problem

Peter Annema <[email protected]>
Newsgroups gmane.comp.mozilla.devel.xpcom
Organization Another Netscape Collabra Server User
Message-ID <[email protected]>
Hi Markus,

this isn't by design, but it is a bug in the design of 
nsDependentConcatenation. We've tried to squash it in time for Mozilla 1.0 but 
unfortunately didn't make it. Right now it means two things:

1) you can't concatenate a concatenation if we can't tell it's a concatenation 
by the string's type e.g.:

   // XXX won't work :-(
   function f(const nsAString& inStr) {
     mStr = inStr + mStr;
   }

   f(str1 + str2);

This doesn't work because by passing the concatenation (str1+str2) into 
function f you lose the type information, and the concatenation (mStr+inStr) 
inside function f can't properly deal with not knowing that inStr is also a 
concatenation.

   // Suggested work-around:
   nsAutoString myStr(str1 + str2);
   f(myStr);

2) you can't have a concatenation as the right hand side of another 
concatenation, e.g.:

   // XXX won't work :-(
   function f(const nsDependentString& inStr) {
     mStr = mStr + inStr;
   }

   f(str1 + str2);


As a rule of thumb, don't pass a concatenation into a function.
Note that we do it in places in Mozilla code where we know we can get away 
with it (for now).

Your problem is a variation of #1. Another way to work around your problem:

void test(int iteration, const nsDependentConcatenation& prefix)
{
     if(iteration>10) {
         nsAutoString result;
         result=prefix;
         return;
     }
     test(iteration+1, prefix+NS_LITERAL_STRING(".x"));
}
void test(int iteration, const nsAString& prefix)
{
     test(iteration+1, prefix+NS_LITERAL_STRING(".x"));
}
test(0, NS_LITERAL_STRING("x"));

Or skip the nsAString version of test and use this call:

test(0, NS_LITERAL_STRING("")+NS_LITERAL_STRING("x"));

Yeah, those are kinda ugly, but it'll save you a few copies along the way.

I hope this helps,

   jag

Markus Essl wrote:
> Hi!
> 
> I hope this is the right newsgroup, and I hope you can answer my question:
> 
> I have a problem with an nsDependentConnection when called recursivly:
> 
> (result contains x.x\0\0\0\0 - but sometimes the copy itererator won't 
> finish because bytes_copied is 0, and first!=last will never get true.. 
> (nsAlgorithm.h:91)
> 
> void test_fails(int iteration, const nsAString& prefix)
> {
>     if(iteration>10) {
>         nsAutoString result;
>         result=prefix;
>         return;
>     }
>     test_fails(iteration+1, prefix+NS_LITERAL_STRING(".x"));
> }
> 
> But this works: (result.mUStr contains "x.x.x.x.x.x.x.x.x.x.x")
> 
> void test(int iteration, const nsAString& prefix)
> {
>     if(iteration>10) {
>         nsAutoString result;
>         result=prefix;
>         return;
>     }
>     nsAutoString string;
>     string=prefix;
>     test(iteration+1, string+NS_LITERAL_STRING(".x"));
> }
> test(0, NS_LITERAL_STRING("x"));
> 
> 
> Is this by design (i don't hope so!) or is this a bug? I couldn't find 
> anything in bugzilla. BTW, I use the RC3 codebase.
> 
> bye,
> Markus
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.