Skip to content

Commit

Permalink
Merge pull request #496 from DrylandEcology/bufix_495_read_soil_line
Browse files Browse the repository at this point in the history
Fix bug in function `_read_soil_line()`: close #495
  • Loading branch information
dschlaep authored May 18, 2020
2 parents ded9ff6 + 71b9749 commit e191f3f
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions ST_grid.c
Original file line number Diff line number Diff line change
Expand Up @@ -1025,14 +1025,28 @@ static int _read_soil_line(char* buf, SoilType* destination, int layer){
&destination->pclay[layer], &destination->imperm[layer],
&destination->soiltemp[layer], destination->rootsFile);

if(cellNum > grid_Cells || layerRead > destination->num_layers){
LogError(logfp, LOGFATAL, "%s: cells out of order or too many layers.", grid_files[GRID_FILE_SOILS]);
if(cellNum > grid_Cells){
LogError(
logfp, LOGFATAL,
"%s: cell number (id=%d) is larger than number of cells in grid (n=%d).",
grid_files[GRID_FILE_SOILS], cellNum, grid_Cells
);
}


/* If the user specified a cell to copy we will perform the copy, regardless
of whether or not they also entered parameters. */
if(entriesRead == 1){
entriesRead = sscanf(buf, "%d,%d", &cellNum, &cellToCopy);

if(cellToCopy > grid_Cells){
LogError(
logfp, LOGFATAL,
"%s: cell to copy (id=%d) not present in grid (n=%d).",
grid_files[GRID_FILE_SOILS], cellToCopy, grid_Cells
);
}

if(entriesRead == 2){
return cellToCopy;
} else {
Expand All @@ -1041,6 +1055,14 @@ static int _read_soil_line(char* buf, SoilType* destination, int layer){
}

if(entriesRead == 16) {
if(layerRead > destination->num_layers){
LogError(
logfp, LOGFATAL,
"%s: cell %d has too many soil layers (%d for max=%d).",
grid_files[GRID_FILE_SOILS], cellNum, layerRead, destination->num_layers
);
}

return SOIL_READ_SUCCESS;
} else {
return SOIL_READ_FAILURE;
Expand Down

0 comments on commit e191f3f

Please sign in to comment.