Skip to content

Commit def73f5

Browse files
mjrenomjreno
andauthored
refactor(idm): verify and store error based on length of user input names (#1775)
* check input length of model,exg,pacakge names * delay model package name error until all names checked --------- Co-authored-by: mjreno <[email protected]>
1 parent bb4a89a commit def73f5

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

src/Utilities/Idm/IdmLoad.f90

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module IdmLoadModule
1010
use SimVariablesModule, only: errmsg
1111
use ConstantsModule, only: LINELENGTH, LENMEMPATH, LENMODELNAME, &
1212
LENEXCHANGENAME, LENCOMPONENTNAME
13-
use SimModule, only: store_error, store_error_filename
13+
use SimModule, only: store_error, count_errors, store_error_filename
1414
use ListModule, only: ListType
1515
use InputLoadTypeModule, only: StaticPkgLoadBaseType, &
1616
DynamicPkgLoadBaseType, &
@@ -213,10 +213,10 @@ subroutine load_models(iout)
213213
use MemoryHelperModule, only: create_mem_path
214214
use MemoryManagerModule, only: mem_setptr
215215
use CharacterStringModule, only: CharacterStringType
216-
use SimVariablesModule, only: idm_context
217216
use DistributedSimModule, only: DistributedSimType, get_dsim
217+
use SimVariablesModule, only: idm_context, simfile
218218
use ModelPackageInputsModule, only: ModelPackageInputsType
219-
use SourceCommonModule, only: idm_component_type
219+
use SourceCommonModule, only: idm_component_type, inlen_check
220220
use SourceLoadModule, only: load_modelnam
221221
! -- dummy
222222
integer(I4B), intent(in) :: iout
@@ -252,7 +252,12 @@ subroutine load_models(iout)
252252
! -- attributes for this model
253253
mtype = mtypes(n)
254254
mfname = mfnames(n)
255-
mname = mnames(n)
255+
call inlen_check(mnames(n), mname, LENMODELNAME, 'MODELNAME')
256+
!
257+
! -- terminate if errors were detected
258+
if (count_errors() > 0) then
259+
call store_error_filename(simfile)
260+
end if
256261
!
257262
! -- load specified model inputs
258263
if (model_loadmask(n) > 0) then
@@ -290,7 +295,8 @@ subroutine load_exchanges(iout)
290295
use CharacterStringModule, only: CharacterStringType
291296
use SimVariablesModule, only: idm_context, simfile
292297
use DistributedSimModule, only: DistributedSimType, get_dsim
293-
use SourceCommonModule, only: idm_subcomponent_type, ifind_charstr
298+
use SourceCommonModule, only: idm_subcomponent_type, ifind_charstr, &
299+
inlen_check
294300
use SourceLoadModule, only: create_input_loader, remote_model_ndim
295301
! -- dummy
296302
integer(I4B), intent(in) :: iout
@@ -348,8 +354,8 @@ subroutine load_exchanges(iout)
348354
! -- attributes for this exchange
349355
exgtype = etypes(n)
350356
efname = efiles(n)
351-
mname1 = emnames_a(n)
352-
mname2 = emnames_b(n)
357+
call inlen_check(emnames_a(n), mname1, LENMODELNAME, 'MODELNAME')
358+
call inlen_check(emnames_b(n), mname2, LENMODELNAME, 'MODELNAME')
353359
!
354360
! initialize mempath as no path
355361
emempaths(n) = ''
@@ -364,6 +370,10 @@ subroutine load_exchanges(iout)
364370
if (m1_idx <= 0) errmsg = trim(errmsg)//' '//trim(mname1)
365371
if (m2_idx <= 0) errmsg = trim(errmsg)//' '//trim(mname2)
366372
call store_error(errmsg)
373+
end if
374+
!
375+
! -- terminate if errors were detected
376+
if (count_errors() > 0) then
367377
call store_error_filename(simfile)
368378
end if
369379
!

src/Utilities/Idm/ModelPackageInputs.f90

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module ModelPackageInputsModule
1010
use SimVariablesModule, only: errmsg
1111
use ConstantsModule, only: LINELENGTH, LENMEMPATH, LENMODELNAME, LENFTYPE, &
1212
LENPACKAGETYPE, LENPACKAGENAME, LENCOMPONENTNAME
13-
use SimModule, only: store_error, store_error_filename
13+
use SimModule, only: store_error, count_errors, store_error_filename
1414
use SimVariablesModule, only: iout
1515
use ArrayHandlersModule, only: expandarray
1616
use CharacterStringModule, only: CharacterStringType
@@ -384,6 +384,7 @@ end subroutine modelpkgs_add
384384
subroutine modelpkgs_addpkgs(this)
385385
! -- modules
386386
use MemoryManagerModule, only: mem_setptr
387+
use SourceCommonModule, only: inlen_check
387388
! -- dummy
388389
class(ModelPackageInputsType) :: this
389390
! -- local
@@ -410,12 +411,17 @@ subroutine modelpkgs_addpkgs(this)
410411
! -- attributes for this package
411412
ftype = ftypes(n)
412413
fname = fnames(n)
413-
pname = pnames(n)
414+
call inlen_check(pnames(n), pname, LENPACKAGENAME, 'PACKAGENAME')
414415
!
415416
! -- add this instance to package list
416417
call this%add(ftype, fname, pname)
417418
end do
418419
!
420+
! -- terminate if errors were detected
421+
if (count_errors() > 0) then
422+
call store_error_filename(this%modelfname)
423+
end if
424+
!
419425
! --
420426
return
421427
end subroutine modelpkgs_addpkgs

src/Utilities/Idm/SourceCommon.f90

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module SourceCommonModule
2121
public :: file_ext
2222
public :: ifind_charstr
2323
public :: filein_fname
24+
public :: inlen_check
2425

2526
contains
2627

@@ -445,7 +446,6 @@ end function ifind_charstr
445446
!<
446447
function filein_fname(filename, tagname, input_mempath, input_fname) &
447448
result(found)
448-
use SimModule, only: store_error, store_error_filename
449449
use MemoryManagerModule, only: mem_setptr, get_isize
450450
use CharacterStringModule, only: CharacterStringType
451451
character(len=*), intent(inout) :: filename
@@ -483,4 +483,33 @@ function filein_fname(filename, tagname, input_mempath, input_fname) &
483483
return
484484
end function filein_fname
485485

486+
!> @brief store an error for input exceeding internal name length
487+
!<
488+
subroutine inlen_check(input_name, mf6_name, maxlen, name_type)
489+
use CharacterStringModule, only: CharacterStringType
490+
type(CharacterStringType), intent(in) :: input_name
491+
character(len=*), intent(inout) :: mf6_name
492+
integer(I4B), intent(in) :: maxlen
493+
character(len=*), intent(in) :: name_type
494+
character(len=LINELENGTH) :: input_str
495+
integer(I4B) :: ilen
496+
!
497+
! -- initialize
498+
mf6_name = ''
499+
input_str = input_name
500+
ilen = len_trim(input_str)
501+
if (ilen > maxlen) then
502+
write (errmsg, '(a,i0,a)') &
503+
'Input name "'//trim(input_str)//'" exceeds maximum allowed length (', &
504+
maxlen, ') for '//trim(name_type)//'.'
505+
call store_error(errmsg)
506+
end if
507+
!
508+
! -- set truncated name
509+
mf6_name = trim(input_str)
510+
!
511+
! -- return
512+
return
513+
end subroutine inlen_check
514+
486515
end module SourceCommonModule

0 commit comments

Comments
 (0)