From bd533e3e3243827a5667ab2efbeefdb85ffda30f Mon Sep 17 00:00:00 2001 From: jthorn Date: Fri, 20 Dec 2002 15:10:22 +0000 Subject: add testing for the error case where the grid is smaller than the molecule (before this would access memory off the end of the grid ==> core dump on some platforms, eg alphas) git-svn-id: http://svn.cactuscode.org/arrangements/CactusBase/LocalInterp/trunk@122 df1f8a13-aa1d-4dd4-9681-27ded5b42416 --- .../InterpLocalUniform.h | 27 ++++++++++++++++++---- src/GeneralizedPolynomial-Uniform/molecule_posn.c | 24 +++++++++++-------- src/GeneralizedPolynomial-Uniform/template.c | 21 ++++++++++++----- 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/src/GeneralizedPolynomial-Uniform/InterpLocalUniform.h b/src/GeneralizedPolynomial-Uniform/InterpLocalUniform.h index 24be556..d2990df 100644 --- a/src/GeneralizedPolynomial-Uniform/InterpLocalUniform.h +++ b/src/GeneralizedPolynomial-Uniform/InterpLocalUniform.h @@ -20,16 +20,16 @@ typedef int bool; /******************************************************************************/ /* number of integers in the range [x,y] inclusive */ -#define how_many_in_range(x,y) ((y) - (x) + 1) +#define HOW_MANY_IN_RANGE(x,y) ((y) - (x) + 1) /* is the integer x even/odd? */ -#define is_even(x) (((x) & 0x1) == 0) -#define is_odd (x) (((x) & 0x1) != 0) +#define IS_EVEN(x) (((x) & 0x1) == 0) +#define IS_ODD (x) (((x) & 0x1) != 0) /* round floating-point value to nearest integer */ /* ... result is expressed as floating point! */ /* ... needs for floor() */ -#define jt_round(x) floor((x) + 0.5) +#define JT_ROUND(x) floor((x) + 0.5) /******************************************************************************/ @@ -149,6 +149,25 @@ struct Jacobian_info CCTK_INT Jacobian_part_stride; }; +/**************************************/ + +/* + * error codes for LocalInterp_molecule_posn() + * ... these are defined in terms of INT_MIN from + */ + +/* x < minimum allowable x in grid */ +#define MOLECULE_POSN_ERROR_X_LT_MIN (INT_MIN + 0) + +/* x > maximum allowable x in grid */ +#define MOLECULE_POSN_ERROR_X_GT_MAX (INT_MIN + 1) + +/* grid is smaller than molecule */ +#define MOLECULE_POSN_ERROR_GRID_TINY (INT_MIN + 2) + +/* is a given integer an error code? */ +#define IS_MOLECULE_POSN_ERROR_CODE(x) (x <= MOLECULE_POSN_ERROR_GRID_TINY) + /******************************************************************************/ /* diff --git a/src/GeneralizedPolynomial-Uniform/molecule_posn.c b/src/GeneralizedPolynomial-Uniform/molecule_posn.c index 44e78fc..e007396 100644 --- a/src/GeneralizedPolynomial-Uniform/molecule_posn.c +++ b/src/GeneralizedPolynomial-Uniform/molecule_posn.c @@ -140,12 +140,16 @@ static const char *rcsid = "$Header$"; @returntype int @returndesc This function returns the integer coordinate i_center at which - the molecule should be centered, or one of the error codes - (from ) - INT_MIN if x is out-of-range on the min end of the grid - (i.e. x < the minimum allowable x) - INT_MAX if x is out-of-range on the max end of the grid - (i.e. x > the maximum allowable x) + the molecule should be centered, or one of the error codes defined + in "InterpLocalUniform.h": + MOLECULE_POSN_ERROR_X_LT_MIN + if x is out-of-range on the min end of the grid + (i.e. x < the minimum allowable x) + MOLECULE_POSN_ERROR_X_GT_MAX + if x is out-of-range on the max end of the grid + (i.e. x > the maximum allowable x) + MOLECULE_POSN_ERROR_GRID_TINY + if the grid is smaller than the molecule @endreturndesc @@*/ int LocalInterp_molecule_posn(fp grid_origin, fp grid_delta, @@ -184,7 +188,7 @@ if (out_of_range_tolerance_max >= 0.0) /* (see diagram in header comment for explanation) */ /* ... as a floating-point number */ { -const fp fp_i_center = is_even(molecule_size) ? floor(fp_i) : jt_round(fp_i); +const fp fp_i_center = IS_EVEN(molecule_size) ? floor(fp_i) : JT_ROUND(fp_i); /* ... as an integer */ int i_center = (int) fp_i_center; @@ -194,13 +198,15 @@ const int centered_i_max = i_center + m_max; /* check if off-centered molecules are forbidden? */ if ((out_of_range_tolerance_min == -1.0) && (centered_i_min < grid_i_min)) - then return INT_MIN; /*** ERROR RETURN ***/ + then return MOLECULE_POSN_ERROR_X_LT_MIN; /*** ERROR RETURN ***/ if ((out_of_range_tolerance_max == -1.0) && (centered_i_max > grid_i_max)) - then return INT_MAX; /*** ERROR RETURN ***/ + then return MOLECULE_POSN_ERROR_X_GT_MAX; /*** ERROR RETURN ***/ /* off-center as needed if we're close to the edge of the grid */ { int shift = 0; +if (molecule_size > HOW_MANY_IN_RANGE(grid_i_min,grid_i_max)) + then return MOLECULE_POSN_ERROR_GRID_TINY; /*** ERROR RETURN ***/ if (centered_i_min < grid_i_min) then shift = grid_i_min - centered_i_min; /* a positive number */ if (centered_i_max > grid_i_max) diff --git a/src/GeneralizedPolynomial-Uniform/template.c b/src/GeneralizedPolynomial-Uniform/template.c index ac1d98e..0bb409b 100644 --- a/src/GeneralizedPolynomial-Uniform/template.c +++ b/src/GeneralizedPolynomial-Uniform/template.c @@ -945,37 +945,46 @@ int pt; /* is the interpolation point out-of-range? */ #if (N_DIMS >= 1) - if ((center_i == INT_MIN) || (center_i == INT_MAX)) + if (IS_MOLECULE_POSN_ERROR_CODE(center_i)) then { + if (center_i == MOLECULE_POSN_ERROR_GRID_TINY) + then return CCTK_ERROR_INTERP_GRID_TOO_TINY; if (error_flags != NULL) then { error_flags->error_pt = pt; error_flags->error_axis = X_AXIS; - error_flags->error_end = (center_i > 0) ? +1 : -1; + error_flags->error_end + = (center_i == MOLECULE_POSN_ERROR_X_GT_MAX) ? +1 : -1; } return CCTK_ERROR_INTERP_POINT_X_RANGE; /*** ERROR RETURN ***/ } #endif #if (N_DIMS >= 2) - if ((center_j == INT_MIN) || (center_j == INT_MAX)) + if (IS_MOLECULE_POSN_ERROR_CODE(center_j)) then { + if (center_j == MOLECULE_POSN_ERROR_GRID_TINY) + then return CCTK_ERROR_INTERP_GRID_TOO_TINY; if (error_flags != NULL) then { error_flags->error_pt = pt; error_flags->error_axis = Y_AXIS; - error_flags->error_end = (center_j > 0) ? +1 : -1; + error_flags->error_end + = (center_j == MOLECULE_POSN_ERROR_X_GT_MAX) ? +1 : -1; } return CCTK_ERROR_INTERP_POINT_X_RANGE; /*** ERROR RETURN ***/ } #endif #if (N_DIMS >= 3) - if ((center_k == INT_MIN) || (center_k == INT_MAX)) + if (IS_MOLECULE_POSN_ERROR_CODE(center_k)) then { + if (center_k == MOLECULE_POSN_ERROR_GRID_TINY) + then return CCTK_ERROR_INTERP_GRID_TOO_TINY; if (error_flags != NULL) then { error_flags->error_pt = pt; error_flags->error_axis = Z_AXIS; - error_flags->error_end = (center_k > 0) ? +1 : -1; + error_flags->error_end + = (center_k == MOLECULE_POSN_ERROR_X_GT_MAX) ? +1 : -1; } return CCTK_ERROR_INTERP_POINT_X_RANGE; /*** ERROR RETURN ***/ } -- cgit v1.2.3