Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1416)

Unified Diff: ui/gfx/codec/png_codec.cc

Issue 2576823002: Fix stack-use-after-scope in png_codec. (Closed)
Patch Set: Move ownership into PngWriteStructInfo. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/codec/png_codec.cc
diff --git a/ui/gfx/codec/png_codec.cc b/ui/gfx/codec/png_codec.cc
index ac386ad91d0b656e146776c8077694c881719e99..d6f49d1934f2ad916d27100d1c4d1ee9b3299203 100644
--- a/ui/gfx/codec/png_codec.cc
+++ b/ui/gfx/codec/png_codec.cc
@@ -328,22 +328,20 @@ class PngReadStructDestroyer {
DISALLOW_COPY_AND_ASSIGN(PngReadStructDestroyer);
};
-// Automatically destroys the given write structs on destruction to make
-// cleanup and error handling code cleaner.
-class PngWriteStructDestroyer {
+// Holds png struct and info ensuring the proper destruction.
+class PngWriteStructInfo {
public:
- explicit PngWriteStructDestroyer(png_struct** ps) : ps_(ps), pi_(0) {
- }
- ~PngWriteStructDestroyer() {
- png_destroy_write_struct(ps_, pi_);
+ explicit PngWriteStructInfo() : png_str_(0), info_ptr_(0) {
Nico 2016/12/14 23:47:52 (minor nit: "str" means "string" for me, not struc
dcheng 2016/12/14 23:48:52 Minor nit: no explicit, and use nullptr
krasin1 2016/12/14 23:53:47 Done.
}
- void SetInfoStruct(png_info** pi) {
- pi_ = pi;
+
+ ~PngWriteStructInfo() {
+ png_destroy_write_struct(&png_str_, &info_ptr_);
}
+
+ png_struct* png_str_;
+ png_info* info_ptr_;
private:
- png_struct** ps_;
- png_info** pi_;
- DISALLOW_COPY_AND_ASSIGN(PngWriteStructDestroyer);
+ DISALLOW_COPY_AND_ASSIGN(PngWriteStructInfo);
};
bool BuildPNGStruct(const unsigned char* input, size_t input_size,
@@ -708,15 +706,15 @@ bool EncodeWithCompressionLevel(const unsigned char* input,
// Row stride should be at least as long as the length of the data.
DCHECK(input_color_components * size.width() <= row_byte_width);
- png_struct* png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
- NULL, NULL, NULL);
- if (!png_ptr)
+ PngWriteStructInfo si;
+ si.png_str_ = png_create_write_struct(PNG_LIBPNG_VER_STRING,
+ NULL, NULL, NULL);
+ if (!si.png_str_)
return false;
- PngWriteStructDestroyer destroyer(&png_ptr);
- png_info* info_ptr = png_create_info_struct(png_ptr);
- if (!info_ptr)
+
+ si.info_ptr_ = png_create_info_struct(si.png_str_);
+ if (!si.info_ptr_)
return false;
- destroyer.SetInfoStruct(&info_ptr);
output->clear();
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698