Closed Bug 943291 Opened 12 years ago Closed 12 years ago

widget/windows/nsDataObj.cpp should use Unicode APIs

Categories

(Core :: Widget: Win32, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla35

People

(Reporter: hsivonen, Assigned: u518207, Mentored)

References

Details

(Whiteboard: [good first bug][lang=c++])

Attachments

(1 file, 3 obsolete files)

widget/windows/nsDataObj.cpp calls nsPrimitiveHelpers::ConvertUnicodeToPlatformPlainText. Instead, it should use Unicode system APIs to make such conversion unnecessary.
We have to convert to system-locale-dependent string for compatibility. But we could use WideCharToMultiByte(CP_ACP, ...) because this code is Windows-specific anyway.
Mentor: VYV03354, jmathies, tabraldes, netzen
Whiteboard: [good first bug][lang=c++]
It seems that we need to allocate buffer for plainTextData. What is the allocation type, or in other words what is the right method to use for an allocation. I couldn't find what is the original allocation type.
Hm, things are really screwed up. nsSaveAsCharset::Convert is using PR_Malloc() to allocate the data which are supposed to be freed by PR_Free(): https://mxr.mozilla.org/mozilla-central/source/intl/unicharutil/nsSaveAsCharset.cpp?rev=b81d755b1b37#169 One consumer is using free() to free the allocated buffer: https://mxr.mozilla.org/mozilla-central/source/widget/gtk/nsDragService.cpp?rev=6c1c7e45c902#1423 Another consumer is using nsMemory:Free(): https://mxr.mozilla.org/mozilla-central/source/widget/windows/nsDataObj.cpp?rev=1a64b22632bc#1277
Attached patch 943291.patch (obsolete) — — Splinter Review
The second if statement uses nsMemory::Alloc() for allocation and the respective nsMemory:Free() like you mentioned. To conform with it my choice was nsMemory::Alloc() for memory allocation. Moreover, PR_Malloc() was unrecognized and it gave me some motivation to look for the alternative. The gtk's free() method wasn't modified because I can't check it; my compilation process seems to ignore gtk. I compile in Windows with VS.
Attachment #8493177 - Flags: review?(VYV03354)
Attachment #8493177 - Flags: review?(VYV03354)
Attachment #8493177 - Attachment is obsolete: true
Attached patch 943291.patch (obsolete) — — Splinter Review
Oh, missed the null termination.
Attachment #8493248 - Flags: review?(VYV03354)
Comment on attachment 8493248 [details] [diff] [review] 943291.patch Review of attachment 8493248 [details] [diff] [review]: ----------------------------------------------------------------- ::: widget/windows/nsDataObj.cpp @@ +1270,5 @@ > DWORD allocLen = (DWORD)len; > if ( aFE.cfFormat == CF_TEXT ) { > // Someone is asking for text/plain; convert the unicode (assuming it's present) > // to text with the correct platform encoding. > + extra space @@ +1272,5 @@ > // Someone is asking for text/plain; convert the unicode (assuming it's present) > // to text with the correct platform encoding. > + > + // 512 reserved char positions. > + size_t bufferSize = sizeof(char)*(len / 2 + 1 + 512); Where 512 come from? The buffer will be insufficient if len > 512. One Unicode character will be converted to at most two multibyte character. So |len + 2| will be sufficient. @@ +1278,4 @@ > char16_t* castedUnicode = reinterpret_cast<char16_t*>(data); > int32_t plainTextLen = 0; > + plainTextLen = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)castedUnicode, len / 2 + 1, plainTextData, bufferSize, NULL, NULL); > + plainTextLen = plainTextLen ? plainTextLen - 1 : 0; If WideCharToMultiByte fails, one byte garbage will be copied to the destination.
Attachment #8493248 - Flags: review?(VYV03354) → review-
Attached patch 943291-v2.patch (obsolete) — — Splinter Review
The 512 were from here: https://mxr.mozilla.org/mozilla-central/source/intl/unicharutil/nsSaveAsCharset.cpp?rev=b81d755b1b37#187 they are named RESERVE_FALLBACK_BYTES in the original allocation. Removed those additional bits. You are right, sometimes this function translates wide character into two characters. Modifications are performed.
Attachment #8493248 - Attachment is obsolete: true
Attachment #8493578 - Flags: review?(VYV03354)
Comment on attachment 8493578 [details] [diff] [review] 943291-v2.patch Review of attachment 8493578 [details] [diff] [review]: ----------------------------------------------------------------- ::: widget/windows/nsDataObj.cpp @@ +1276,5 @@ > char16_t* castedUnicode = reinterpret_cast<char16_t*>(data); > int32_t plainTextLen = 0; > + plainTextLen = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)castedUnicode, len / 2 + 1, plainTextData, bufferSize, NULL, NULL); > + // If WideCharToMultiByte fails, one byte garbage will be copied to the destination. > + plainTextLen = plainTextLen ? plainTextLen - 1 : 1; Could you propagate the failure to the caller instead of copying the garbage? See the else clause below about the error handling.
Attachment #8493578 - Attachment is obsolete: true
Attachment #8493578 - Flags: review?(VYV03354)
Attached patch 943291-v3.patch — — Splinter Review
Here is next modification.
Attachment #8494371 - Flags: review?(VYV03354)
Comment on attachment 8494371 [details] [diff] [review] 943291-v3.patch Looks good to me.
Attachment #8494371 - Flags: review?(VYV03354) → review+
I tried to run mochitest, but it returns an error 93198 ERROR Automation Error: Received unexpected exception while running application. In my opinion it is not related to this patch. Maybe to try running another test.
Thanks.
Try looks green, landing: https://hg.mozilla.org/integration/mozilla-inbound/rev/7b9c4085e11f Thanks for the contribution to Mozilla!
That's good. Thanks for guidance!
Assignee: nobody → mycoolclub
Status: NEW → RESOLVED
Closed: 12 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla35
Thank you for fixing this! One step closer to getting rid of nsIPlatformCharset.
Good.
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: