Skip to content

Commit

Permalink
fix: Wrong buffer size in path string conversion functions (#746)
Browse files Browse the repository at this point in the history
Fixes #733
  • Loading branch information
striezel authored May 13, 2024
1 parent 4fec872 commit abb561a
Showing 1 changed file with 15 additions and 8 deletions.
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

0 comments on commit abb561a

Please sign in to comment.