do away with need to accept out-of-bounds values in getTileRaw().

This commit is contained in:
Eric House 2011-11-23 18:25:12 -08:00
parent 795b86e569
commit 2bfe120014
2 changed files with 15 additions and 15 deletions

View file

@ -90,7 +90,9 @@ model_make( MPFORMAL DictionaryCtxt* dict,
XP_U16 nRows ) XP_U16 nRows )
{ {
ModelCtxt* result; ModelCtxt* result;
XP_U16 size = sizeof(*result) + TILES_SIZE(result, nCols); XP_U16 size;
XP_ASSERT( nCols == nRows && nCols <= MAX_COLS );
size = sizeof(*result) + TILES_SIZE(result, nCols);
result = (ModelCtxt*)XP_MALLOC( mpool, size ); result = (ModelCtxt*)XP_MALLOC( mpool, size );
if ( result != NULL ) { if ( result != NULL ) {
XP_MEMSET( result, 0, sizeof(*result) ); XP_MEMSET( result, 0, sizeof(*result) );
@ -700,15 +702,11 @@ setModelTileRaw( ModelCtxt* model, XP_U16 col, XP_U16 row, CellTile tile )
static CellTile static CellTile
getModelTileRaw( const ModelCtxt* model, XP_U16 col, XP_U16 row ) getModelTileRaw( const ModelCtxt* model, XP_U16 col, XP_U16 row )
{ {
CellTile tile;
XP_U16 nCols = model->nCols; XP_U16 nCols = model->nCols;
XP_ASSERT( model->nRows == nCols ); XP_ASSERT( model->nRows == nCols );
if ( col < nCols && row < nCols ) { XP_ASSERT( col < nCols );
tile = model->tiles[(row*nCols) + col]; XP_ASSERT( row < nCols );
} else { return model->tiles[(row*nCols) + col];
tile = TILE_EMPTY_BIT;
}
return tile;
} /* getModelTileRaw */ } /* getModelTileRaw */
static void static void

View file

@ -318,10 +318,11 @@ static XP_Bool
modelIsEmptyAt( const ModelCtxt* model, XP_U16 col, XP_U16 row ) modelIsEmptyAt( const ModelCtxt* model, XP_U16 col, XP_U16 row )
{ {
Tile tile; Tile tile;
XP_Bool found; XP_U16 nCols = model_numCols( model );
XP_Bool found = col < nCols
found = model_getTile( model, col, row, XP_FALSE, -1, &tile, && row < nCols
NULL, NULL, NULL ); && model_getTile( model, col, row, XP_FALSE, -1, &tile,
NULL, NULL, NULL );
return !found; return !found;
} /* modelIsEmptyAt */ } /* modelIsEmptyAt */
@ -722,11 +723,12 @@ find_end( const ModelCtxt* model, XP_U16 col, XP_U16 row,
XP_Bool isHorizontal ) XP_Bool isHorizontal )
{ {
XP_U16* incr = isHorizontal? &col: &row; XP_U16* incr = isHorizontal? &col: &row;
XP_U16 limit = isHorizontal? MAX_COLS-1:MAX_ROWS-1; XP_U16 nCols = model_numCols( model );
XP_U16 limit = nCols - 1;
XP_U16 lastGood = *incr; XP_U16 lastGood = *incr;
XP_ASSERT( col < MAX_COLS ); XP_ASSERT( col < nCols );
XP_ASSERT( row < MAX_ROWS ); XP_ASSERT( row < nCols );
for ( ; ; ) { for ( ; ; ) {
XP_ASSERT( *incr <= limit ); XP_ASSERT( *incr <= limit );