pj_apply_vgridshift accesses point outside of grid (segmentation fault) #396
When you happen to transform point on the edge of vertical shift grid file (e.g. at the south pole) the bilinear interpolation code accesses point outside of grid limits due to nature of bilinear interpolation which samples points (x, y), (x + 1, y), (x, y + 1), (x + 1, y + 1). Coordinates x and y are checked against limits but (x + 1) and (y + 1) are left unchecked. This causes undefined behaviour that sometimes leads to program crash (segmentation fault).
I've fixed the function in our installation by replacing 1 with variables next_x and next_y that are set to 1 everywhere except at the grid's right and bottom border. This effectively replicates values at border.
int next_x, next_y; ... next_x = ((grid_ix + 1) < ct->lim.lam); next_y = ((grid_iy + 1) < ct->lim.phi); ... value = cvs[grid_ix + grid_iy * ct->lim.lam] * (1.0-grid_x) * (1.0-grid_y) + cvs[grid_ix + next_x + grid_iy * ct->lim.lam] * (grid_x) * (1.0-grid_y) + cvs[grid_ix + (grid_iy + next_y) * ct->lim.lam] * (1.0-grid_x) * (grid_y) + cvs[grid_ix + next_x + (grid_iy + next_y) * ct->lim.lam] * (grid_x) * (grid_y);
The text was updated successfully, but these errors were encountered: