Skip to content

Commit

Permalink
Change to exception handling with Kakadu to avoid crash with mal-form…
Browse files Browse the repository at this point in the history
…ed JPEG2000 streams. Fixes #264
  • Loading branch information
ruven committed Feb 5, 2024
1 parent cfe916a commit b2d5944
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
05/02/2024:
- Added JPEG2000 codec version information to logging.
- Change to exception handling with Kakadu to avoid crash with mal-formed JPEG2000 streams. Fixes
https://github.com/ruven/iipsrv/issues/264.


26/01/2024:
Expand Down
14 changes: 9 additions & 5 deletions src/KakaduImage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Culture of the Czech Republic.
Copyright (C) 2009-2023 IIPImage.
Copyright (C) 2009-2024 IIPImage.
Author: Ruven Pillay
This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -375,8 +375,7 @@ void KakaduImage::loadImageInfo( int seq, int ang )
// For bilevel images, force channels to 1 as we sometimes come across such images which claim 3 channels
if( bpc == 1 ) channels = 1;

// Get the max and min values for our data type
//double sminvalue[4], smaxvalue[4];
// Get the max and min values for our data type - only handle 8 and 16 bit for now
for( unsigned int i=0; i<channels; i++ ){
min.push_back( 0.0 );
if( bpc > 8 && bpc <= 16 ) max.push_back( 65535.0 );
Expand Down Expand Up @@ -431,8 +430,8 @@ void KakaduImage::closeImage()
if( codestream.exists() ) codestream.destroy();

// Close our JP2 family and JPX files
src.close();
jpx_input.close();
if( src.exists() ) src.close();
if( jpx_input.exists() ) jpx_input.close();

#ifdef KAKADU_DEBUG
logfile << "Kakadu :: closeImage() :: " << timer.getTime() << " microseconds" << endl;
Expand Down Expand Up @@ -820,7 +819,12 @@ void KakaduImage::process( unsigned int res, int layers, int xoffset, int yoffse
}
catch (...){
// Shut down our decompressor, delete our buffers, destroy our threads and codestream before rethrowing the exception
#if defined(KDU_MAJOR_VERSION) && (KDU_MAJOR_VERSION >= 8 || ((KDU_MAJOR_VERSION >= 7) && (KDU_MINOR_VERSION >= 5)))
// Note that from Kakadu 7.5 onwards, we need to use reset() rather than finish() in case of an exception
decompressor.reset( true );
#else
decompressor.finish();
#endif
if( env.exists() ) env.destroy();
delete_buffer( stripe_buffer );
delete_buffer( buffer );
Expand Down

0 comments on commit b2d5944

Please sign in to comment.