Skip to content

Commit

Permalink
Merge pull request #191 from RamonUnch/prefer-malloc/free-over-new/de…
Browse files Browse the repository at this point in the history
…lete-because-of-null-checks

prefer malloc/free over new/delete because of null checks
  • Loading branch information
RamonUnch authored Jul 13, 2024
2 parents a269149 + f2ed184 commit fc4043a
Show file tree
Hide file tree
Showing 31 changed files with 197 additions and 164 deletions.
10 changes: 6 additions & 4 deletions GpMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,12 +914,12 @@ void GreenPadWnd::on_drop( HDROP hd )
// Get length of the i string for array size.
UINT len = ::myDragQueryFile( hd, i, NULL, 0)+1;
len = Max(len, (UINT)MAX_PATH); // ^ the Above may fail on NT3.1
TCHAR *str = new TCHAR [len];
TCHAR *str = (TCHAR *)malloc( sizeof(TCHAR) * len );
if( str )
{
::myDragQueryFile( hd, i, str, len );
Open( str, AutoDetect );
delete [] str;
free( str );
}
}
::DragFinish( hd );
Expand Down Expand Up @@ -1057,7 +1057,7 @@ void GreenPadWnd::on_external_exe_start(const Path &g)
d = Path(Path::Cur);

String fcmd;
for( int i=0, e=g.len(); i<e; ++i )
for( size_t i=0, e=g.len(); i<e; ++i )
{
if( g[i]==TEXT('%') )
{
Expand Down Expand Up @@ -1598,7 +1598,7 @@ BOOL GreenPadWnd::PostMsgToAllFriends(UINT msg)
bool GreenPadWnd::OpenByMyself( const ki::Path& fn, int cs, bool needReConf, bool always )
{
//MsgBox(fn.c_str(), TEXT("File:"), 0);
LOGGERS( fn );
LOGGERS( fn.c_str() );
// ファイルを開けなかったらそこでおしまい。
TextFileR tf(cs);

Expand Down Expand Up @@ -1727,7 +1727,9 @@ bool GreenPadWnd::OpenByMyself( const ki::Path& fn, int cs, bool needReConf, boo
edit_.getDoc().ClearAll();
stb_.SetText( RzsString(IDS_LOADING).c_str() );
old_filetime_ = filename_.getLastWriteTime(); // Save timestamp
//::EnableWindow(edit_.hwnd(), FALSE);
edit_.getDoc().OpenFile( tf );
//::EnableWindow(edit_.hwnd(), TRUE);
stb_.SetText( TEXT("(1,1)") );

// タイトルバー更新
Expand Down
8 changes: 4 additions & 4 deletions NSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ template<class ComparisonPolicy> class BMSearch
BMSearch( const unicode* key )
: keylen_( my_lstrlenW(key) )
{
key_ = new unicode[keylen_+1];
key_ = (unicode *)malloc( sizeof(unicode) * (keylen_+1) );
if( !key_ ) return;
my_lstrcpyW( key_, key );

Expand All @@ -58,7 +58,7 @@ template<class ComparisonPolicy> class BMSearch

~BMSearch()
{
delete [] key_;
free( key_ );
}

int Search( const unicode* str, int strlen )
Expand Down Expand Up @@ -102,7 +102,7 @@ template<class ComparisonPolicy> class BMSearchRev
BMSearchRev( const unicode* key )
: keylen_( my_lstrlenW(key) )
{
key_ = new unicode[keylen_+1];
key_ = (unicode *)malloc( sizeof(unicode) * (keylen_+1) );
if( !key_ ) return;

my_lstrcpyW( key_, key );
Expand All @@ -113,7 +113,7 @@ template<class ComparisonPolicy> class BMSearchRev

~BMSearchRev()
{
delete [] key_;
free( key_ );
}

int Search( const unicode* str, int strlen )
Expand Down
2 changes: 1 addition & 1 deletion OpenSaveDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ ki::aarr<TCHAR> OpenFileDlg::ConnectWithNull( const TCHAR *lst[], size_t num )
for( size_t i=0; i<num; ++i )
TtlLen += (my_lstrlen(lst[i]) + 1);

aarr<TCHAR> a( new TCHAR[TtlLen] );
aarr<TCHAR> a( TtlLen );

TCHAR* p = a.get();
if( p )
Expand Down
6 changes: 3 additions & 3 deletions OpenSaveDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class OpenFileDlg
int csi() const;

public:
static ki::aarr<TCHAR> ConnectWithNull( const TCHAR *lst[], int num );
static ki::aarr<TCHAR> ConnectWithNull( const TCHAR *lst[], size_t num );

private:
const CharSetList& csl_;
Expand Down Expand Up @@ -118,7 +118,7 @@ class SaveFileDlg
int lb() const;

public:
static ki::aarr<TCHAR> ConnectWithNull( const TCHAR *lst[], int num );
static ki::aarr<TCHAR> ConnectWithNull( const TCHAR *lst[], size_t num );

private:
const CharSetList& csl_;
Expand Down Expand Up @@ -149,7 +149,7 @@ inline int SaveFileDlg::csi() const
inline int SaveFileDlg::lb() const
{ return lb_; }

inline ki::aarr<TCHAR> SaveFileDlg::ConnectWithNull( const TCHAR *lst[], int num )
inline ki::aarr<TCHAR> SaveFileDlg::ConnectWithNull( const TCHAR *lst[], size_t num )
{ return OpenFileDlg::ConnectWithNull( lst, num ); }


Expand Down
14 changes: 7 additions & 7 deletions RSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class RegLexer
public:
RegLexer( const wchar_t* pat, ulong len );
RegToken GetToken();
wchar_t GetChar() { return chr_; }
wchar_t GetChar() const { return chr_; }

private:
const wchar_t* pat_;
Expand Down Expand Up @@ -175,7 +175,7 @@ class RegParser: public Object
public:
RegParser( const unicode* pat );
~RegParser() { delete root_; }
RegNode* root() { return root_; }
RegNode* root() const { return root_; }
bool err() { return err_; }
bool isHeadType() const { return isHeadType_; }
bool isTailType() const { return isTailType_; }
Expand Down Expand Up @@ -421,15 +421,15 @@ struct RegTrans : public Object
return false;
}
*/
bool match_c( wchar_t c )
bool match_c( wchar_t c ) const
{
for( RegClass* p=cls.get(); p; p=p->next.get() )
if( p->range.stt<=c && c<=p->range.end )
return true;
return false;
}

bool match_i( wchar_t c )
bool match_i( wchar_t c ) const
{
c = IgnoreCase::map(c);
for( RegClass* p=cls.get(); p; p=p->next.get() )
Expand All @@ -439,7 +439,7 @@ struct RegTrans : public Object
return false;
}

bool match( wchar_t c, bool caseS )
bool match( wchar_t c, bool caseS ) const
{
bool m = caseS ? match_c( c ) : match_i( c );
return cmpcls ? !m : m;
Expand Down Expand Up @@ -486,7 +486,7 @@ class RegNFA: public Object
};

RegNFA::RegNFA( const wchar_t* pat )
: parser( pat )
: parser( pat ), st (16)
{
start = gen_state();
final = gen_state();
Expand Down Expand Up @@ -624,7 +624,7 @@ int RegNFA::match( const wchar_t* str, int len, bool caseS )

int matchpos = -1;

storage<st_ele> stack;
storage<st_ele> stack(16);
push(stack, start, 0);
while( stack.size() > 0 )
{
Expand Down
2 changes: 1 addition & 1 deletion RSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RSearch A_FINAL: public Searchable
{
public:
explicit RSearch( const unicode* key, bool caseS, bool down );
~RSearch();
~RSearch() override;

private:
bool Search( const unicode* str, ulong len, ulong stt,
Expand Down
12 changes: 6 additions & 6 deletions Search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ void SearchManager::on_init()
#ifdef _UNICODE
SetItemText( IDC_FINDBOX, str.get() );
#else
char *ab = new TCHAR[(len+1)*3];
char *ab = malloc( (len+1) * 3 * sizeof(TCHAR) );
if( ab )
{
::WideCharToMultiByte( CP_ACP, 0, str.get(), -1,
ab, (len+1)*3, NULL, NULL );
SetItemText( IDC_FINDBOX, ab );
delete [] ab;
free( ab );
}
#endif
}
Expand Down Expand Up @@ -226,21 +226,21 @@ void SearchManager::UpdateData()

TCHAR* str;
LRESULT n = SendMsgToItem( IDC_FINDBOX, WM_GETTEXTLENGTH );
str = new TCHAR[n+1];
str = (TCHAR*)malloc( sizeof(TCHAR) * (n+1) );
if( str )
{
GetItemText( IDC_FINDBOX, n+1, str );
findStr_ = str;
delete [] str;
free( str );
}

n = SendMsgToItem( IDC_REPLACEBOX, WM_GETTEXTLENGTH );
str = new TCHAR[n+1];
str = (TCHAR*)malloc( sizeof(TCHAR) * (n+1) );
if( str )
{
GetItemText( IDC_REPLACEBOX, n+1, str );
replStr_ = str;
delete [] str;
free( str );
}
}

Expand Down
6 changes: 3 additions & 3 deletions editwing/ewDoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Insert A_FINAL: public Command
// @param del コマンド終了時にdelete [] strしてよいか?
//@}
Insert( const DPos& s, const unicode* str, ulong len, bool del=false );
~Insert();
~Insert() override;

private:

Expand Down Expand Up @@ -151,7 +151,7 @@ class Replace A_FINAL: public Command
//@}
Replace( const DPos& s, const DPos& e,
const unicode* str, ulong len, bool del=false );
~Replace();
~Replace() override;

private:

Expand Down Expand Up @@ -188,7 +188,7 @@ class MacroCommand A_FINAL: public Command
ulong size() const { return arr_.size(); }

//@ デストラクタ //@}
~MacroCommand()
~MacroCommand() override
{
for( ulong i=0,e=arr_.size(); i<e; ++i )
delete arr_[i];
Expand Down
26 changes: 13 additions & 13 deletions editwing/ip_cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ void Cursor::on_ime_composition( LPARAM lp )
if( str )
{
pEvHan_->on_ime( *this, str, len );
delete [] str;
ime().FreeString( str );
}
}
#endif // NO_IME
Expand Down Expand Up @@ -509,11 +509,11 @@ void Cursor::InputUTF32( qbyte utf32 )

void Cursor::Input( const char* str, ulong len )
{
unicode* ustr = new unicode[ len*4 ];
unicode* ustr = (unicode *)malloc( len * 4 * sizeof(unicode) );
if(!ustr) return;
len = ::MultiByteToWideChar( CP_ACP, 0, str, len, ustr, len*4 );
Input( ustr, len );
delete [] ustr;
free( ustr );
}
void Cursor::InputAt( const unicode *str, ulong len, int x, int y )
{
Expand Down Expand Up @@ -585,11 +585,11 @@ void Cursor::InputAt( const unicode *str, ulong len, int x, int y )
}
void Cursor::InputAt( const char* str, ulong len, int x, int y )
{
unicode* ustr = new unicode[ len*4 ];
unicode* ustr = (unicode *)malloc( len * 4 * sizeof(unicode) );
if(!ustr) return;
len = ::MultiByteToWideChar( CP_ACP, 0, str, len, ustr, len*4 );
InputAt( ustr, len, x, y );
delete [] ustr;
free( ustr );
}

void Cursor::DelBack( bool wide )
Expand Down Expand Up @@ -729,7 +729,7 @@ ki::aarr<unicode> Cursor::getSelectedStr() const

// テキスト取得, Get Text
ulong len = doc_.getRangeLength( dm, dM );
ki::aarr<unicode> ub( new unicode[len+1] );
ki::aarr<unicode> ub( len+1 );
if( ub.get() )
doc_.getText( ub.get(), dm, dM );
return ub;
Expand Down Expand Up @@ -798,7 +798,7 @@ void Cursor::Copy()
, NULL, MB_OK|MB_TASKMODAL|MB_TOPMOST) ;
return;
}
unicode *p = new unicode[len+1];
unicode *p = (unicode *)malloc( sizeof(unicode) * (len+1) );
if(!p) return;
doc_.getText( p, dm, dM );
// Replace null characters by spaces when copying.
Expand All @@ -810,7 +810,7 @@ void Cursor::Copy()
::WideCharToMultiByte( CP_ACP, 0, p, len+1,
static_cast<char*>(::GlobalLock(h)), (len+1)*3, NULL, NULL );
::GlobalUnlock( h );
delete [] p;
free( p );
if( !clp.SetData( CF_TEXT, h ) )
GlobalFree(h); // Could not set data
}
Expand Down Expand Up @@ -948,7 +948,7 @@ void Cursor::ModSelection(ModProc mfunk)
return; // Nothing to do.

size_t len = doc_.getRangeLength( dm, dM );
unicode *p = new unicode[len+1];
unicode *p = (unicode *)malloc( sizeof(unicode) * (len+1) );
if( !p ) return;
doc_.getText( p, dm, dM );
unicode *np = mfunk(p, &len); // IN and OUT len!!!
Expand All @@ -959,7 +959,7 @@ void Cursor::ModSelection(ModProc mfunk)
MoveCur(ocur, true);
}

delete [] p;
free( p );
// // Useless for now...
// if( np < p || np > p+len+1 )
// delete np; // was allocated
Expand Down Expand Up @@ -1242,12 +1242,12 @@ bool Cursor::on_drag_start( short x, short y )
const VPos dm = Min(cur_, sel_);
const VPos dM = Max(cur_, sel_);
ulong len = doc_.getRangeLength( dm, dM );
unicode *p = new unicode[len+1];
unicode *p = (unicode *)malloc( sizeof(unicode) * (len+1) );
if( p )
{
doc_.getText( p, dm, dM );
OleDnDSourceTxt doDrag(p, len);
delete [] p;
free( p );
if( doDrag.getEffect() == DROPEFFECT_MOVE )
doc_.Execute( Delete( cur_, sel_ ) );
}
Expand Down Expand Up @@ -1331,7 +1331,7 @@ int Cursor::on_ime_reconvertstring( RECONVERTSTRING* rs )
if( !ub.get() ) return 0;
ulong len;
for(len=0; ub[len]; ++len);
ki::aarr<char> nw( new TCHAR[(len+1)*3] );
ki::aarr<char> nw( (len+1)*3 );
if( !nw.get() ) return 0;
str = nw;
::WideCharToMultiByte( CP_ACP, 0, ub.get(), -1,
Expand Down
3 changes: 0 additions & 3 deletions editwing/ip_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ class Line //: public ki::Object
str_[ len ] = 0x007f;
}

// Dummy empty constructor to allocate array of lines
Line() {}

void Clear() // Manually destroy line
{
if( str_ != empty_buf() )
Expand Down
Loading

0 comments on commit fc4043a

Please sign in to comment.