Skip to content

Commit

Permalink
8253948: Memory leak in ImageFileReader
Browse files Browse the repository at this point in the history
Reviewed-by: alanb
  • Loading branch information
zhengyu123 committed Oct 5, 2020
1 parent 65cab55 commit 81dae70
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/java.base/share/native/libjimage/imageFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ ImageFileReader* ImageFileReader::id_to_reader(u8 id) {
}

// Constructor intializes to a closed state.
ImageFileReader::ImageFileReader(const char* name, bool big_endian) {
ImageFileReader::ImageFileReader(const char* name, bool big_endian) :
_module_data(NULL) {
// Copy the image file name.
int len = (int) strlen(name) + 1;
_name = new char[len];
Expand All @@ -369,6 +370,10 @@ ImageFileReader::~ImageFileReader() {
delete[] _name;
_name = NULL;
}

if (_module_data != NULL) {
delete _module_data;
}
}

// Open image file for read access.
Expand Down Expand Up @@ -419,9 +424,9 @@ bool ImageFileReader::open() {
_string_bytes = _index_data + string_bytes_offset;

// Initialize the module data
module_data = new ImageModuleData(this);
_module_data = new ImageModuleData(this);
// Successful open (if memory allocation succeeded).
return module_data != NULL;
return _module_data != NULL;
}

// Close image file.
Expand All @@ -436,6 +441,11 @@ void ImageFileReader::close() {
osSupport::close(_fd);
_fd = -1;
}

if (_module_data != NULL) {
delete _module_data;
_module_data = NULL;
}
}

// Read directly from the file.
Expand Down Expand Up @@ -568,5 +578,5 @@ void ImageFileReader::get_resource(ImageLocation& location, u1* uncompressed_dat

// Return the ImageModuleData for this image
ImageModuleData * ImageFileReader::get_image_module_data() {
return module_data;
return _module_data;
}
2 changes: 1 addition & 1 deletion src/java.base/share/native/libjimage/imageFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ friend class ImageFileReaderTable;
u4* _offsets_table; // Location offset table
u1* _location_bytes; // Location attributes
u1* _string_bytes; // String table
ImageModuleData *module_data; // The ImageModuleData for this image
ImageModuleData *_module_data; // The ImageModuleData for this image

ImageFileReader(const char* name, bool big_endian);
~ImageFileReader();
Expand Down

1 comment on commit 81dae70

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented on 81dae70 Oct 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.