bounds error with lua

Clearly defined bug reports and their fixes
Post Reply
rgladstone
Posts: 64
Joined: 15 Apr 2013, 16:23
Antispam: Yes

bounds error with lua

Post by rgladstone » 28 Jul 2020, 21:58

Hi all, the problem I am reporting should be triggered by most Elmer simulations when lua functionality is parsed. I haven't provided an example because my question is about how to solve this in principal. If someone can suggested a possible solution then I am happy to implement it on my (rather large) test case. If you need me to provide a minimal example let me know and I'll try to do so.

When compiling and running Elmer (latest version, elmerice branch, but I've checked the relevant code modules in devel branch and it has the same line numbers as I give below) with bounds checking turned on I get the following error:

Code: Select all

At line 1217 of file /projappl/project_2002875/source/elmer_RMG/elmerfem/fem/src/GeneralUtils.F90
Fortran runtime error: Substring out of bounds: upper bound (14) of 'lua_result' exceeds string length (1)
I believe the cause of this behaviour is that LEN(lua_result)=1, where lua_result is a fortran pointer.  This comes from the call to the intrinsic function c_f_pointer at line 237 in Lua.F90.  This function converts a c pointer to a fortran pointer.  I don't know how to interrogate the c pointer. The fortran pointer is of type character(kind=c_char, len=:). The line that triggers the bounds error presumably works fine when bounds checking is not used (or else how would lua be parsed!), but the fact that it triggers a bounds error means that bounds checking cannot be used more generally.

A solution might be to find a way to ensure that the fortran pointer sp has a suitable LEN after this c_f_pointer call.  Perhaps there are other possible solutions. I do not have any experience with Fortran/C interoperability and I don't know how to fix this.  Does anyone have any ideas?

Thanks!

Rupert Gladstone

kevinarden
Posts: 641
Joined: 25 Jan 2019, 01:28
Antispam: Yes

Re: bounds error with lua

Post by kevinarden » 29 Jul 2020, 01:13

My first guess would be to change the upper bound limit of the string, the upper bound is usually set in the decleration of the variable.

rgladstone
Posts: 64
Joined: 15 Apr 2013, 16:23
Antispam: Yes

Re: bounds error with lua

Post by rgladstone » 29 Jul 2020, 15:49

Hi Kevin, thanks for replying.

So the problem variable is a pointer to a string of kind c_char. It is defined as len=: I have not seen this syntax before. I tried setting a hard coded max length but it failed. Not sure if that is what you meant with upper bound? I'll paste some code here to make it clearer.

lua_result is defined like this:

Code: Select all

character(kind=c_char, len=:), pointer :: lua_result
It causes a bounds error at this line because lua_result has length 1 and result_len at this point is greater than 1 (actually 14 in my case):

Code: Select all

matcstr(1:result_len) = lua_result(1:result_len)
I naively tried defining it like this:

Code: Select all

character(kind=c_char, len=MAX_NAME_LEN), pointer :: lua_result
But then it failed a couple of lines earlier, at this line:

Code: Select all

lua_result => lua_popstring(LuaState, result_len)
I think this is because lua_popstring wants to return a length 1 pointer with the meaningful length of data to which the pointer points being given by result_len. But I already defined lua_result as having length more than one.

So if there is a way to modify lua_popstring or lua_tolstring to return something whose length matches the length of the data to which it points (or which it contains, if it could be an allocatable string for example) this might be a solution.

Curiously lua_tolstring contains this unused variable:

Code: Select all

character(kind=c_char, len=:), allocatable :: s
I guess whoever wrote this routine in Lua.F90 tried to use an allocatable string but failed and used a pointer instead...

Any further thoughts appreciated!

Regards,
Rupert

rgladstone
Posts: 64
Joined: 15 Apr 2013, 16:23
Antispam: Yes

Re: bounds error with lua

Post by rgladstone » 30 Jul 2020, 13:46

Another colleague found a fix for this, manipulating the pointer in Lua.F90 such that by the time it gets back to GeneralUtils.F90 it has a length!

I've committed this to a branch I'm working on in the elmerfem Repo. In case anyone is interested, the commit hash is:
222c9560c948450f24f1cc63c509c7f6ca250e86

Thanks!

Rupert

raback
Site Admin
Posts: 3697
Joined: 22 Aug 2009, 11:57
Antispam: Yes
Location: Espoo, Finland
Contact:

Re: bounds error with lua

Post by raback » 31 Jul 2020, 02:57

Hi Rupert,

Thanx for the fix! I guess it will migrate to elmerice and devel in due time...

-Peter

Post Reply