Skip to content

Commit

Permalink
Revert "Correct usage of FILE_NAME_INFO instead of MY_FILE_NAME_INFO"
Browse files Browse the repository at this point in the history
This reverts commit c56cc13.
  • Loading branch information
agauniyal committed Feb 14, 2018
1 parent bc1a34b commit a57fae0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 48 deletions.
92 changes: 45 additions & 47 deletions include/rang.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,59 +165,57 @@ namespace rang_implementation {

#ifdef OS_WIN

inline bool isMsysPty(int fd) noexcept
{
// Dynamic load for binary compability with old Windows
const auto ptrGetFileInformationByHandleEx
= reinterpret_cast<decltype(&GetFileInformationByHandleEx)>(
GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),
"GetFileInformationByHandleEx"));
if (!ptrGetFileInformationByHandleEx) {
return false;
}

HANDLE h = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
if (h == INVALID_HANDLE_VALUE) {
return false;
}
inline bool isMsysPty(int fd) noexcept
{
// Dynamic load for binary compability with old Windows
const auto ptrGetFileInformationByHandleEx
= reinterpret_cast<decltype(&GetFileInformationByHandleEx)>(
GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),
"GetFileInformationByHandleEx"));
if (!ptrGetFileInformationByHandleEx) {
return false;
}

// Check that it's a pipe:
if (GetFileType(h) != FILE_TYPE_PIPE) {
return false;
}
HANDLE h = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
if (h == INVALID_HANDLE_VALUE) {
return false;
}

// See 'Struct Hack' C API for more information
const size_t sizeNameInfo = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * MAX_PATH;
// Check that it's a pipe:
if (GetFileType(h) != FILE_TYPE_PIPE) {
return false;
}

// std::malloc returns an aligned address with alignment at least as strict as max_align_t.
auto pNameInfo = std::unique_ptr<FILE_NAME_INFO, decltype(&std::free)>(
static_cast<FILE_NAME_INFO*>(std::malloc(sizeNameInfo)), &std::free);
// POD type is binary compatible with FILE_NAME_INFO from WinBase.h
// It have the same alignment and used to avoid UB in caller code
struct MY_FILE_NAME_INFO {
DWORD FileNameLength;
WCHAR FileName[MAX_PATH];
};

if (!pNameInfo) {
return false;
}

// Using 'placement new' to initialize object been used:
new (pNameInfo.get()) FILE_NAME_INFO();
// Manually initialize last member FileName:
std::uninitialized_fill(pNameInfo->FileName,pNameInfo->FileName +
(sizeNameInfo - offsetof(FILE_NAME_INFO,FileName))
/ sizeof(WCHAR),WCHAR());

// Check pipe name is template of
// {"cygwin-","msys-"}XXXXXXXXXXXXXXX-ptyX-XX
if (!ptrGetFileInformationByHandleEx(h, FileNameInfo, pNameInfo.get(), sizeNameInfo)) {
return false;
}
std::wstring name(pNameInfo->FileName, pNameInfo->FileNameLength / sizeof(WCHAR));
if ((name.find(L"msys-") == std::wstring::npos
&& name.find(L"cygwin-") == std::wstring::npos)
|| name.find(L"-pty") == std::wstring::npos) {
return false;
}
auto pNameInfo = std::unique_ptr<MY_FILE_NAME_INFO>(
new (std::nothrow) MY_FILE_NAME_INFO());
if (!pNameInfo) {
return false;
}

return true;
}
// Check pipe name is template of
// {"cygwin-","msys-"}XXXXXXXXXXXXXXX-ptyX-XX
if (ptrGetFileInformationByHandleEx(h, FileNameInfo, pNameInfo.get(),
sizeof(MY_FILE_NAME_INFO))) {
std::wstring name(pNameInfo->FileName, pNameInfo->FileNameLength);
if ((name.find(L"msys-") == std::wstring::npos
&& name.find(L"cygwin-") == std::wstring::npos)
|| name.find(L"-pty") == std::wstring::npos) {
return false;
}
} else {
return false;
}

return true;
}

#endif

Expand Down
2 changes: 1 addition & 1 deletion test/colorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using namespace std;
using namespace rang;

void printHeading(string heading)
void printHeading(string &heading)
{
cout << '\n'
<< style::reset << heading << style::reset << bg::reset << fg::reset
Expand Down

0 comments on commit a57fae0

Please sign in to comment.