Skip to content

Commit 7c138a6

Browse files
authored
Next (#1217)
* Refactor tuple element access in fast_io_dsal - Updated `get` function signatures in `tuple.h` to return `decltype(auto)` for improved type deduction. - Enhanced consistency by ensuring `[[nodiscard]]` attribute is applied uniformly across all overloads of `get`. - Added new overloads for `get` to support retrieval by type, improving usability and flexibility. - Minor formatting adjustments for better readability in the `forward_as_tuple` function. * Add tuple application utility and size retrieval in fast_io_dsal - Introduced `apply` function to facilitate the application of a callable to elements of a tuple. - Added `tuple_size` function to retrieve the size of a `fast_io::containers::tuple`. - Implemented helper functions in the `details` namespace for improved code organization and clarity. * Refactor error handling and improve function signatures across platform headers - Updated error handling in various platform headers to consistently use `throw_posix_error()` with appropriate error codes. - Enhanced function signatures in POSIX and platform-specific implementations for clarity and consistency. - Replaced direct calls to system functions with `noexcept_call` for safer error management. - Improved readability and maintainability of the code by standardizing error checks and function calls. * Refactor thread start routine declaration and improve error handling in POSIX headers - Updated `thread_start_routine` in both NT and Win32 headers to include `FAST_IO_WINSTDCALL` for better calling convention compatibility. - Enhanced error handling in `get_module_install_path_from_argv0` by checking for a null environment path and throwing an appropriate error. - Improved readability by initializing pointers to empty and using consistent formatting in the `argv0.h` file. - Adjusted conditional compilation checks in `posix.h` for clearer handling of open mode flags. * Refactor `get` function signatures in tuple.h for improved type deduction - Updated `get` function signatures to return `auto&&` instead of `decltype(auto)` for better type handling. - Enhanced consistency across all overloads of `get` by applying uniform return types. - Minor adjustments made to improve readability and maintainability of the code. * f * Enhance error handling and improve path resolution in module installation functions - Added error checks for null pointers and buffer overflows in `get_module_install_path_from_argv0` to ensure robust path resolution. - Updated `get_module_install_path` to handle `sysctl` errors more accurately. - Replaced direct system calls with `noexcept_call` for safer error management across various platform headers. - Improved readability and maintainability by standardizing error handling and function calls. * Enhance Android logger functionality with noexcept specifier - Updated function signatures in `android.h` to include `noexcept` for improved safety and performance. - Refactored operator overloads and logging implementations to ensure exception safety during logging operations. - Improved code clarity and maintainability by standardizing function signatures across the Android logging interface. * Enhance error handling and improve function signatures across platform headers - Added null pointer check in `is_invalid_dos_filename_with_size` to prevent crashes. - Refactored `zero_copy_transmit64_define` to streamline character transmission logic and improve performance. - Updated `try_lock` method in `posix_file_lock` to return a boolean indicating lock success. - Adjusted function signatures in various headers for consistency and clarity, including changes to `NtSetSystemTime` and `RtlAcquirePebLock`. - Improved error handling in `posix_seek_impl` to throw appropriate errors for overflow conditions. * Enhance POSIX and Win32 platform headers with improved error handling and function signatures - Updated `sys_mmap` to use `long` for return type and improved error handling for memory mapping. - Refactored `open_socket_impl` to include checks for socket open modes and set appropriate flags for non-blocking and close-on-exec behavior. - Introduced `to_win32_page_protect` function for better handling of file map attributes in Win32. - Enhanced error handling in `create_file_mapping_impl` and random number generation functions to account for API behavior as per MSDN documentation. * f * f2 * f3 * Refactor timestamp handling in nt_family_clock_settime for improved type safety - Changed the type of `tms` from `uint_least64_t` to `int_least64_t` to ensure proper handling of negative values. - Updated calculations for `tms` to maintain consistency and prevent potential overflow issues. * Update Win32 linker headers to correct function signatures and improve consistency - Adjusted function signatures in `msvc_linker_32_i686.h`, `msvc_linker_32.h`, `msvc_linker_64.h`, and `msvc_linker_arm64ec.h` to ensure proper handling of parameters and return types. - Enhanced consistency across linker headers by standardizing the naming conventions for function aliases. - Improved clarity and maintainability of the code by ensuring uniformity in the handling of alternate names for Windows API functions. * Refactor append methods in process argument and environment structures to return references - Updated `append` methods in `basic_win32_process_args`, `basic_win32_process_envs`, and `posix_process_args` to return a reference to the current object, enhancing method chaining capabilities. - Improved code clarity and consistency across process argument and environment handling. * Fix path resizing logic in POSIX process header for accurate string length calculation - Updated the resizing logic in `posix.h` to correctly account for the null terminator when determining the length of the string. - Added a trailing slash to the returned path string to ensure proper formatting when appending filenames. * Add posix_vfork option and refactor process execution methods for POSIX - Introduced a new `posix_vfork` option in `option.h` to support process creation using vfork, which has specific limitations on parameter modifications. - Refactored process execution methods in `posix.h` to utilize a common `fork_execveat_impl` function, streamlining the handling of process creation based on the selected mode. - Enhanced clarity and maintainability of the code by consolidating vfork and pipefork logic into unified implementations. * Refactor process execution logic in posix.h to improve clarity and maintainability - Added conditional compilation for session management in the vfork_and_execveat function, enhancing the handling of process creation modes. - Improved code organization by encapsulating session management logic within preprocessor directives, allowing for easier modifications in the future. * Update vfork_and_execveat function to include unused parameter for process mode - Modified the vfork_and_execveat function signature to add [[maybe_unused]] for the process_mode parameter, improving code clarity and indicating that the parameter may not be utilized in all contexts. * Enhance process_mode enumeration and update posix_execveat function - Changed the process_mode enumeration to a scoped enum class for better type safety and clarity. - Added a new 'follow' option to the process_mode enum to allow symbolic links to be followed during process execution. - Updated the posix_execveat function to accept the process_mode parameter, modifying behavior based on the follow option. - Refactored flags handling in posix_execveat and vfork_and_execveat functions to improve clarity and maintainability. * Refactor process creation logic in nt.h and update process_mode options - Introduced a new flag in process_mode to control the appending of paths to argv0, enhancing process creation flexibility. - Updated the nt_6x_process_create_impl and nt_create_process_overloads functions to utilize the new argv0_no_path_append flag for improved argument handling. - Refactored open_mode handling in process creation functions to accommodate the follow option, ensuring consistent behavior across different modes. - Enhanced clarity and maintainability of the code by organizing process parameter initialization and handling logic. * nt_process_args * Enhance argument handling in nt_6x_process_create_impl by adding quotation marks around paths - Updated the process creation logic to include double quotation marks around the image path and process parameters when appending to argv0. - Improved clarity in the handling of DOS and NT paths by ensuring proper formatting during argument construction. * Refactor environment handling in posix_process_args for improved clarity - Updated the `cstr_guard` return logic to enhance readability by removing unnecessary return statements. - Introduced a new namespace `posix` to encapsulate platform-specific environment variable handling, including `_NSGetEnviron` for Darwin and `environ` for other platforms. - Streamlined the `get_envs` function to utilize the new `posix` namespace, improving code organization and maintainability. * Fix argument initialization in args_envs.cc by updating process argument structure - Changed the initialization of process arguments from a string to a native__process_args structure for improved clarity and consistency in argument handling. - This update enhances the readability of the code and aligns with recent refactoring efforts in process management. * Refactor posix namespace in arg_env.h to improve environment variable handling - Moved platform-specific environment variable declarations into the posix namespace for better organization and clarity. - Updated references in the get_envs function to utilize the new namespace, enhancing code maintainability and readability. - Removed redundant declarations to streamline the code structure. * f * f * Update process argument handling in process.cc and nt.h - Changed the initialization of process arguments in process.cc to use native_process_args for improved clarity. - Updated constructor signatures in nt.h to remove default arguments for process_args_with_argv0, enhancing consistency and readability across process creation functions. * Add args_with_argv0_t structure and update process constructors - Introduced a new `args_with_argv0_t` structure to encapsulate argument handling with `argv0`. - Updated constructors in `nt.h` and `posix.h` to accept `args_with_argv0_t`, enhancing consistency in process creation across platforms. - Improved clarity in argument handling by ensuring all relevant constructors utilize the new structure, streamlining the process initialization logic. * Refactor process constructors in nt.h, posix.h, and win32.h to standardize argument handling - Updated constructors across nt.h, posix.h, and win32.h to use a consistent default for `process_mode`, specifically `argv0_no_path_append`. - Enhanced clarity in argument handling by ensuring all relevant constructors utilize the new default, streamlining process initialization logic. - Improved maintainability and readability of the code by aligning constructor signatures across different platforms. * Refactor argument handling in win32.h for consistency and clarity - Standardized the usage of `char_literal_v` in the argument construction process by removing unnecessary spaces in template syntax. - Enhanced readability and maintainability of the code by ensuring consistent formatting across argument handling in the `win32_winnt_process_create_from_handle_imp` function. * Refactor error handling and object duplication in win32.h - Updated the `throw_win32_error` function call to remove the status argument for improved clarity in error handling. - Modified the `win32_duplicate_object_std` function to use a boolean for the `inherit` parameter, enhancing readability and consistency in handle duplication logic. * f * k * Refactor thread implementation in nt.h and impl.h for clarity and consistency - Simplified the namespace structure for `fast_io::win32::nt`, improving organization. - Updated thread-related functions and constructors to use `inline constexpr` for better performance and clarity. - Enhanced error handling in sleep functions to ensure proper validation of input parameters. - Streamlined the usage of `nt_thread` and `this_thread` to improve readability and maintainability across the codebase. * fix thread * Enhance thread handling and error management in nt and win32 headers - Updated `RtlCreateUserThread` signature to accept a function pointer for improved flexibility. - Refined error handling in thread creation and management functions to ensure proper resource cleanup on failure. - Standardized the use of `inline constexpr` for thread-related functions to enhance performance and clarity. - Improved sleep functions to validate input parameters and handle edge cases more effectively. - Streamlined namespace usage and organization for better code maintainability. * Update RtlCreateUserThread signatures across linker headers for consistency - Modified the `RtlCreateUserThread` linker comments in multiple platform-specific headers to ensure uniformity in function signature representation. - Replaced `std::ranges::destroy_at` with `std::destroy_at` in thread management code for improved clarity and performance. * Add documentation comment to thread_start_routine in win32.h - Included a comment in the `thread_start_routine` function to clarify that the API function is called directly and that any CRT-specific processing occurs in `DllMain DLL_THREAD_ATTACH`. - This enhancement improves code readability and understanding of the threading implementation. * t1 * Refactor threading implementation for POSIX and Windows - Updated the threading example to enable functionality across all platforms by removing conditional compilation for non-Windows systems. - Enhanced error handling in mutex locking by throwing specific POSIX errors. - Introduced a new `pthread.h` file to encapsulate POSIX thread management, improving organization and maintainability. - Refined memory management in thread-related classes to utilize a consistent allocator pattern. - Removed the obsolete `posix.h` file to streamline the threading interface.
1 parent 2748df5 commit 7c138a6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2649
-632
lines changed

examples/0035.process/args_envs.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ int main(int argc, char **argv)
1919

2020
::fast_io::native_process p{
2121
::fast_io::mnp::os_c_str(argv[1]),
22-
{"char",
23-
L"wchar Double\"quotation\"marks",
24-
u8"u8",
25-
u"u16",
26-
U"u32",
27-
114514,
28-
115145.1919810,
29-
::fast_io::concat_fast_io("This ", 1, "s ", ::fast_io::mnp::code_cvt_os_c_str(u8"just "), "a parameter")},
22+
::fast_io::native_process_args{"char",
23+
L"wchar Double\"quotation\"marks",
24+
u8"u8",
25+
u"u16",
26+
U"u32",
27+
114514,
28+
115145.1919810,
29+
::fast_io::concat_fast_io("This ", 1, "s ", ::fast_io::mnp::code_cvt_os_c_str(u8"just "), "a parameter")},
3030
{"env1",
3131
L"env2",
3232
3,

examples/0035.process/pipe.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ int main(int argc, char **argv)
2020
::fast_io::iobuf_pipe pipe_err;
2121
::fast_io::native_process p{
2222
::fast_io::mnp::os_c_str(argv[1]),
23-
{},
23+
::fast_io::native_process_args{},
2424
{},
2525
{::fast_io::posix_dev_null(), pipe_out.handle, pipe_err.handle},
2626
::fast_io::process_mode::none};

examples/0035.process/process.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ int main(int argc, char **argv)
1111
::fast_io::io::perr("Usage: ", ::fast_io::mnp::os_c_str(*argv), " <exe>\n");
1212
return 1;
1313
}
14-
::fast_io::native_process p{::fast_io::mnp::os_c_str(argv[1]), {}, {}, {.in = fast_io::in(), .out = fast_io::out(), .err = fast_io::err()}, ::fast_io::process_mode::none};
14+
::fast_io::native_process p{::fast_io::mnp::os_c_str(argv[1]), ::fast_io::native_process_args{}, {}, {.in = fast_io::in(), .out = fast_io::out(), .err = fast_io::err()}, ::fast_io::process_mode::none};
1515
auto ec{wait(p)};
1616
::fast_io::io::perrln(::fast_io::mnp::os_c_str(*argv), " -> Exit code: ", static_cast<int>(ec.wait_loc));
1717
}

examples/0040.thread/thread.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
int main()
55
{
6-
#ifdef _WIN32 // temporaryly disable this example on rest platforms
7-
auto t = ::fast_io::thread{[](int param)
6+
auto t = ::fast_io::native_thread{[](int param)
87
#if __cpp_static_call_operator >= 2020207L
98
static
109
#endif
@@ -16,6 +15,8 @@ int main()
1615
#else
1716
::fast_io::println("the child thread id is: ", ::fast_io::mnp::pointervw(::fast_io::this_thread::get_id()));
1817
#endif
18+
#else
19+
::fast_io::println("the child thread id is: ", ::fast_io::this_thread::get_id());
1920
#endif
2021
// ::fflush(stdout);
2122
::fast_io::this_thread::sleep_for(::std::chrono::seconds{1});
@@ -29,8 +30,9 @@ int main()
2930
#else
3031
::fast_io::println("the parent thread id is: ", ::fast_io::mnp::pointervw(::fast_io::this_thread::get_id()));
3132
#endif
33+
#else
34+
::fast_io::println("the parent thread id is: ", ::fast_io::this_thread::get_id());
3235
#endif
3336

34-
return 0;
35-
#endif
37+
3638
}

include/fast_io_core_impl/buffer_view.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ struct basic_obuffer_view
181181
{
182182
return static_cast<::std::size_t>(end_ptr - begin_ptr);
183183
}
184+
185+
inline constexpr bool empty() const noexcept
186+
{
187+
return begin_ptr == curr_ptr;
188+
}
184189
};
185190

186191
template <::std::integral char_type>

include/fast_io_driver/install_path/argv0.h

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <limits.h>
44
#include <stdlib.h>
55
#include <cstring>
6+
#include <unistd.h>
67

78
#ifndef PATH_MAX
89
#define PATH_MAX 4096
@@ -20,7 +21,7 @@ inline ::fast_io::install_path get_module_install_path_from_argv0(char const *ar
2021
{
2122
if (!argv0) [[unlikely]]
2223
{
23-
throw_posix_error();
24+
throw_posix_error(EINVAL);
2425
}
2526

2627
::fast_io::install_path ret;
@@ -36,10 +37,13 @@ inline ::fast_io::install_path get_module_install_path_from_argv0(char const *ar
3637
char path_list_separator[8] = ":"; // could be ":; "
3738
if (argv0[0] == path_separator)
3839
{
39-
[[maybe_unused]] auto const unused1{::fast_io::noexcept_call(realpath, argv0, newpath)};
40-
if (auto status{::fast_io::noexcept_call(access, newpath, F_OK)}; !status)
40+
char *rp{::fast_io::noexcept_call(::realpath, argv0, newpath)};
41+
if (!rp) [[unlikely]]
42+
{
43+
throw_posix_error();
44+
}
45+
if (::fast_io::noexcept_call(::access, newpath, F_OK) == 0)
4146
{
42-
newpath[PATH_MAX - 1] = 0;
4347
ret.module_name = ::fast_io::u8concat_fast_io(::fast_io::mnp::code_cvt_os_c_str(newpath));
4448
auto const begin{strlike_begin(::fast_io::io_strlike_type<char8_t, ::fast_io::u8string>, ret.module_name)};
4549
auto curr{strlike_curr(::fast_io::io_strlike_type<char8_t, ::fast_io::u8string>, ret.module_name)};
@@ -57,18 +61,39 @@ inline ::fast_io::install_path get_module_install_path_from_argv0(char const *ar
5761
}
5862
else
5963
{
60-
throw_posix_error(status);
64+
throw_posix_error();
6165
}
6266
}
6367
else if (__builtin_strchr(argv0, static_cast<int>(path_separator)))
6468
{
65-
[[maybe_unused]] auto const unused1{::fast_io::noexcept_call(getcwd, newpath2, PATH_MAX)};
66-
::fast_io::noexcept_call(strncat, newpath2, path_separator_as_string, PATH_MAX + 256);
67-
::fast_io::noexcept_call(strncat, newpath2, argv0, PATH_MAX + 256);
68-
[[maybe_unused]] auto const unused2{::fast_io::noexcept_call(realpath, newpath2, newpath)};
69-
if (auto status{::fast_io::noexcept_call(access, newpath, F_OK)};!status)
69+
if (!::fast_io::noexcept_call(::getcwd, newpath2, PATH_MAX)) [[unlikely]]
70+
{
71+
throw_posix_error();
72+
}
73+
{
74+
auto const used1{::std::strlen(newpath2)};
75+
if (used1 + 1 >= sizeof(newpath2)) [[unlikely]]
76+
{
77+
throw_posix_error(ERANGE);
78+
}
79+
::fast_io::noexcept_call(::strncat, newpath2, path_separator_as_string, sizeof(newpath2) - used1 - 1);
80+
}
81+
{
82+
auto const used2{::std::strlen(newpath2)};
83+
auto const add2{::std::strlen(argv0)};
84+
if (used2 + add2 >= sizeof(newpath2)) [[unlikely]]
85+
{
86+
throw_posix_error(ERANGE);
87+
}
88+
::fast_io::noexcept_call(::strncat, newpath2, argv0, sizeof(newpath2) - used2 - 1);
89+
}
90+
char *rp2{::fast_io::noexcept_call(::realpath, newpath2, newpath)};
91+
if (!rp2) [[unlikely]]
92+
{
93+
throw_posix_error();
94+
}
95+
if (::fast_io::noexcept_call(::access, newpath, F_OK) == 0)
7096
{
71-
newpath[PATH_MAX - 1] = 0;
7297
ret.module_name = ::fast_io::u8concat_fast_io(::fast_io::mnp::code_cvt_os_c_str(newpath));
7398
auto const begin{strlike_begin(::fast_io::io_strlike_type<char8_t, ::fast_io::u8string>, ret.module_name)};
7499
auto curr{strlike_curr(::fast_io::io_strlike_type<char8_t, ::fast_io::u8string>, ret.module_name)};
@@ -84,26 +109,45 @@ inline ::fast_io::install_path get_module_install_path_from_argv0(char const *ar
84109
ret.module_name.erase(begin, curr);
85110
return ret;
86111
}
87-
else
88-
{
89-
throw_posix_error(status);
90-
}
112+
throw_posix_error();
91113
}
92114
else
93115
{
94-
char *saveptr;
95-
char *pathitem;
96-
char *save_path{::fast_io::noexcept_call(getenv, "PATH")};
97-
for (pathitem = ::fast_io::noexcept_call(strtok_r, save_path, path_list_separator, &saveptr); pathitem;
98-
pathitem = ::fast_io::noexcept_call(strtok_r, nullptr, path_list_separator, &saveptr))
116+
char *saveptr{};
117+
char *pathitem{};
118+
char const *env_path{::fast_io::noexcept_call(::getenv, "PATH")};
119+
if (!env_path) [[unlikely]]
120+
{
121+
throw_posix_error(EINVAL);
122+
}
123+
char pathbuf[PATH_MAX + 256 + 1]{};
124+
::fast_io::noexcept_call(::strncpy, pathbuf, env_path, PATH_MAX + 256);
125+
pathbuf[PATH_MAX + 256] = 0;
126+
127+
for (pathitem = ::fast_io::noexcept_call(::strtok_r, pathbuf, path_list_separator, &saveptr); pathitem;
128+
pathitem = ::fast_io::noexcept_call(::strtok_r, nullptr, path_list_separator, &saveptr))
99129
{
100-
::fast_io::noexcept_call(strncpy, newpath2, pathitem, PATH_MAX + 256);
101-
::fast_io::noexcept_call(strncat, newpath2, path_separator_as_string, PATH_MAX + 256);
102-
::fast_io::noexcept_call(strncat, newpath2, argv0, PATH_MAX + 256);
103-
[[maybe_unused]] auto const unused1{::realpath(newpath2, newpath)};
104-
if (!::fast_io::noexcept_call(access, newpath, F_OK))
130+
::fast_io::noexcept_call(::strncpy, newpath2, pathitem, PATH_MAX + 256);
131+
{
132+
auto const used1{::std::strlen(newpath2)};
133+
if (used1 + 1 >= sizeof(newpath2)) [[unlikely]]
134+
{
135+
continue;
136+
}
137+
::fast_io::noexcept_call(::strncat, newpath2, path_separator_as_string, sizeof(newpath2) - used1 - 1);
138+
}
139+
{
140+
auto const used2{::std::strlen(newpath2)};
141+
auto const add2{::std::strlen(argv0)};
142+
if (used2 + add2 >= sizeof(newpath2)) [[unlikely]]
143+
{
144+
continue;
145+
}
146+
::fast_io::noexcept_call(::strncat, newpath2, argv0, sizeof(newpath2) - used2 - 1);
147+
}
148+
char *rp{::fast_io::noexcept_call(::realpath, newpath2, newpath)};
149+
if (rp && ::fast_io::noexcept_call(::access, newpath, F_OK) == 0)
105150
{
106-
newpath[PATH_MAX - 1] = 0;
107151
ret.module_name = ::fast_io::u8concat_fast_io(::fast_io::mnp::code_cvt_os_c_str(newpath));
108152
auto const begin{strlike_begin(::fast_io::io_strlike_type<char8_t, ::fast_io::u8string>, ret.module_name)};
109153
auto curr{strlike_curr(::fast_io::io_strlike_type<char8_t, ::fast_io::u8string>, ret.module_name)};
@@ -120,7 +164,7 @@ inline ::fast_io::install_path get_module_install_path_from_argv0(char const *ar
120164
return ret;
121165
}
122166
} // end for
123-
throw_posix_error();
167+
throw_posix_error(EINVAL);
124168

125169
} // end else
126170
}

include/fast_io_driver/install_path/bsd.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ inline ::fast_io::install_path get_module_install_path()
3636
#endif
3737
::std::size_t size{PATH_MAX};
3838

39-
if (auto status{::fast_io::noexcept_call(::sysctl, mib, 4, buffer1, __builtin_addressof(size), nullptr, 0)}; !status) [[unlikely]]
39+
if (auto status{::fast_io::noexcept_call(::sysctl, mib, 4, buffer1, __builtin_addressof(size), nullptr, 0)}; status == -1) [[unlikely]]
4040
{
41-
throw_posix_error(status);
41+
throw_posix_error();
4242
}
4343

4444
resolved = ::fast_io::noexcept_call(::realpath, buffer1, buffer2);
4545

4646
if (!resolved) [[unlikely]]
4747
{
48-
throw_posix_error(resolved);
48+
throw_posix_error();
4949
}
5050

5151
::fast_io::install_path ret;

include/fast_io_driver/install_path/darwin.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ inline ::fast_io::install_path get_module_install_path()
3030
::std::uint_least32_t size{PATH_MAX};
3131
if (::fast_io::noexcept_call(::_NSGetExecutablePath, buffer, __builtin_addressof(size)) == -1) [[unlikely]]
3232
{
33-
throw_posix_error();
33+
if (size > PATH_MAX)
34+
{
35+
throw_posix_error(ERANGE);
36+
}
37+
else
38+
{
39+
throw_posix_error(EINVAL);
40+
}
3441
}
3542
char buffer2[PATH_MAX + 1];
3643
char *resolved{::fast_io::noexcept_call(::realpath, buffer, buffer2)};

include/fast_io_driver/install_path/linux.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,19 @@ namespace fast_io::details
1919
#endif
2020
inline ::fast_io::install_path get_module_install_path()
2121
{
22-
char buffer[PATH_MAX + 1];
22+
constexpr ::std::size_t path_max{::std::max<::std::size_t>(PATH_MAX, 4096u)};
23+
24+
char buffer[path_max + 1];
2325
::fast_io::install_path ret;
2426

27+
using my_ssize_t = ::std::make_signed_t<::std::size_t>;
2528
#if defined(__linux__) && defined(__NR_readlink)
26-
auto resolved{::fast_io::system_call<__NR_readlink, int>("/proc/self/exe", buffer, PATH_MAX)};
29+
auto resolved{::fast_io::system_call<__NR_readlink, my_ssize_t>("/proc/self/exe", buffer, path_max)};
2730
system_call_throw_error(resolved);
31+
if (static_cast<::std::size_t>(resolved) >= path_max)
32+
{
33+
throw_posix_error(ERANGE);
34+
}
2835
buffer[resolved] = '\0';
2936
ret.module_name = ::fast_io::u8concat_fast_io(::fast_io::mnp::code_cvt(::fast_io::mnp::os_c_str_with_known_size(buffer, resolved)));
3037
#else

include/fast_io_driver/install_path/null.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ namespace fast_io::details
1111
[[noreturn]]
1212
inline void get_module_install_path()
1313
{
14-
throw_posix_error();
14+
throw_posix_error(ENOTSUP);
1515
}
1616
} // namespace fast_io::details

0 commit comments

Comments
 (0)