Skip to content

Commit

Permalink
Merge pull request #1017 from MacroModel/master
Browse files Browse the repository at this point in the history
It is prohibited to directly use fast_io::string internally
  • Loading branch information
trcrsired authored Dec 6, 2024
2 parents c118c32 + 24de435 commit ed90b1b
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 23 deletions.
21 changes: 21 additions & 0 deletions fuzzing/0000.write/filebuf_file.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <fast_io.h>
#include <fast_io_legacy.h>

fast_io::filebuf_file obf(
#if defined(_WIN32) && !defined(__WINE__)
"nul"
#else
"/dev/null"
#endif
,
fast_io::open_mode::out);


extern "C" int LLVMFuzzerTestOneInput(std::uint8_t const *pptr, std::size_t n) noexcept
{
char const *ptr{reinterpret_cast<char const *>(pptr)};
using namespace fast_io::mnp;
::fast_io::operations::write_all(obf, ptr, ptr + n);

return 0;
}
21 changes: 21 additions & 0 deletions fuzzing/0000.write/wfilebuf_file.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <fast_io.h>
#include <fast_io_legacy.h>

fast_io::wfilebuf_file obf(
#if defined(_WIN32) && !defined(__WINE__)
"nul"
#else
"/dev/null"
#endif
,
fast_io::open_mode::out);


extern "C" int LLVMFuzzerTestOneInput(std::uint8_t const *pptr, std::size_t n) noexcept
{
wchar_t const *ptr{reinterpret_cast<wchar_t const *>(pptr)};
using namespace fast_io::mnp;
::fast_io::operations::write_all(obf, ptr, ptr + n / sizeof(wchar_t));

return 0;
}
10 changes: 5 additions & 5 deletions include/fast_io_hosted.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ freestanding ones.
#endif

#if !defined(__AVR__)
#include "fast_io_dsal/string_view.h"
#include "fast_io_dsal/string.h"
#include "fast_io_dsal/impl/common.h"
#include "fast_io_dsal/impl/vector.h"
#include "fast_io_dsal/impl/string_view.h"
#include "fast_io_dsal/impl/cstring_view.h"
#include "fast_io_dsal/impl/string.h"

#include "fast_io_hosted/platforms/native.h"
#include "fast_io_hosted/file_loaders/impl.h"
Expand All @@ -75,9 +78,6 @@ freestanding ones.

#include "fast_io_hosted/threads/threads.h"

#include "fast_io_dsal/impl/common.h"
#include "fast_io_dsal/impl/vector.h"

#include "fast_io_hosted/filesystem/native.h"
#include "fast_io_hosted/dll/dll.h"
#include "fast_io_hosted/process/process.h"
Expand Down
8 changes: 4 additions & 4 deletions include/fast_io_hosted/filesystem/win32_9xa.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ namespace fast_io
struct win32_9xa_dirent
{
void *file_struct{};
::fast_io::u8string find_path{};
::fast_io::containers::basic_string<char8_t, ::fast_io::native_global_allocator> find_path{};

::fast_io::win32_9xa_dir_handle d_handle{};
file_type d_type{};
[[maybe_unused]] ::std::uint_least64_t d_ino{};
::fast_io::u8string filename{};
::fast_io::containers::basic_string<char8_t, ::fast_io::native_global_allocator> filename{};

inline ~win32_9xa_dirent()
{
Expand All @@ -28,7 +28,7 @@ inline bool set_win32_9xa_dirent(win32_9xa_dirent &entry, bool start)
::fast_io::win32::win32_find_dataa wfda{};
if (start)
{
entry.find_path = ::fast_io::u8concat_fast_io(::fast_io::mnp::code_cvt(entry.d_handle.path), u8"\\*");
entry.find_path = ::fast_io::win32::details::concat_win32_9xa_dir_handle_path_str(::fast_io::mnp::code_cvt(entry.d_handle.path), u8"\\*");
entry.file_struct = ::fast_io::win32::FindFirstFileA(reinterpret_cast<char const *>(entry.find_path.c_str()), __builtin_addressof(wfda));
}
else
Expand All @@ -39,7 +39,7 @@ inline bool set_win32_9xa_dirent(win32_9xa_dirent &entry, bool start)
}
}

entry.filename = ::fast_io::u8string{::fast_io::u8concat_fast_io(::fast_io::mnp::code_cvt_os_c_str(wfda.cFileName))};
entry.filename = ::fast_io::win32::details::concat_win32_9xa_dir_handle_path_str(::fast_io::mnp::code_cvt_os_c_str(wfda.cFileName));

if (wfda.dwFileAttributes & 0x400)
{
Expand Down
6 changes: 3 additions & 3 deletions include/fast_io_hosted/filesystem/win32_9xa_at.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ inline constexpr win32_9xa_at_flags &operator^=(win32_9xa_at_flags &x, win32_9xa
namespace win32::details
{

inline ::fast_io::tlc::u8string concat_tlc_win32_9xa_path_uncheck_whether_exist(::fast_io::win32_9xa_dir_handle const &dirhd, char8_t const *path_c_str, ::std::size_t path_size) noexcept
inline ::fast_io::containers::basic_string<char8_t, ::fast_io::native_thread_local_allocator> concat_tlc_win32_9xa_path_uncheck_whether_exist(::fast_io::win32_9xa_dir_handle const &dirhd, char8_t const *path_c_str, ::std::size_t path_size) noexcept
{
auto const beg{path_c_str};
auto curr{beg};
Expand Down Expand Up @@ -89,7 +89,7 @@ inline ::fast_io::tlc::u8string concat_tlc_win32_9xa_path_uncheck_whether_exist(
}
}

return ::fast_io::tlc::u8concat_fast_io_tlc(::fast_io::mnp::code_cvt(dirhd.path), u8"\\", ::fast_io::mnp::os_c_str_with_known_size(beg, path_size));
return ::fast_io::win32::details::concat_tlc_win32_9xa_dir_handle_path_str(::fast_io::mnp::code_cvt(dirhd.path), u8"\\", ::fast_io::mnp::os_c_str_with_known_size(beg, path_size));
}

inline void win32_9xa_unlinkat_impl(::fast_io::win32_9xa_dir_handle const &dirhd, char8_t const *path_c_str, ::std::size_t path_size, win32_9xa_at_flags flags)
Expand Down Expand Up @@ -348,7 +348,7 @@ inline void win32_9xa_utimensat_impl(::fast_io::win32_9xa_dir_handle const &dirh
inline void win32_9xa_symlinkat_impl(char8_t const *oldpath_c_str, ::std::size_t oldpath_size,
::fast_io::win32_9xa_dir_handle const &newdirhd, char8_t const *newpath_c_str, ::std::size_t newpath_size)
{
::fast_io::u8cstring_view path{::fast_io::containers::null_terminated, oldpath_c_str, oldpath_size};
::fast_io::containers::basic_cstring_view<char8_t> path{::fast_io::containers::null_terminated, oldpath_c_str, oldpath_size};

::fast_io::u8win32_file_9xa f{::fast_io::win32::details::basic_win32_9xa_create_file_at_fs_dirent_impl(
__builtin_addressof(newdirhd), reinterpret_cast<char const *>(newpath_c_str), newpath_size,
Expand Down
55 changes: 44 additions & 11 deletions include/fast_io_hosted/platforms/win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -918,11 +918,44 @@ struct

struct win32_9xa_dir_handle
{
::fast_io::u8string path;
::fast_io::containers::basic_string<char8_t, ::fast_io::native_global_allocator> path;
};

namespace win32::details
{

template <typename... Args>
constexpr inline ::fast_io::containers::basic_string<char8_t, ::fast_io::native_global_allocator> concat_win32_9xa_dir_handle_path_str(Args &&...args)
{
constexpr bool type_error{::fast_io::operations::defines::print_freestanding_okay<::fast_io::details::dummy_buffer_output_stream<char8_t>, Args...>};
if constexpr (type_error)
{
return ::fast_io::basic_general_concat<false, char8_t, ::fast_io::containers::basic_string<char8_t, ::fast_io::native_global_allocator>>(
::fast_io::io_print_forward<char8_t>(::fast_io::io_print_alias(args))...);
}
else
{
static_assert(type_error, "some types are not printable, so we cannot concat ::fast_io::FFFstring");
return {};
}
}

template <typename... Args>
constexpr inline ::fast_io::containers::basic_string<char8_t, ::fast_io::native_thread_local_allocator> concat_tlc_win32_9xa_dir_handle_path_str(Args &&...args)
{
constexpr bool type_error{::fast_io::operations::defines::print_freestanding_okay<::fast_io::details::dummy_buffer_output_stream<char8_t>, Args...>};
if constexpr (type_error)
{
return ::fast_io::basic_general_concat<false, char8_t, ::fast_io::containers::basic_string<char8_t, ::fast_io::native_thread_local_allocator>>(
::fast_io::io_print_forward<char8_t>(::fast_io::io_print_alias(args))...);
}
else
{
static_assert(type_error, "some types are not printable, so we cannot concat ::fast_io::FFFstring");
return {};
}
}

inline void close_win32_9xa_dir_handle(win32_9xa_dir_handle &h) noexcept
{
h.path.clear();
Expand Down Expand Up @@ -952,7 +985,7 @@ struct find_struct_guard
inline void check_win32_9xa_dir_is_valid(win32_9xa_dir_handle const &h)
{
::fast_io::win32::win32_find_dataa wfda{};
auto temp_find_path{::fast_io::tlc::u8concat_fast_io_tlc(h.path, u8"\\*")};
auto temp_find_path{concat_tlc_win32_9xa_dir_handle_path_str(h.path, u8"\\*")};
auto find_struct{::fast_io::win32::FindFirstFileA(reinterpret_cast<char const *>(temp_find_path.c_str()), __builtin_addressof(wfda))};
if (find_struct == reinterpret_cast<void *>(static_cast<::std::ptrdiff_t>(-1)))
{
Expand All @@ -972,7 +1005,7 @@ inline win32_9xa_dir_handle basic_win32_9xa_create_dir_file_impl(char const *fil
#endif
= char8_t const *;

::fast_io::u8string path{::fast_io::u8concat_fast_io(::fast_io::mnp::os_c_str_with_known_size(
::fast_io::containers::basic_string<char8_t, ::fast_io::native_global_allocator> path{concat_win32_9xa_dir_handle_path_str(::fast_io::mnp::os_c_str_with_known_size(
reinterpret_cast<char8_t_const_may_alias_ptr>(filename_c_str), filename_c_str_len))};

for (auto &c : path)
Expand All @@ -995,7 +1028,7 @@ inline win32_9xa_dir_handle basic_win32_9xa_create_dir_file_impl(char const *fil
}

inline win32_9xa_dir_handle basic_win32_9xa_create_dir_file_at_fs_dirent_impl(win32_9xa_dir_handle const *directory_handle, char const *filename_c_str,
::std::size_t filename_c_str_len)
::std::size_t filename_c_str_len)
{
using char8_t_const_may_alias_ptr
#if __has_cpp_attribute(__gnu__::__may_alias__)
Expand Down Expand Up @@ -1037,15 +1070,15 @@ inline win32_9xa_dir_handle basic_win32_9xa_create_dir_file_at_fs_dirent_impl(wi
}
}

win32_9xa_dir_handle ret{::fast_io::u8concat_fast_io(::fast_io::mnp::code_cvt(directory_handle->path), u8"\\", ::fast_io::mnp::os_c_str_with_known_size(beg, filename_c_str_len))};
win32_9xa_dir_handle ret{concat_win32_9xa_dir_handle_path_str(::fast_io::mnp::code_cvt(directory_handle->path), u8"\\", ::fast_io::mnp::os_c_str_with_known_size(beg, filename_c_str_len))};

check_win32_9xa_dir_is_valid(ret);

return ret;
}

inline void *basic_win32_9xa_create_file_at_fs_dirent_impl(win32_9xa_dir_handle const *directory_handle, char const *filename_c_str,
::std::size_t filename_c_str_len, open_mode_perms ompm)
::std::size_t filename_c_str_len, open_mode_perms ompm)
{
using char8_t_const_may_alias_ptr
#if __has_cpp_attribute(__gnu__::__may_alias__)
Expand Down Expand Up @@ -1087,7 +1120,7 @@ inline void *basic_win32_9xa_create_file_at_fs_dirent_impl(win32_9xa_dir_handle
}
}

::fast_io::u8string str{::fast_io::u8concat_fast_io(::fast_io::mnp::code_cvt(directory_handle->path), u8"\\", ::fast_io::mnp::os_c_str_with_known_size(beg, filename_c_str_len))};
::fast_io::containers::basic_string<char8_t, ::fast_io::native_global_allocator> str{concat_win32_9xa_dir_handle_path_str(::fast_io::mnp::code_cvt(directory_handle->path), u8"\\", ::fast_io::mnp::os_c_str_with_known_size(beg, filename_c_str_len))};
auto handle{::fast_io::details::win32_create_file_impl<win32_family::ansi_9x>(str, ompm)};
return handle;
}
Expand Down Expand Up @@ -1131,7 +1164,7 @@ inline win32_9xa_dir_handle win32_9xa_create_dir_file_impl(T const &t)

template <::std::integral char_type>
inline win32_9xa_dir_handle win32_9xa_create_dir_file_at_fs_dirent_impl(win32_9xa_dir_handle const &directory_handle, char_type const *filename_c_str,
::std::size_t filename_c_str_len)
::std::size_t filename_c_str_len)
{
return win32_api_common_9xa(::fast_io::mnp::os_c_str_with_known_size(filename_c_str, filename_c_str_len),
win32_9xa_create_dir_file_at_fs_dirent{__builtin_addressof(directory_handle)});
Expand All @@ -1146,7 +1179,7 @@ inline win32_9xa_dir_handle win32_9xa_create_dir_file_at_impl(win32_9xa_dir_hand

template <::std::integral char_type>
inline void *win32_9xa_create_file_at_fs_dirent_impl(win32_9xa_dir_handle const &directory_handle, char_type const *filename_c_str,
::std::size_t filename_c_str_len, open_mode_perms ompm)
::std::size_t filename_c_str_len, open_mode_perms ompm)
{
return win32_api_common_9xa(::fast_io::mnp::os_c_str_with_known_size(filename_c_str, filename_c_str_len),
win32_9xa_create_file_at_fs_dirent{__builtin_addressof(directory_handle), ompm});
Expand All @@ -1163,7 +1196,7 @@ inline void *win32_9xa_create_file_at_impl(win32_9xa_dir_handle const &directory

struct win32_9xa_fs_dirent
{
win32_9xa_dir_handle handle{}; // path
win32_9xa_dir_handle handle{}; // path
::fast_io::manipulators::basic_os_c_str_with_known_size<char8_t> filename{}; // file
};

Expand All @@ -1184,7 +1217,7 @@ struct win32_9xa_at_entry
#endif
inline win32_9xa_at_entry win32_9xa_at_fdcwd() noexcept
{
return win32_9xa_at_entry{{::fast_io::u8concat_fast_io(u8".")}};
return win32_9xa_at_entry{{win32::details::concat_win32_9xa_dir_handle_path_str(u8".")}};
}

#if !defined(__CYGWIN__) && !defined(__WINE__) && !defined(__BIONIC__) && defined(_WIN32_WINDOWS)
Expand Down

0 comments on commit ed90b1b

Please sign in to comment.