wchar_t tmp[256]; // potential buffer overflow exploit here
swprintf(tmp, L"%S", name); // %S (note caps) converts narrow (8 byte) to unicode
This will fail, sooner or later, because this conversion only seems to work for ASCII defined characters (ie char values less than 127). WinLatin (1252) has plenty of characters which are not ASCII. You might think you'll never encounter them, but one way that they can creep in is if you use MSWord's smartquotes, and then paste text from Word into your filename. Probably filenames written by people outside of the US also tend to have these characters.
I don't know what would happen with swprintf if you had some other code page, but I'm guessing it also would be bad.
Here's the proper way to do it.
Not only does this work for 'fancy' characters, but it also prevents the buffer overflow bug.
No comments:
Post a Comment