Skip to content

Commit 239cdfd

Browse files
committed
complete non-null term input sanitizing
1 parent 480e589 commit 239cdfd

21 files changed

Lines changed: 74 additions & 67 deletions

src/copy.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ static off_t fs_copy_loop(int const rid, int const wid, off_t const len)
142142
bool fs_copy_file_range_or_loop(std::string_view source, std::string_view dest, bool overwrite)
143143
{
144144
// copy a file in chunks
145-
146-
fd_handle rid(::open(source.data(), O_RDONLY | O_CLOEXEC));
145+
const std::string src(source);
146+
fd_handle rid(::open(src.c_str(), O_RDONLY | O_CLOEXEC));
147147
if (!rid)
148148
return false;
149149

@@ -159,7 +159,8 @@ bool fs_copy_file_range_or_loop(std::string_view source, std::string_view dest,
159159
opt |= O_EXCL;
160160

161161
// https://linux.die.net/man/3/open
162-
fd_handle wid(::open(dest.data(), opt, s.st_mode));
162+
const std::string dst(dest);
163+
fd_handle wid(::open(dst.c_str(), opt, s.st_mode));
163164
if (!wid)
164165
return false;
165166

@@ -234,11 +235,11 @@ bool fs_copy_file(std::string_view source, std::string_view dest, bool overwrite
234235
if(!overwrite)
235236
opts |= COPY_FILE_FAIL_IF_EXISTS;
236237

237-
// https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-copyfileexa
238+
// https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-copyfileexw
238239
// preserves source file attributes
239240

240-
if(CopyFileExW(fs_win32_to_wide(source).data(),
241-
fs_win32_to_wide(dest).data(),
241+
if(CopyFileExW(fs_win32_to_wide(source).c_str(),
242+
fs_win32_to_wide(dest).c_str(),
242243
nullptr, nullptr, FALSE, opts) != 0)
243244
return true;
244245

src/disk.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ std::size_t fs_get_blksize(std::string_view path)
3535
if (root.empty())
3636
return {};
3737

38-
HANDLE h = CreateFileW(fs_win32_to_wide(R"(\\.\)" + root).data(),
38+
HANDLE h = CreateFileW(fs_win32_to_wide(R"(\\.\)" + root).c_str(),
3939
0,
4040
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4141
nullptr, OPEN_EXISTING, 0, nullptr);
@@ -120,9 +120,8 @@ ino_t fs_inode(std::string_view path)
120120
#if defined(HAVE_GETFILEINFORMATIONBYNAME)
121121
// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/ns-ntifs-file_stat_basic_information
122122
FILE_STAT_BASIC_INFORMATION f1;
123-
const auto w1 = fs_win32_to_wide(path);
124123

125-
if (GetFileInformationByName(w1.data(), FileStatBasicByNameInfo, &f1, sizeof(f1)))
124+
if (GetFileInformationByName(fs_win32_to_wide(path).c_str(), FileStatBasicByNameInfo, &f1, sizeof(f1)))
126125
return static_cast<ino_t>(f1.FileId.QuadPart);
127126

128127
#else

src/equivalent.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ static bool fs_win32_equiv(std::string_view path1, std::string_view path2, std::
3838
// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/ns-ntifs-file_stat_basic_information
3939
FILE_STAT_BASIC_INFORMATION f1, f2;
4040

41-
if ( GetFileInformationByName(fs_win32_to_wide(path1).data(), FileStatBasicByNameInfo, &f1, sizeof(f1)) &&
42-
GetFileInformationByName(fs_win32_to_wide(path2).data(), FileStatBasicByNameInfo, &f2, sizeof(f2))) {
41+
if ( GetFileInformationByName(fs_win32_to_wide(path1).c_str(), FileStatBasicByNameInfo, &f1, sizeof(f1)) &&
42+
GetFileInformationByName(fs_win32_to_wide(path2).c_str(), FileStatBasicByNameInfo, &f2, sizeof(f2))) {
4343
return f1.VolumeSerialNumber.QuadPart == f2.VolumeSerialNumber.QuadPart &&
4444
f1.FileId.QuadPart == f2.FileId.QuadPart;
4545
}
@@ -50,14 +50,14 @@ static bool fs_win32_equiv(std::string_view path1, std::string_view path2, std::
5050
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfileinformationbyhandle#remarks
5151
// FILE_FLAG_BACKUP_SEMANTICS to allow opening directories
5252

53-
HANDLE h1 = CreateFileW(fs_win32_to_wide(path1).data(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ, nullptr,
53+
HANDLE h1 = CreateFileW(fs_win32_to_wide(path1).c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ, nullptr,
5454
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
5555
if(h1 == INVALID_HANDLE_VALUE) {
5656
fs_print_error(path1, __func__);
5757
return false;
5858
}
5959

60-
HANDLE h2 = CreateFileW(fs_win32_to_wide(path2).data(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ, nullptr,
60+
HANDLE h2 = CreateFileW(fs_win32_to_wide(path2).c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ, nullptr,
6161
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
6262
if(h2 == INVALID_HANDLE_VALUE) {
6363
fs_print_error(path2, __func__);

src/executable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ bool fs_is_executable_binary(std::string_view path)
4646
#if defined(_WIN32)
4747
// MinGW, MSVC, oneAPI at least need || is_appexec_alias()
4848
DWORD t;
49-
ok = (GetBinaryTypeW(fs_win32_to_wide(path).data(), &t) != 0) || fs_is_appexec_alias(path);
49+
ok = (GetBinaryTypeW(fs_win32_to_wide(path).c_str(), &t) != 0) || fs_is_appexec_alias(path);
5050
#else
5151
// https://github.com/jart/cosmopolitan/blob/master/ape/specification.md
5252
std::array<std::uint8_t, 4> magic;

src/extra/component.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ std::string_view::size_type fs_max_component(std::string_view path)
3131

3232
#if defined(_WIN32)
3333
DWORD L = 0;
34-
if(GetVolumeInformationW(fs_win32_to_wide(fs_root(path)).data(), nullptr, 0, nullptr, &L, nullptr, nullptr, 0) != 0)
34+
if(GetVolumeInformationW(fs_win32_to_wide(fs_root(path)).c_str(), nullptr, 0, nullptr, &L, nullptr, nullptr, 0) != 0)
3535
return L;
3636
#elif defined(_PC_NAME_MAX)
3737
errno = 0;
38-
auto const r = pathconf(path.data(), _PC_NAME_MAX);
38+
const std::string cpath(path);
39+
auto const r = pathconf(cpath.c_str(), _PC_NAME_MAX);
3940
if(r != -1)
4041
return r;
42+
4143
if(errno == 0)
4244
return DEFAULT_MAX_PATH;
4345
#elif defined(NAME_MAX)

src/extra/cygwin.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ constexpr int CCP_POSIX_TO_WIN_A = 0;
1414
static std::string fs_convert_path(std::string_view path, [[maybe_unused]] int const what)
1515
{
1616
#ifdef __CYGWIN__
17-
const auto L = cygwin_conv_path(what, path.data(), nullptr, 0);
17+
const std::string cpath(path);
18+
const auto L = cygwin_conv_path(what, cpath.c_str(), nullptr, 0);
1819
if(L > 0){
1920
std::string r;
2021
r.resize(L);
2122

22-
if (!cygwin_conv_path(what, path.data(), r.data(), L))
23+
if (!cygwin_conv_path(what, cpath.c_str(), r.data(), L))
2324
return r;
2425
}
2526
#endif

src/extra/exepath.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ std::string fs_exe_path()
4646
}
4747
#elif defined(__linux__) || defined(__CYGWIN__)
4848
// https://man7.org/linux/man-pages/man2/readlink.2.html
49-
std::string_view exe = "/proc/self/exe";
49+
constexpr std::string exe = "/proc/self/exe";
5050
std::string p;
5151
p.resize(fs_symlink_length(exe));
5252

53-
if(ssize_t L = readlink(exe.data(), p.data(), p.size()); L > 0) {
53+
if(ssize_t L = ::readlink(exe.c_str(), p.data(), p.size()); L > 0) {
5454
p.resize(L);
5555
return p;
5656
}

src/extra/owner.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ static std::string fs_win32_owner(std::string_view path, bool group)
5656
PSECURITY_DESCRIPTOR pSD = nullptr;
5757
PSID pSid = nullptr;
5858
DWORD r;
59+
const std::string cpath(path);
5960
// https://learn.microsoft.com/en-us/windows/win32/api/aclapi/nf-aclapi-getnamedsecurityinfoa
6061
if (group)
61-
r = GetNamedSecurityInfoA(path.data(), SE_FILE_OBJECT, GROUP_SECURITY_INFORMATION, nullptr, &pSid, nullptr, nullptr, &pSD);
62+
r = GetNamedSecurityInfoA(cpath.c_str(), SE_FILE_OBJECT, GROUP_SECURITY_INFORMATION, nullptr, &pSid, nullptr, nullptr, &pSD);
6263
else
63-
r = GetNamedSecurityInfoA(path.data(), SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, &pSid, nullptr, nullptr, nullptr, &pSD);
64+
r = GetNamedSecurityInfoA(cpath.c_str(), SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, &pSid, nullptr, nullptr, nullptr, &pSD);
6465

6566
std::string s;
6667
if(r == ERROR_SUCCESS)
@@ -77,16 +78,17 @@ static std::string fs_win32_owner(std::string_view path, bool group)
7778
static std::optional<uid_t> fs_stat_uid(std::string_view path)
7879
{
7980
int r = 0;
81+
const std::string cpath(path);
8082

8183
#if defined(HAVE_STATX)
8284
struct statx sx;
83-
r = ::statx(AT_FDCWD, path.data(), AT_NO_AUTOMOUNT, STATX_UID, &sx);
85+
r = ::statx(AT_FDCWD, cpath.c_str(), AT_NO_AUTOMOUNT, STATX_UID, &sx);
8486
if (r == 0)
8587
return sx.stx_uid;
8688
#endif
8789

8890
if(r == 0 || errno == ENOSYS){
89-
if(struct stat s; !::stat(path.data(), &s))
91+
if(struct stat s; !::stat(cpath.c_str(), &s))
9092
return s.st_uid;
9193
}
9294

@@ -96,15 +98,17 @@ static std::optional<uid_t> fs_stat_uid(std::string_view path)
9698
static std::optional<gid_t> fs_stat_gid(std::string_view path)
9799
{
98100
int r = 0;
101+
const std::string cpath(path);
102+
99103
#if defined(HAVE_STATX)
100104
struct statx sx;
101-
r = ::statx(AT_FDCWD, path.data(), AT_NO_AUTOMOUNT, STATX_GID, &sx);
105+
r = ::statx(AT_FDCWD, cpath.c_str(), AT_NO_AUTOMOUNT, STATX_GID, &sx);
102106
if (r == 0)
103107
return sx.stx_gid;
104108
#endif
105109

106110
if(r == 0 || errno == ENOSYS){
107-
if(struct stat s; !::stat(path.data(), &s))
111+
if(struct stat s; !::stat(cpath.c_str(), &s))
108112
return s.st_gid;
109113
}
110114

src/extra/removable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ fs_is_removable(std::string_view path)
3030
// is path a removable device like a USB stick or SD card or CD-ROM, DVD, Blu-ray
3131
// not a fixed disk like a hard drive or SSD
3232
#if defined(_WIN32)
33-
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getdrivetypea
33+
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getdrivetypew
3434

35-
UINT t = GetDriveTypeW(fs_win32_to_wide(fs_root(path)).data());
35+
UINT t = GetDriveTypeW(fs_win32_to_wide(fs_root(path)).c_str());
3636
switch (t)
3737
{
3838
case DRIVE_REMOVABLE:

src/inquire.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace Filesystem = std::filesystem;
3939
static bool fs_win32_is_type(std::string_view path, const DWORD type){
4040

4141
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
42-
HANDLE h = CreateFileW(fs_win32_to_wide(path).data(),
42+
HANDLE h = CreateFileW(fs_win32_to_wide(path).c_str(),
4343
0,
4444
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4545
nullptr, OPEN_EXISTING, 0, nullptr);
@@ -131,7 +131,7 @@ fs_exists(std::string_view path)
131131

132132
// WIN32_FILE_ATTRIBUTE_DATA fad;
133133

134-
// ok = GetFileAttributesExW(fs_win32_to_wide(path).data(), GetFileExInfoStandard, &fad);
134+
// ok = GetFileAttributesExW(fs_win32_to_wide(path).c_str(), GetFileExInfoStandard, &fad);
135135
// if (!ok){
136136
// DWORD err = GetLastError();
137137
// if (err != ERROR_FILE_NOT_FOUND && err != ERROR_PATH_NOT_FOUND)
@@ -167,7 +167,7 @@ fs_is_dir(std::string_view path)
167167
// this also works for Symlinks to directories
168168
WIN32_FILE_ATTRIBUTE_DATA fad;
169169

170-
ok = GetFileAttributesExW(fs_win32_to_wide(path).data(), GetFileExInfoStandard, &fad) &&
170+
ok = GetFileAttributesExW(fs_win32_to_wide(path).c_str(), GetFileExInfoStandard, &fad) &&
171171
(fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
172172
#else
173173
ok = S_ISDIR(fs_st_mode(path));

0 commit comments

Comments
 (0)