Changeset 3041 for trunk


Ignore:
Timestamp:
07/27/15 18:44:50 (9 years ago)
Author:
Takeshi Nakazato
Message:

New Development: No

JIRA Issue: Yes CAS-7743

Ready for Test: Yes

Interface Changes: Yes/No?

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No?

Module(s): Module Names change impacts.

Description: Describe your changes here...


Better handling of large number that exceeds a limit of uInt (unsigned int) and proper handling of possible memory allocation error.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/GenericEdgeDetector.cpp

    r2639 r3041  
    1111//
    1212#include <math.h>
     13#include <limits.h>
     14#include <stdint.h>
    1315
    1416#include <casa/Arrays/Vector.h>
     
    167169  dx_ = dy_ / decCorr ;
    168170
    169   nx_ = uInt( ceil( wx / dx_ ) ) ;
    170   ny_ = uInt( ceil( wy / dy_ ) ) ;
     171  Double nxTemp = ceil(wx / dx_);
     172  Double nyTemp = ceil(wy / dy_);
     173  if (nxTemp > (Double)UINT_MAX || nyTemp > (Double)UINT_MAX) {
     174          throw AipsError("Error in setup: Too large number of pixels.");
     175  }
     176  nx_ = uInt( nxTemp ) ;
     177  ny_ = uInt( nyTemp ) ;
    171178
    172179  pcenx_ = 0.5 * Double( nx_ - 1 ) ;
     
    186193  os_.origin(LogOrigin( "GenericEdgeDetector", "countup", WHERE )) ;
    187194
    188   uInt *a_p = new uInt[nx_*ny_] ;
    189   apix_.takeStorage( IPosition(2,nx_,ny_), a_p, TAKE_OVER ) ;
     195  try {
     196          size_t n = (size_t)nx_ * (size_t)ny_;
     197          uInt *a_p = new uInt[n] ;
     198          apix_.takeStorage( IPosition(2,nx_,ny_), a_p, TAKE_OVER ) ;
     199  }
     200  catch (std::bad_alloc const &e) {
     201          os_ << LogIO::SEVERE << "Failed to allocate working array" << LogIO::POST;
     202          throw e;
     203  }
     204  catch (...) {
     205          os_ << LogIO::SEVERE << "Failed due to unknown error" << LogIO::POST;
     206          throw;
     207  }
    190208  apix_ = 0 ;
    191209 
     
    198216  const Double *py_p = pdir_p + 1 ;
    199217  long offset ;
     218  // apix_ is always contiguous
     219  uInt *a_p = apix_.data();
    200220  for ( uInt i = 0 ; i < len ; i++ ) {
    201221    ix = uInt(round( *px_p )) ;
Note: See TracChangeset for help on using the changeset viewer.