Mozilla 2/Strings/Static Analysis: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
(updated mod suggestion)
(added section)
Line 31: Line 31:
}
}
</pre>
</pre>
=== Finalizer-less strings ===
[dmandelin] I'm writing up my thoughts on how to make strings work without requiring any MMGC finalizers, which is the first application of this analysis.

Revision as of 02:40, 28 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" [dmandelin] Can modification be defined as any of (1) calling a non-const method, (2) passing as a non-const argument, (3) being a parameter, or (4) escaping (by having a pointer stored or a pointer or reference returned)?

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.
}

Finalizer-less strings

[dmandelin] I'm writing up my thoughts on how to make strings work without requiring any MMGC finalizers, which is the first application of this analysis.