Repository navigation
bugfix(mapcache): Close the file and free the buffer when a map preview copy fails #3485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tintinhamans
wants to merge
1
commit into
TheSuperHackers:main
Choose a base branch
from
tintinhamans:arctic/fix/mapcache-error-leaks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+17
−41
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1090,53 +1090,38 @@ const MapMetaData *MapCache::findMap(AsciiString mapName) | |
| // ------------------------------------------------------------------------------------------------ | ||
| /** Embed the pristine map into the xfer stream */ | ||
| // ------------------------------------------------------------------------------------------------ | ||
| static void copyFromBigToDir( const AsciiString& infile, const AsciiString& outfile ) | ||
| static Bool copyFromBigToDir( const AsciiString& infile, const AsciiString& outfile ) | ||
| { | ||
| // open the map file | ||
|
|
||
| File *file = TheFileSystem->openFile( infile.str(), File::READ | File::BINARY ); | ||
| if( file == nullptr ) | ||
| { | ||
| DEBUG_CRASH(( "copyFromBigToDir - Error opening source file '%s'", infile.str() )); | ||
| throw SC_INVALID_DATA; | ||
| return FALSE; | ||
| } | ||
|
|
||
| // how big is the map file | ||
| Int fileSize = file->seek( 0, File::END ); | ||
|
|
||
|
|
||
| // rewind to beginning of file | ||
| const Int fileSize = file->seek( 0, File::END ); | ||
| file->seek( 0, File::START ); | ||
|
|
||
| // allocate buffer big enough to hold the entire map file | ||
| char *buffer = NEW char[ fileSize ]; | ||
| if( buffer == nullptr ) | ||
| { | ||
| DEBUG_CRASH(( "copyFromBigToDir - Unable to allocate buffer for file '%s'", infile.str() )); | ||
| throw SC_INVALID_DATA; | ||
| } | ||
|
|
||
| // copy the file to the buffer | ||
| if( file->read( buffer, fileSize ) < fileSize ) | ||
| { | ||
| DEBUG_CRASH(( "copyFromBigToDir - Error reading from file '%s'", infile.str() )); | ||
| throw SC_INVALID_DATA; | ||
| } | ||
| // close the BIG file | ||
| const Bool readOk = file->read( buffer, fileSize ) == fileSize; | ||
| file->close(); | ||
| DEBUG_ASSERTCRASH( readOk, ( "copyFromBigToDir - Error reading from file '%s'", infile.str() ) ); | ||
|
|
||
| File *filenew = TheFileSystem->openFile( outfile.str(), File::WRITE | File::CREATE | File::BINARY ); | ||
|
|
||
| if( !filenew || filenew->write(buffer, fileSize) < fileSize) | ||
| Bool writeOk = FALSE; | ||
| if( readOk ) | ||
| { | ||
| DEBUG_CRASH(( "copyFromBigToDir - Error writing to file '%s'", outfile.str() )); | ||
| throw SC_INVALID_DATA; | ||
| File *filenew = TheFileSystem->openFile( outfile.str(), File::WRITE | File::CREATE | File::BINARY ); | ||
| if( filenew != nullptr ) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to assert on failure.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added, the assert covers both opening and writing the output file |
||
| { | ||
| writeOk = filenew->write( buffer, fileSize ) == fileSize; | ||
| filenew->close(); | ||
| } | ||
| DEBUG_ASSERTCRASH( writeOk, ( "copyFromBigToDir - Error writing to file '%s'", outfile.str() ) ); | ||
| } | ||
|
|
||
| filenew->close(); | ||
|
|
||
| // delete the buffer | ||
| delete [] buffer; | ||
|
|
||
| return writeOk; | ||
| } | ||
|
|
||
| Image *getMapPreviewImage( AsciiString mapName ) | ||
|
|
@@ -1181,16 +1166,7 @@ Image *getMapPreviewImage( AsciiString mapName ) | |
|
|
||
| mapPreviewDir.concat(name); | ||
|
|
||
| Bool success = false; | ||
| try | ||
| { | ||
| copyFromBigToDir(tgaName, mapPreviewDir); | ||
| success = true; | ||
| } | ||
| catch (...) | ||
| { | ||
| success = false; // no rethrow | ||
| } | ||
| const Bool success = copyFromBigToDir(tgaName, mapPreviewDir); | ||
|
tintinhamans marked this conversation as resolved.
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| if (success) | ||
| { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be good to have an assert here still to warn on failure of file opening
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added the assert back