From a231e4dd854d9275d3af8ade350270a1875fa57c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Sun, 19 Mar 2023 19:42:29 -0300 Subject: [PATCH] ecode: Closes SpartanJ/ecode#85. Gracefully handle document save failing. Fixed out of range access when editing files. --- src/eepp/ui/doc/textdocument.cpp | 10 +++++++--- src/eepp/ui/uicodeeditor.cpp | 3 ++- src/tools/ecode/ecode.cpp | 11 ++++++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/eepp/ui/doc/textdocument.cpp b/src/eepp/ui/doc/textdocument.cpp index 2103afd12..0386d1061 100644 --- a/src/eepp/ui/doc/textdocument.cpp +++ b/src/eepp/ui/doc/textdocument.cpp @@ -517,8 +517,11 @@ TextDocument::LoadStatus TextDocument::reload() { bool TextDocument::save( const std::string& path ) { if ( path.empty() || mDefaultFileName == path ) return false; - if ( FileSystem::fileCanWrite( FileSystem::fileRemoveFileName( path ) ) ) { + if ( FileSystem::fileCanWrite( path ) ) { IOStreamFile file( path, "wb" ); + std::string oldFilePath( mFilePath ); + URI oldFileURI( mFileURI ); + FileInfo oldFileInfo( mFileRealPath ); mFilePath = path; mFileURI = URI( "file://" + mFilePath ); mSaving = true; @@ -530,8 +533,9 @@ bool TextDocument::save( const std::string& path ) { notifyDocumentSaved(); return true; } else { - mFilePath.clear(); - mFileRealPath = FileInfo(); + mFilePath = std::move( oldFilePath ); + mFileURI = std::move( oldFileURI ); + mFileRealPath = std::move( oldFileInfo ); mSaving = false; } } diff --git a/src/eepp/ui/uicodeeditor.cpp b/src/eepp/ui/uicodeeditor.cpp index 2ba8d12cd..b1031ddcc 100644 --- a/src/eepp/ui/uicodeeditor.cpp +++ b/src/eepp/ui/uicodeeditor.cpp @@ -1731,7 +1731,8 @@ Float UICodeEditor::getXOffsetCol( const TextPosition& position ) const { const String& line = mDoc->line( position.line() ).getText(); Float glyphWidth = getGlyphWidth(); Float x = 0; - for ( auto i = 0; i < position.column(); i++ ) { + Int64 maxCol = eemin( (Int64)line.size(), position.column() ); + for ( auto i = 0; i < maxCol; i++ ) { if ( line[i] == '\t' ) { x += glyphWidth * mTabWidth; } else if ( line[i] != '\n' && line[i] != '\r' ) { diff --git a/src/tools/ecode/ecode.cpp b/src/tools/ecode/ecode.cpp index 9f89cb794..482928d33 100644 --- a/src/tools/ecode/ecode.cpp +++ b/src/tools/ecode/ecode.cpp @@ -102,8 +102,17 @@ void App::saveDoc() { return; if ( mSplitter->getCurEditor()->getDocument().hasFilepath() ) { - if ( mSplitter->getCurEditor()->save() ) + if ( mSplitter->getCurEditor()->save() ) { updateEditorState(); + } else { + UIMessageBox * msgBox = errorMsgBox( i18n( + "could_not_write_file", "Could not write file to disk.\nPlease check if the file " + "is read-only or if ecode does not have permissions to write to that file.\n" + "File path is: " + ) + mSplitter->getCurEditor()->getDocument().getFilePath() ); + + setFocusEditorOnClose( msgBox ); + } } else { saveFileDialog( mSplitter->getCurEditor() ); }