Open Bug 959745 Opened 8 years ago Updated 8 years ago

mozilla::Version calls strdup too many times


(Core :: XPCOM, defect)

Windows 7
Not set




(Reporter: away, Unassigned)



This code in XRE_mainInit results in 8 calls to strdup:

>    if (mozilla::Version(mAppData->minVersion) > gToolkitVersion ||
>       mozilla::Version(mAppData->maxVersion) < gToolkitVersion) {

1. Version constructor on gToolkitVersion
2. Version constructor on mAppData->minVersion
3,4. operator> calls CompareVersions, which strdups both operands
5. Version constructor on gToolkitVersion
6. Version constructor on mAppData->maxVersion
7,8. operator< calls CompareVersions, which strdups both operands

(I noticed this from the corresponding |free| calls while I was debugging memory poisoning code.)
It may be possible to do this without *any* strdup at all. By substituting strncmp for strcmp, and possibly other similar changes, we should be able to avoid writing any nulls into the input string. And these Version objects are so transient that I don't think we have to worry about the underlying strings going away.

(If that level of optimization is unwarranted, I guess we can just remove the strdup from the Version constructor)
How many of these objects are constructed using string literals?  We can write a specialized ctor to avoid copying those ones...

I don't think the strncmp trick will work because basically the Version object currently assumes it does not own the string.
You need to log in before you can comment on or make changes to this bug.