Skip to content

Commit 13e051d

Browse files
committed
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.
1 parent 6315171 commit 13e051d

File tree

24 files changed

+467
-187
lines changed

24 files changed

+467
-187
lines changed

include/fast_io_driver/install_path/argv0.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ inline ::fast_io::install_path get_module_install_path_from_argv0(char const *ar
2020
{
2121
if (!argv0) [[unlikely]]
2222
{
23-
throw_posix_error();
23+
throw_posix_error(EINVAL);
2424
}
2525

2626
::fast_io::install_path ret;
@@ -36,8 +36,8 @@ inline ::fast_io::install_path get_module_install_path_from_argv0(char const *ar
3636
char path_list_separator[8] = ":"; // could be ":; "
3737
if (argv0[0] == path_separator)
3838
{
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)
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)
4141
{
4242
newpath[PATH_MAX - 1] = 0;
4343
ret.module_name = ::fast_io::u8concat_fast_io(::fast_io::mnp::code_cvt_os_c_str(newpath));
@@ -57,16 +57,16 @@ inline ::fast_io::install_path get_module_install_path_from_argv0(char const *ar
5757
}
5858
else
5959
{
60-
throw_posix_error(status);
60+
throw_posix_error();
6161
}
6262
}
6363
else if (__builtin_strchr(argv0, static_cast<int>(path_separator)))
6464
{
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)
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)
7070
{
7171
newpath[PATH_MAX - 1] = 0;
7272
ret.module_name = ::fast_io::u8concat_fast_io(::fast_io::mnp::code_cvt_os_c_str(newpath));
@@ -93,15 +93,15 @@ inline ::fast_io::install_path get_module_install_path_from_argv0(char const *ar
9393
{
9494
char *saveptr;
9595
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))
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))
9999
{
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);
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);
103103
[[maybe_unused]] auto const unused1{::realpath(newpath2, newpath)};
104-
if (!::fast_io::noexcept_call(access, newpath, F_OK))
104+
if (!::fast_io::noexcept_call(::access, newpath, F_OK))
105105
{
106106
newpath[PATH_MAX - 1] = 0;
107107
ret.module_name = ::fast_io::u8concat_fast_io(::fast_io::mnp::code_cvt_os_c_str(newpath));
@@ -120,7 +120,7 @@ inline ::fast_io::install_path get_module_install_path_from_argv0(char const *ar
120120
return ret;
121121
}
122122
} // end for
123-
throw_posix_error();
123+
throw_posix_error(EINVAL);
124124

125125
} // end else
126126
}

include/fast_io_driver/install_path/bsd.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ inline ::fast_io::install_path get_module_install_path()
3838

3939
if (auto status{::fast_io::noexcept_call(::sysctl, mib, 4, buffer1, __builtin_addressof(size), nullptr, 0)}; !status) [[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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ inline ::fast_io::install_path get_module_install_path()
2222
char buffer[PATH_MAX + 1];
2323
::fast_io::install_path ret;
2424

25+
using my_ssize_t = ::std::make_signed_t<::std::size_t>;
2526
#if defined(__linux__) && defined(__NR_readlink)
26-
auto resolved{::fast_io::system_call<__NR_readlink, int>("/proc/self/exe", buffer, PATH_MAX)};
27+
auto resolved{::fast_io::system_call<__NR_readlink, my_ssize_t>("/proc/self/exe", buffer, PATH_MAX)};
2728
system_call_throw_error(resolved);
2829
buffer[resolved] = '\0';
2930
ret.module_name = ::fast_io::u8concat_fast_io(::fast_io::mnp::code_cvt(::fast_io::mnp::os_c_str_with_known_size(buffer, resolved)));

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

include/fast_io_driver/install_path/openbsd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ inline ::fast_io::install_path get_module_install_path()
4343

4444
if (size > PATH_MAX) [[unlikely]]
4545
{
46-
throw_posix_error();
46+
throw_posix_error(ERANGE);
4747
}
4848

4949
if (::fast_io::noexcept_call(::sysctl, mib, 4, argv, __builtin_addressof(size), nullptr, 0) != 0) [[unlikely]]

include/fast_io_dsal/impl/tuple.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ struct tuple<>
9292
template <typename... Args>
9393
tuple(Args &&...) -> tuple<Args...>;
9494

95+
// ADL get
9596
template <::std::size_t I, typename... Args>
9697
FAST_IO_GNU_ALWAYS_INLINE
9798
[[nodiscard]]

include/fast_io_dsal/tuple.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ using ::fast_io::containers::tuple;
1414
using ::fast_io::containers::get;
1515
using ::fast_io::containers::is_tuple;
1616
using ::fast_io::containers::forward_as_tuple;
17+
using ::fast_io::containers::apply;
1718

19+
using ::std::tuple_element;
20+
using ::std::tuple_size;
1821
}
1922

2023
#include "impl/misc/pop_macros.h"

include/fast_io_hosted/file_loaders/allocation_file_loader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ inline allocation_file_loader_ret allocation_load_address_impl(int fd)
141141
inline void rewind_allocation_file_loader(int fd)
142142
{
143143
#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__WINE__) && !defined(__BIONIC__)
144-
auto seekret = ::fast_io::noexcept_call(_lseeki64, fd, 0, 0);
144+
auto seekret = ::fast_io::noexcept_call(::_lseeki64, fd, 0, 0);
145145
#else
146146
#if defined(_LARGEFILE64_SOURCE)
147147
auto seekret = ::fast_io::noexcept_call(::lseek64, fd, 0, 0);

include/fast_io_hosted/file_loaders/file_size.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,25 @@ inline ::std::size_t posix_loader_get_file_size(int fd)
133133
}
134134
return static_cast<::std::size_t>(statxbuf.stx_size);
135135
#else
136+
136137
#if defined(__linux__) && !defined(__MLIBC_O_CLOEXEC)
137138
struct stat64 st;
138139
#else
139140
struct stat st;
140141
#endif
141-
if (
142+
143+
if (::fast_io::noexcept_call(
142144
#if defined(__linux__) && !defined(__MLIBC_O_CLOEXEC)
143-
fstat64
145+
::fstat64
144146
#else
145-
fstat
147+
::fstat
146148
#endif
147-
(fd, __builtin_addressof(st)) < 0)
149+
,
150+
fd, __builtin_addressof(st)) == -1) [[unlikely]]
151+
{
148152
throw_posix_error();
153+
}
154+
149155
using st_size_unsigned_type = ::std::make_unsigned_t<decltype(st.st_size)>;
150156
if constexpr (sizeof(st_size_unsigned_type) > sizeof(::std::size_t))
151157
{

0 commit comments

Comments
 (0)