Skip to content

Commit

Permalink
Attempt at addressing issue #1, release 0.9.1
Browse files Browse the repository at this point in the history
I've audited every single use of std::terminate( )/abort( ) (this is why
I keep the codebase small!), an ensured that at the very least, some
form of error message is displayed before any termination, no matter how
unexpected.

Also fixed my own idiotic use of `void displayWindowsMsgBoxWithMessage(
PCWSTR )` whereby I'd construct a std::wstring from a (wide) string
literal, then move it into `void displayWindowsMsgBoxWithMessage( const
std::wstring )`. What the hell was I doing??

Lastly, I disabled AVX generation, and now uses the SSE2 option for 32
bit builds, and the DEFAULT option for 64 bit builds. All 64 bit
processors have SSE2 built in, and MSVC doesn't even recognize
`/arch:SSE2` when building a 64 bit executable.

Hopefully this addresses the issue!
  • Loading branch information
ariccio committed Jan 22, 2015
1 parent e264801 commit faa5e18
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 45 deletions.
24 changes: 22 additions & 2 deletions WinDirStat/windirstat/SelectDrivesDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,15 +383,34 @@ void CSelectDrivesDlg::buildSelectList( ) {



const rsize_t drive_name_buffer_size = MAX_PATH;
const rsize_t drive_name_buffer_size = ( MAX_PATH * 2 );
wchar_t drive_name_buffer[ drive_name_buffer_size ] = { 0 };
rsize_t chars_remaining = 0;
const HRESULT fmt_res = StringCchPrintfExW( drive_name_buffer, drive_name_buffer_size, NULL, &chars_remaining, 0, L"%c:\\", ( i + _T( 'A' ) ) );
//const HRESULT fmt_res = StringCchPrintfW( drive_name_buffer, drive_name_buffer_size, L"%c:\\", ( i + _T( 'A' ) ) );
ASSERT( SUCCEEDED( fmt_res ) );
if ( !SUCCEEDED( fmt_res ) ) {
if ( fmt_res == STRSAFE_E_INSUFFICIENT_BUFFER ) {
displayWindowsMsgBoxWithMessage( L"CSelectDrivesDlg::buildSelectList failed!!! STRSAFE_E_INSUFFICIENT_BUFFER!!" );
std::terminate( );
}
if ( fmt_res == STRSAFE_E_INVALID_PARAMETER ) {
displayWindowsMsgBoxWithMessage( L"CSelectDrivesDlg::buildSelectList failed!!! STRSAFE_E_INVALID_PARAMETER!!" );
std::terminate( );
}
if ( fmt_res == STRSAFE_E_END_OF_FILE ) {
displayWindowsMsgBoxWithMessage( L"CSelectDrivesDlg::buildSelectList failed!!! STRSAFE_E_END_OF_FILE!!" );
std::terminate( );
}
else {
displayWindowsMsgBoxWithMessage( L"CSelectDrivesDlg::buildSelectList failed!!! (unknown error)" );
std::terminate( );
}
displayWindowsMsgBoxWithMessage( L"Unintended execution in CSelectDrivesDlg::buildSelectList!!! (anyways, there's an unknown error)" );
std::terminate( );
abort( );

//shut `/analyze` up.
return;
}

const rsize_t drive_name_length = ( drive_name_buffer_size - chars_remaining );
Expand Down Expand Up @@ -547,6 +566,7 @@ _Pre_defensive_ void CSelectDrivesDlg::OnOK( ) {
for ( INT i = 0; i < m_list.GetItemCount( ); i++ ) {
const auto item = m_list.GetItem( i );
if ( item == NULL ) {
displayWindowsMsgBoxWithMessage( L"Error in CSelectDrivesDlg::OnOK: item == NULL (aborting)" );
std::terminate( );
//`/analyze` is confused.
return;
Expand Down
3 changes: 2 additions & 1 deletion WinDirStat/windirstat/TreeListControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ _Ret_notnull_ CTreeListControl* CTreeListItem::GetTreeListControl( ) {
const auto tlc = CTreeListControl::GetTheTreeListControl( );
ASSERT( tlc != NULL );
if ( tlc == NULL ) {
displayWindowsMsgBoxWithMessage( L"Serious error in CTreeListItem::GetTreeListControl: tlc == NULL, This should never happen!(aborting)" );
//throw std::logic_error( "This should never happen!" );
std::terminate( );

Expand Down Expand Up @@ -501,7 +502,7 @@ void CTreeListControl::OnContextMenu( CWnd* /*pWnd*/, CPoint pt ) {
const auto thisHeader = GetHeaderCtrl( );
auto rc = GetWholeSubitemRect( i, 0, thisHeader );
if ( item == NULL ) {
displayWindowsMsgBoxWithMessage( std::move( std::wstring( L"GetItem returned NULL!" ) ) );
displayWindowsMsgBoxWithMessage( L"GetItem returned NULL!" );
return;
}
/*
Expand Down
49 changes: 44 additions & 5 deletions WinDirStat/windirstat/globalhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,21 @@ namespace {
const HRESULT err_res = CStyle_GetLastErrorAsFormattedMessage( psz_formatted_datetime, strSize, chars_written );
if ( !SUCCEEDED( err_res ) ) {
TRACE( _T( "Error in CStyle_GetLastErrorAsFormattedMessage!!\r\n" ) );
displayWindowsMsgBoxWithMessage( std::wstring( L"Error in CStyle_GetLastErrorAsFormattedMessage!!\r\n" ) );
displayWindowsMsgBoxWithMessage( L"Error in CStyle_GetLastErrorAsFormattedMessage!!\r\n" );
return err_res;
}
return E_FAIL;
}

void ensure_valid_return_date( const int gdfres, const rsize_t strSize ) {
if ( !( ( gdfres + 1 ) < static_cast< std::int64_t >( strSize ) ) ) {
displayWindowsMsgBoxWithMessage( L"Error in ensure_valid_return_date!(aborting)" );
std::wstring err_str( L"DEBUGGING INFO: strSize: " );
err_str += std::to_wstring( strSize );
err_str += L", gdfres: ";
err_str += std::to_wstring( gdfres );
err_str += L".";
displayWindowsMsgBoxWithMessage( err_str.c_str( ) );
std::terminate( );
}
if ( gdfres == 0 ) {
Expand All @@ -174,6 +181,13 @@ namespace {

void ensure_valid_return_time( const int gtfres, const rsize_t strSize ) {
if ( !( ( gtfres + 1 ) < static_cast< std::int64_t >( strSize ) ) ) {
displayWindowsMsgBoxWithMessage( L"Error in ensure_valid_return_time!(aborting)" );
std::wstring err_str( L"DEBUGGING INFO: strSize: " );
err_str += std::to_wstring( strSize );
err_str += L", gtfres: ";
err_str += std::to_wstring( gtfres );
err_str += L".";
displayWindowsMsgBoxWithMessage( err_str.c_str( ) );
std::terminate( );
}
if ( gtfres == 0 ) {
Expand Down Expand Up @@ -578,6 +592,28 @@ _Success_( SUCCEEDED( return ) ) HRESULT CStyle_GetNumberFormatted( const std::i

const HRESULT strsafe_printf_res = StringCchPrintfExW( number_str_buffer, bufSize, NULL, &chars_remaining, 0, L"%I64d", number );
if ( !SUCCEEDED( strsafe_printf_res ) ) {

if ( strsafe_printf_res == STRSAFE_E_INSUFFICIENT_BUFFER ) {
displayWindowsMsgBoxWithMessage( L"STRSAFE_E_INSUFFICIENT_BUFFER in CStyle_GetNumberFormatted!(aborting)" );
}
if ( strsafe_printf_res == STRSAFE_E_END_OF_FILE ) {
displayWindowsMsgBoxWithMessage( L"STRSAFE_E_END_OF_FILE in CStyle_GetNumberFormatted!(aborting)" );
}
if ( strsafe_printf_res == STRSAFE_E_INVALID_PARAMETER ) {
displayWindowsMsgBoxWithMessage( L"STRSAFE_E_INVALID_PARAMETER in CStyle_GetNumberFormatted!(aborting)" );
}
else {
displayWindowsMsgBoxWithMessage( L"Unknown error in CStyle_GetNumberFormatted!(aborting)" );
}


std::wstring err_str( L"DEBUGGING INFO: bufSize: " );
err_str += std::to_wstring( bufSize );
err_str += L", number: ";
err_str += std::to_wstring( number );
displayWindowsMsgBoxWithMessage( err_str.c_str( ) );


std::terminate( );
}

Expand Down Expand Up @@ -607,9 +643,12 @@ _Success_( SUCCEEDED( return ) ) HRESULT CStyle_GetNumberFormatted( const std::i
return STRSAFE_E_END_OF_FILE;
default:
ASSERT( false );
displayWindowsMsgBoxWithMessage( L"Unexpected error in CStyle_GetNumberFormatted, after GetNumberFormatEx!(aborting)" );
displayWindowsMsgBoxWithError( last_err );
std::terminate( );
}
ASSERT( false );
displayWindowsMsgBoxWithMessage( L"Unintended execution in CStyle_GetNumberFormatted, after GetNumberFormatEx!(aborting!)" );
std::terminate( );
}
ASSERT( get_number_fmt_ex_res > 0 );
Expand Down Expand Up @@ -658,7 +697,7 @@ const HRESULT allocate_and_copy_name_str( _Pre_invalid_ _Post_z_ _Post_readable_
#endif
}
else {
displayWindowsMsgBoxWithMessage( L"Copy failed!!!" );
displayWindowsMsgBoxWithMessage( L"Copy of name_str failed!!!" );
std::terminate( );
}
return res;
Expand Down Expand Up @@ -1208,12 +1247,12 @@ void write_bad_fmt_msg( _Out_writes_z_( 41 ) _Pre_writable_size_( 42 ) _Post_rea
chars_written = 41;
}

void displayWindowsMsgBoxWithError( ) {
void displayWindowsMsgBoxWithError( const DWORD error ) {
const rsize_t err_msg_size = 1024;
wchar_t err_msg[ err_msg_size ] = { 0 };
rsize_t chars_written = 0;

const HRESULT err_res = CStyle_GetLastErrorAsFormattedMessage( err_msg, err_msg_size, chars_written );
const HRESULT err_res = CStyle_GetLastErrorAsFormattedMessage( err_msg, err_msg_size, chars_written, error );
if ( SUCCEEDED( err_res ) ) {
WTL::AtlMessageBox( NULL, err_msg, TEXT( "Error" ), MB_OK );
TRACE( _T( "Error: %s\r\n" ), err_msg );
Expand All @@ -1223,7 +1262,7 @@ void displayWindowsMsgBoxWithError( ) {
const rsize_t err_msg_size_2 = 4096;
wchar_t err_msg_2[ err_msg_size_2 ] = { 0 };
rsize_t chars_written_2 = 0;
const HRESULT err_res_2 = CStyle_GetLastErrorAsFormattedMessage( err_msg_2, err_msg_size_2, chars_written_2 );
const HRESULT err_res_2 = CStyle_GetLastErrorAsFormattedMessage( err_msg_2, err_msg_size_2, chars_written_2, error );
if ( SUCCEEDED( err_res_2 ) ) {
WTL::AtlMessageBox( NULL, err_msg_2, TEXT( "Error" ), MB_OK );
TRACE( _T( "Error: %s\r\n" ), err_msg_2 );
Expand Down
2 changes: 1 addition & 1 deletion WinDirStat/windirstat/globalhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ _Success_( return > 32 ) INT_PTR ShellExecuteWithAssocDialog ( _In_ const HWND

void check8Dot3NameCreationAndNotifyUser( );

void displayWindowsMsgBoxWithError ( );
void displayWindowsMsgBoxWithError ( const DWORD error = GetLastError( ) );

void displayWindowsMsgBoxWithMessage( std::wstring message );

Expand Down
4 changes: 2 additions & 2 deletions WinDirStat/windirstat/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ DOUBLE DoSomeWorkShim( _In_ CItemBranch* const ThisCItem, std::wstring path, _In
ASSERT( debug_buf_res_1 != -1 );
if ( debug_buf_res_1 == -1 ) {
OutputDebugStringW( global_strings::output_dbg_string_error );
std::terminate( );
//std::terminate( );
}

OutputDebugStringW( debug_buf );
Expand All @@ -407,7 +407,7 @@ DOUBLE DoSomeWorkShim( _In_ CItemBranch* const ThisCItem, std::wstring path, _In
ASSERT( debug_buf_res_2 != -1 );
if ( debug_buf_res_2 == -1 ) {
OutputDebugStringW( global_strings::output_dbg_string_error );
std::terminate( );
//std::terminate( );
}

OutputDebugStringW( debug_buf_2 );
Expand Down
8 changes: 4 additions & 4 deletions WinDirStat/windirstat/mainframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ namespace {
//m_open = owner->OpenClipboard( );
if ( !m_open ) {
displayWindowsMsgBoxWithError( );
displayWindowsMsgBoxWithMessage( std::move( std::wstring( L"Cannot open the clipboard." ) ) );
displayWindowsMsgBoxWithMessage( L"Cannot open the clipboard." );
TRACE( _T( "Cannot open the clipboard!\r\n" ) );
}
if ( empty ) {
if ( !EmptyClipboard( ) ) {
displayWindowsMsgBoxWithError( );
displayWindowsMsgBoxWithMessage( std::move( std::wstring( L"Cannot empty the clipboard." ) ) );
displayWindowsMsgBoxWithMessage( L"Cannot empty the clipboard." );
TRACE( _T( "Cannot empty the clipboard!\r\n" ) );
}
}
Expand Down Expand Up @@ -526,7 +526,7 @@ void CMainFrame::CopyToClipboard( _In_ const std::wstring psz ) const {

const auto lp = GlobalLock( h );
if ( lp == NULL ) {
displayWindowsMsgBoxWithMessage( std::move( std::wstring( L"GlobalLock failed!" ) ) );
displayWindowsMsgBoxWithMessage( L"GlobalLock failed!" );
return;
}

Expand Down Expand Up @@ -559,7 +559,7 @@ void CMainFrame::CopyToClipboard( _In_ const std::wstring psz ) const {
if ( GlobalUnlock( h ) == 0 ) {
const auto err = GetLastError( );
if ( err != NO_ERROR ) {
displayWindowsMsgBoxWithMessage( std::move( std::wstring( L"GlobalUnlock failed!" ) ) );
displayWindowsMsgBoxWithMessage( L"GlobalUnlock failed!" );
return;
}
}
Expand Down
6 changes: 4 additions & 2 deletions WinDirStat/windirstat/mountpoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "stdafx.h"

#include "mountpoints.h"

#include "globalhelpers.h"
//#ifdef _DEBUG
//#define new DEBUG_NEW
//#endif
Expand Down Expand Up @@ -57,8 +57,9 @@ void CMountPoints::GetDriveVolumes( ) {
wchar_t volume_[ volumeTCHARsize ] = { 0 };
if ( ( drives bitand mask ) != 0 ) {
const auto swps = swprintf_s( s_, volumeTCHARsize, L"%c:\\", ( i + _T( 'A' ) ) );

if ( swps == -1 ) {
displayWindowsMsgBoxWithMessage( L"unexpected error in CMountPoints::GetDriveVolumes!!(aborting)" );

std::terminate( );
}

Expand Down Expand Up @@ -129,6 +130,7 @@ void CMountPoints::GetAllMountPoints( ) {
}
const auto FindVolumeCloseRes = FindVolumeClose( hvol );
if ( !( FindVolumeCloseRes ) ) {
displayWindowsMsgBoxWithMessage( L"Failed to close a handle in CMountPoints::GetAllMountPoints. Something is wrong!" );
std::terminate( );
}
}
Expand Down
55 changes: 43 additions & 12 deletions WinDirStat/windirstat/nt_kernel_support.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "stdafx.h"
#include "globalhelpers.h"


namespace NativeAPI {
Expand Down Expand Up @@ -392,64 +393,94 @@ namespace NativeAPI {
if ( mod_handle_res == 0 ) {
//failed!
const auto last_err = GetLastError( );
fwprintf( stderr, L"Failed to get handle to ntdll! Err: %lu\r\n", last_err );

TRACE( L"Failed to get handle to ntdll! Err: %lu\r\n", last_err );
displayWindowsMsgBoxWithMessage( L"Failed to get handle to ntdll!" );
displayWindowsMsgBoxWithError( last_err );

//TODO: don't abort!
abort( );
//abort( );
}
const HMODULE ntdll = ntdll_temp;
if ( !NtQueryInformationFile ) {
NtQueryInformationFile = reinterpret_cast<NtQueryInformationFile_t>( GetProcAddress( ntdll, "NtQueryInformationFile" ) );
if ( NtQueryInformationFile == NULL ) {
abort( );
TRACE( L"Failed to get pointer to NtQueryInformationFile!\r\n" );
displayWindowsMsgBoxWithMessage( L"Failed to get pointer to NtQueryInformationFile!" );
//displayWindowsMsgBoxWithError( last_err );

//abort( );
}
}
if ( !NtQueryVolumeInformationFile ) {
NtQueryVolumeInformationFile = reinterpret_cast<NtQueryVolumeInformationFile_t>( GetProcAddress( ntdll, "NtQueryVolumeInformationFile" ) );
if ( NtQueryVolumeInformationFile == NULL ) {
abort( );
TRACE( L"Failed to get pointer to NtQueryVolumeInformationFile!\r\n" );
displayWindowsMsgBoxWithMessage( L"Failed to get pointer to NtQueryVolumeInformationFile!" );

//abort( );
}
}
if ( !NtOpenDirectoryObject ) {
NtOpenDirectoryObject = reinterpret_cast<NtOpenDirectoryObject_t>( GetProcAddress( ntdll, "NtOpenDirectoryObject" ) );
if ( NtOpenDirectoryObject == NULL ) {
abort( );
TRACE( L"Failed to get pointer to NtOpenDirectoryObject!\r\n" );
displayWindowsMsgBoxWithMessage( L"Failed to get pointer to NtOpenDirectoryObject!" );

//abort( );
}
}
if ( !NtOpenFile ) {
NtOpenFile = reinterpret_cast<NtOpenFile_t>( GetProcAddress( ntdll, "NtOpenFile" ) );
if ( NtOpenFile == NULL ) {
abort( );
TRACE( L"Failed to get pointer to NtOpenFile!\r\n" );
displayWindowsMsgBoxWithMessage( L"Failed to get pointer to NtOpenFile!" );

//abort( );
}
}
if ( !NtCreateFile ) {
NtCreateFile = reinterpret_cast<NtCreateFile_t>( GetProcAddress( ntdll, "NtCreateFile" ) );
if ( NtCreateFile == NULL ) {
abort( );
TRACE( L"Failed to get pointer to NtCreateFile!\r\n" );
displayWindowsMsgBoxWithMessage( L"Failed to get pointer to NtCreateFile!" );

//abort( );
}
}
if ( !NtClose ) {
NtClose = reinterpret_cast<NtClose_t>( GetProcAddress( ntdll, "NtClose" ) );
if ( NtClose == NULL ) {
abort( );
TRACE( L"Failed to get pointer to NtClose!\r\n" );
displayWindowsMsgBoxWithMessage( L"Failed to get pointer to NtClose!" );

//abort( );
}
}
if ( !NtQueryDirectoryFile ) {
NtQueryDirectoryFile = reinterpret_cast<NtQueryDirectoryFile_t>( GetProcAddress( ntdll, "NtQueryDirectoryFile" ) );
if ( NtQueryDirectoryFile == NULL ) {
abort( );
TRACE( L"Failed to get pointer to NtQueryDirectoryFile!\r\n" );
displayWindowsMsgBoxWithMessage( L"Failed to get pointer to NtQueryDirectoryFile!" );

//abort( );
}
}
if ( !NtSetInformationFile ) {
NtSetInformationFile = reinterpret_cast<NtSetInformationFile_t>( GetProcAddress( ntdll, "NtSetInformationFile" ) );
if ( NtSetInformationFile == NULL ) {
abort( );
TRACE( L"Failed to get pointer to NtSetInformationFile!\r\n" );
displayWindowsMsgBoxWithMessage( L"Failed to get pointer to NtSetInformationFile!" );

//abort( );
}
}
if ( !NtWaitForSingleObject ) {
NtWaitForSingleObject = reinterpret_cast<NtWaitForSingleObject_t>( GetProcAddress( ntdll, "NtWaitForSingleObject" ) );
if ( NtWaitForSingleObject == NULL ) {
abort( );
TRACE( L"Failed to get pointer to NtWaitForSingleObject!\r\n" );
displayWindowsMsgBoxWithMessage( L"Failed to get pointer to NtWaitForSingleObject!" );

//abort( );
}
}
}
Expand Down
Loading

0 comments on commit faa5e18

Please sign in to comment.