Closed Bug 384803 Opened 17 years ago Closed 17 years ago

nsStringBundle::FormatStringFromID should return nsMemory allocated strings

Categories

(Core :: Internationalization, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED

People

(Reporter: prasad, Assigned: prasad)

Details

Attachments

(1 file, 1 obsolete file)

User-Agent:       Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.4) Gecko/20070508 Iceweasel/2.0.0.4 (Debian-2.0.0.4-1)
Build Identifier: 

The FormatString function in nsStringBundle uses nsTextFormatter::smprintf, but
nsTextFormatter does not use the shared nsMemory allocator. Instead it is recommended to free the memory it allocates using nsTextFormatter::smprintf_free.

FormatString through FormatStringFromID is being used in a lot of other modules that do not have access to nsTextFormatter (non MOZILLA_INTERNAL_API code). Lets instead use nsMemory based allocation for the result that we give out and free the string returned by smprintf ourselves!


Reproducible: Always
Stores the result from nsTextFormatter::smprintf to a temporary string, nsCRT::strdup it and then free the temporary string using nsTextFormatter::smprintf_free.
Attachment #268697 - Flags: review?(smontagu)
Comment on attachment 268697 [details] [diff] [review]
FormatString uses nsCRT::strdup to allocate the result buffer.

>Index: intl/strres/src/nsStringBundle.cpp
>@@ -380,28 +380,37 @@ nsStringBundle::FormatString(const PRUni
>-  *aResult = 
>+  PRUnichar *text = 
>     nsTextFormatter::smprintf(aFormatStr,

I believe this can fail, so you should add:

if (!text) {
    *aResult = nsnull;
    return NS_ERROR_OUT_OF_MEMORY;
}

>+  // Instead it is recommended to free the memory it allocates using

It is more of a requirement / demand than a recommendation.

>+  // nsTextFormatter::smprintf_free.  Lets instead use nsMemory based

I think this should be "Let's"

>+  *aResult = nsCRT::strdup(text);
use NS_strdup
Attachment #268697 - Flags: review?(smontagu) → review-
The patch is updated as per the comments by timeless.
Attachment #268697 - Attachment is obsolete: true
Attachment #269068 - Flags: review?(timeless)
Status: UNCONFIRMED → NEW
Ever confirmed: true
Comment on attachment 269068 [details] [diff] [review]
FormatString uses NS_strdup to allocate the result buffer

there should be a space between *aResult and ?

:)
Attachment #269068 - Flags: superreview?(cbiesinger)
Attachment #269068 - Flags: review?(timeless)
Attachment #269068 - Flags: review?(smontagu)
Attachment #269068 - Flags: review+
Attachment #269068 - Flags: superreview?(cbiesinger) → superreview+
Attachment #269068 - Flags: review?(smontagu) → review+
timeless, can you checkin this patch for me?  I guess you could add that space when checking it in.
Comment on attachment 269068 [details] [diff] [review]
FormatString uses NS_strdup to allocate the result buffer

mozilla/intl/strres/src/nsStringBundle.cpp 	1.152
Assignee: smontagu → prasad
Status: NEW → RESOLVED
Closed: 17 years ago
Resolution: --- → FIXED
Flags: in-testsuite-
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: