Skip to content

Commit 7edfd56

Browse files
committed
Fix bugs found during code review (with additional test).
1 parent f65ec49 commit 7edfd56

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/physics/utils/pio_reader.F90

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,7 @@ subroutine get_netcdf_var_real_5d(this, varname, var, errmsg, errcode, start, co
14531453

14541454
!Now attempt to allocate and initialize the variable,
14551455
!and read-in the NetCDF data:
1456-
allocate(var(dim_sizes(1), dim_sizes(2), dim_sizes(3), dim_sizes(4), dim_sizes(5)), &
1456+
allocate(var(alloc_dims(1), alloc_dims(2), alloc_dims(3), alloc_dims(4), alloc_dims(5)), &
14571457
stat=errcode, errmsg=errmsg)
14581458
if(errcode /= 0) then
14591459
!Reset PIO back to original error handling method:
@@ -2600,12 +2600,12 @@ subroutine var_subset_check(varname, var_ndims, dim_sizes, do_subset, alloc_dims
26002600

26012601
!Check if count indices are within bounds:
26022602
do i = 1, file_var_dim_num
2603-
if ((count(i) < 1) .or. (count(i) > dim_sizes(i))) then
2603+
if ((count(i) < 1) .or. ((start(i)+count(i)-1) > dim_sizes(i))) then
26042604
errcode = bad_subset_range_err
26052605
write(errmsg, '(a,i0,3a,i0,a,i0,a)') &
26062606
"Element ", i, " of 'count' for variable '", &
26072607
trim(varname), "' is out of bounds. Expected 1 to ", &
2608-
dim_sizes(i), " but got ", count(i), "."
2608+
(dim_sizes(i)-start(i)+1), " but got ", count(i), "."
26092609
return
26102610
end if
26112611
end do

test/unit/fortran/src/pio_reader/test_pio_reader.pf

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,20 +1920,28 @@ subroutine test_pio_reader_bad_count_element_range()
19201920
! Open file:
19211921
call reader%open_file(fname, errmsg, errcode)
19221922

1923-
! Attempt to read variable with negative start element:
1923+
! Attempt to read variable with negative count element:
19241924
call reader%get_var("pressure", pressure, errmsg, errcode, start=(/1/), count=(/-6/))
19251925

19261926
! Check that the correct error was raised:
19271927
@assertNotEqual(0, errcode)
19281928
@assertEqual(expected_err_msg_neg, trim(errmsg))
19291929

1930-
! Next attempt to read variable with too-large start element:
1930+
! Next attempt to read variable with too-large count element:
19311931
call reader%get_var("pressure", pressure, errmsg, errcode, start=(/1/), count=(/60/))
19321932

19331933
! Check that the correct error was raised:
19341934
@assertNotEqual(0, errcode)
19351935
@assertEqual(expected_err_msg_huge, trim(errmsg))
19361936

1937+
! Now attempt to read variable with too-large start+count element:
1938+
call reader%get_var("pressure", pressure, errmsg, errcode, start=(/3/), count=(/58/))
1939+
1940+
! Check that the correct error was raised:
1941+
@assertNotEqual(0, errcode)
1942+
@assertEqual(expected_err_msg_huge, trim(errmsg))
1943+
1944+
19371945
! Perform test cleanup:
19381946
call reader%close_file(errmsg, errcode)
19391947
deallocate(reader)

0 commit comments

Comments
 (0)