Skip to content

Commit

Permalink
fix wrong buffer size in path string conversion functions
Browse files Browse the repository at this point in the history
  • Loading branch information
striezel committed May 5, 2024
1 parent 2c3ee86 commit 437a809
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions include/boost/gil/io/path_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ inline std::string convert_to_string( std::string const& obj)

inline std::string convert_to_string( std::wstring const& s )
{
std::size_t len = wcslen( s.c_str() );
char* c = reinterpret_cast<char*>( alloca( len ));
wcstombs( c, s.c_str(), len );
std::mbstate_t state = std::mbstate_t();
const wchar_t* str = s.c_str();
const std::size_t len = std::wcsrtombs(nullptr, &str, 0, &state);
char* c = reinterpret_cast<char*>( alloca( len + 1));
wcstombs( c, s.c_str(), len + 1 );

return std::string( c, c + len );
}
Expand Down Expand Up @@ -80,7 +82,8 @@ inline char const* convert_to_native_string( const std::string& str )

inline char const* convert_to_native_string( const wchar_t* str )
{
std::size_t len = wcslen( str ) + 1;
std::mbstate_t state = std::mbstate_t();
const std::size_t len = std::wcsrtombs(nullptr, &str, 0, &state) + 1;
char* c = new char[len];
wcstombs( c, str, len );

Expand All @@ -89,7 +92,9 @@ inline char const* convert_to_native_string( const wchar_t* str )

inline char const* convert_to_native_string( std::wstring const& str )
{
std::size_t len = wcslen( str.c_str() ) + 1;
std::mbstate_t state = std::mbstate_t();
const wchar_t* wstr = str.c_str();
const std::size_t len = std::wcsrtombs(nullptr, &wstr, 0, &state) + 1;
char* c = new char[len];
wcstombs( c, str.c_str(), len );

Expand Down

0 comments on commit 437a809

Please sign in to comment.