Skip to content
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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions include/boost/gil/io/path_spec.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// Copyright 2007-2008 Andreas Pokorny, Christian Henning
// Copyright 2024 Dirk Stolle
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
Expand All @@ -11,6 +12,7 @@
#include <boost/gil/io/detail/filesystem.hpp>

#include <cstdlib>
#include <cwchar>
#include <string>
#include <type_traits>

Expand Down Expand Up @@ -41,11 +43,13 @@ 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);
std::string result(len, '\0');
std::wcstombs( &result[0], s.c_str(), len );

return std::string( c, c + len );
return result;
}

inline std::string convert_to_string( char const* str )
Expand Down Expand Up @@ -80,18 +84,21 @@ 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 );
std::wcstombs( c, str, len );

return c;
}

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 );
std::wcstombs( c, str.c_str(), len );

return c;
}
Expand Down
Loading