Skip to content

Commit

Permalink
fix toBuffer for image/jpeg
Browse files Browse the repository at this point in the history
Was mixing vec.capacity() and vec.size(). With MSVC the vec's growth factor is 1.5 (same as what I'm using), so this bug didn't manifest. With glibc it's 2.0. Also had an off-by-one.

Fixes Automattic#1350
  • Loading branch information
zbjornson committed Jan 13, 2019
1 parent 5fb3e73 commit 8663a30
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/closure.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ void JpegClosure::init_destination(j_compress_ptr cinfo) {
JpegClosure* closure = (JpegClosure*)cinfo->client_data;
closure->vec.resize(PAGE_SIZE);
closure->jpeg_dest_mgr->next_output_byte = &closure->vec[0];
closure->jpeg_dest_mgr->free_in_buffer = closure->vec.capacity();
closure->jpeg_dest_mgr->free_in_buffer = closure->vec.size();
}

boolean JpegClosure::empty_output_buffer(j_compress_ptr cinfo) {
JpegClosure* closure = (JpegClosure*)cinfo->client_data;
size_t currentSize = closure->vec.capacity();
size_t currentSize = closure->vec.size();
closure->vec.resize(currentSize * 1.5);
closure->jpeg_dest_mgr->next_output_byte = &closure->vec[currentSize - 1];
closure->jpeg_dest_mgr->free_in_buffer = closure->vec.capacity() - currentSize;
closure->jpeg_dest_mgr->next_output_byte = &closure->vec[currentSize];
closure->jpeg_dest_mgr->free_in_buffer = closure->vec.size() - currentSize;
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/closure.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct JpegClosure : Closure {
uint32_t quality = 75;
uint32_t chromaSubsampling = 2;
bool progressive = false;
jpeg_destination_mgr* jpeg_dest_mgr;
jpeg_destination_mgr* jpeg_dest_mgr = nullptr;

static void init_destination(j_compress_ptr cinfo);
static boolean empty_output_buffer(j_compress_ptr cinfo);
Expand Down

0 comments on commit 8663a30

Please sign in to comment.