Skip to content

Commit 1fc5cd9

Browse files
authoredJun 2, 2021
Merge pull request #5 from jvdp1/add_checks1
Add checks on `work` and `iwork`
2 parents 70e13f8 + 25876b7 commit 1fc5cd9

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed
 

‎src/stdlib_sorting_ord_sort.fypp

+4-1
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,15 @@ contains
111111
integer(int_size) :: array_size
112112
integer :: stat
113113

114+
array_size = size( array, kind=int_size )
114115
if ( present(work) ) then
116+
if ( size( work, kind=int_size) < array_size/2 ) then
117+
error stop "${k1}$_${sname}$_ord_sort: work array is too small."
118+
endif
115119
! Use the work array as scratch memory
116120
call merge_sort( array, work )
117121
else
118122
! Allocate a buffer to use as scratch memory.
119-
array_size = size( array, kind=int_size )
120123
allocate( buf(0:array_size/2-1), stat=stat )
121124
if ( stat /= 0 ) error stop "${k1}$_${sname}$_ord_sort: Allocation of buffer failed."
122125
call merge_sort( array, buf )

‎src/stdlib_sorting_sort_index.fypp

+9
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,13 @@ contains
107107

108108
! If necessary allocate buffers to serve as scratch memory.
109109
if ( present(work) ) then
110+
if ( size(work, kind=int_size) < array_size/2 ) then
111+
error stop "work array is too small."
112+
end if
110113
if ( present(iwork) ) then
114+
if ( size(iwork, kind=int_size) < array_size/2 ) then
115+
error stop "iwork array is too small."
116+
endif
111117
call merge_sort( array, index, work, iwork )
112118
else
113119
allocate( ibuf(0:array_size/2-1), stat=stat )
@@ -118,6 +124,9 @@ contains
118124
allocate( buf(0:array_size/2-1), stat=stat )
119125
if ( stat /= 0 ) error stop "Allocation of array buffer failed."
120126
if ( present(iwork) ) then
127+
if ( size(iwork, kind=int_size) < array_size/2 ) then
128+
error stop "iwork array is too small."
129+
endif
121130
call merge_sort( array, index, buf, iwork )
122131
else
123132
allocate( ibuf(0:array_size/2-1), stat=stat )

0 commit comments

Comments
 (0)
Please sign in to comment.