Mozilla 2/Strings/Static Analysis: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
(Alright, let's try this wording.)
Line 19: Line 19:
nsresult GetAString(nsAString &result)
nsresult GetAString(nsAString &result)
{
{
   result.Assign("foo"); // this works... we would merely be returning a new
   result.Assign("foo"); // this is ok, it can be converted to return a new
                         // immutable value in the outparam
                         // immutable string
}
}


Line 27: Line 27:
   result.Append("foo"); // this won't work... it modifies the inout param so
   result.Append("foo"); // this won't work... it modifies the inout param so
                         // we would have to rewrite "result" to be
                         // we would have to rewrite "result" to be
                         // nsAStringBuilder&
                         // nsAStringBuilder&, or split it into two separate
                        // params, one in, one out.
}
}
</pre>
</pre>

Revision as of 13:55, 12 February 2008

How many strings exist *only* for conversion?

Procedure:

  • find calls to NS_ConvertUTF8toUTF16 and CopyUTF8toUTF16
  • check to see whether that string is modified after the conversion takes place

TODO: define "modification"

repeat for utf16->ut8

If "AString" were immutable, where would we fail?

Imagine that all nsAStrings currently allocated on the stack became a different type (nsAStringBuilder or std::wstring or something). But when we pass strings around, they are immutable. Classify any cases where this wouldn't work:

Take the following methods:

nsresult GetAString(nsAString &result)
{
  result.Assign("foo"); // this is ok, it can be converted to return a new
                        // immutable string
}

nsresult AppendToAString(nsAString &result)
{
  result.Append("foo"); // this won't work... it modifies the inout param so
                        // we would have to rewrite "result" to be
                        // nsAStringBuilder&, or split it into two separate
                        // params, one in, one out.
}