-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix wrong buffer size in path string conversion functions #746
fix wrong buffer size in path string conversion functions #746
Conversation
I dont know much about multibyte string conversions, but I will review it to the best of my ability tomorrow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Big thanks @striezel
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the commits, I learned a lot from reading the description. From the include chain I couldn't find #include <cwchar>
, could you please add that? std::wcsrtombs
seems to be declared there.
I looked at std::codecvt
as possible C++ refactoring, but unfortunately it was disappointing.
Just to explain the basics (may not be completely accurate, but it should be enough to get a basic understanding): So when a string is converted from a wide string ( Here, the length calculation is done by |
Thanks for the explanation. I suppose the easiest solution is to throw boost.locale at it to completely nuke it, but I am not sure if it is worth it. I suppose people can open their streams and pass those if they want to do anything exotic. |
You are right.
Yes. As far as I remember, parts of that are deprecated in newer C++ standards. |
As far as I understand it, some parts of Boost.Locale also need the ICU library (International Components for Unicode) and that library is ca. one order of magnitude larger than Boost.Locale itself (counting file sizes for installed libraries here). Furthermore, GIL does not have any dependency on Boost.Locale as of now, so this would potentially add two new dependencies to GIL. That kind of overhead may not be acceptable to some users of Boost.GIL, especially when we can have a smaller solution with the help of the standard library. |
I agree, we should try to keep the list of required dependencies as short as possible. |
Description
Fixes #733.
Issue:
The length calculation of the destination buffers for conversions from
std::wstring
tostd::string
orwchar_t*
tochar*
is incorrect, causing a buffer overflow in some cases.Explanation:
The current code uses
wcslen()
to "calculate" the length of the converted destination string buffer. However,wcslen()
is just astrlen()
forwchar_t*
strings. So it returns the number of wide characters in awchar_t*
string, but this does not need to be the same as the number of narrow characters (that is:char
) in the converted string (char*
orstd::string
). It just happens to be the same, if the wide character string only uses characters of the English alphabet where each wide character is converted to exactly one narrow character.So the length has to be calculated properly. There are (at least) two possibilities:
Use
wcstombs()
's POSIX extension to calculate the length before doing the conversion.But that is an extension and cannot be relied upon to be present.
We should use
wcsrtombs()
instead, where that behaviour is not an extension but part of the function and we do not need to rely on the presence of an extension. This is what this pull request does.References
Tasklist