Lua: don't double-close Lua state on missing hook

Message ID 20171012150333.22913-1-christian.storm@siemens.com
State Accepted
Headers show
Series
  • Lua: don't double-close Lua state on missing hook
Related show

Commit Message

Christian Storm Oct. 12, 2017, 3:03 p.m.
If the Lua hook function cannot be found, then lua_parser_fn() closes
the Lua state. Thereafter, parser() in parser/parser.c closes the Lua
state again via lua_parser_exit(), resulting in a segmentation fault:
    software =
    {
    version = "0.0.1";
    images: (
            {
            filename = "dummy.root";
            device = "/dev/mmcblk0p3";
            hook = "segfault";
        }
    );
    embedded-script = "";
    }

Hence, don't close the Lua stack in lua_parser_fn() but instead clean
up the stack removing the nil pushed to it via lua_getglobal().

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 corelib/lua_interface.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Stefano Babic Oct. 17, 2017, 7:19 a.m. | #1
Hi Christian,

On 12/10/2017 17:03, Christian Storm wrote:
> If the Lua hook function cannot be found, then lua_parser_fn() closes
> the Lua state. Thereafter, parser() in parser/parser.c closes the Lua
> state again via lua_parser_exit(), resulting in a segmentation fault:
>     software =
>     {
>     version = "0.0.1";
>     images: (
>             {
>             filename = "dummy.root";
>             device = "/dev/mmcblk0p3";
>             hook = "segfault";
>         }
>     );
>     embedded-script = "";
>     }
> 
> Hence, don't close the Lua stack in lua_parser_fn() but instead clean
> up the stack removing the nil pushed to it via lua_getglobal().
> 
> Signed-off-by: Christian Storm <christian.storm@siemens.com>
> ---
>  corelib/lua_interface.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
> index 39ed01f..8698a1d 100644
> --- a/corelib/lua_interface.c
> +++ b/corelib/lua_interface.c
> @@ -550,7 +550,7 @@ int lua_parser_fn(lua_State *L, const char *fcn, struct img_type *img)
>  
>  	lua_getglobal(L, fcn);
>  	if(!lua_isfunction(L, lua_gettop(L))) {
> -		lua_close(L);
> +		lua_pop(L, 1);
>  		TRACE("Script : no %s in script, exiting", fcn);
>  		return -1;
>  	}
> 


Acked-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
Stefano Babic Oct. 18, 2017, 3:03 p.m. | #2
On 12/10/2017 17:03, Christian Storm wrote:
> If the Lua hook function cannot be found, then lua_parser_fn() closes
> the Lua state. Thereafter, parser() in parser/parser.c closes the Lua
> state again via lua_parser_exit(), resulting in a segmentation fault:
>     software =
>     {
>     version = "0.0.1";
>     images: (
>             {
>             filename = "dummy.root";
>             device = "/dev/mmcblk0p3";
>             hook = "segfault";
>         }
>     );
>     embedded-script = "";
>     }
> 
> Hence, don't close the Lua stack in lua_parser_fn() but instead clean
> up the stack removing the nil pushed to it via lua_getglobal().
> 
> Signed-off-by: Christian Storm <christian.storm@siemens.com>
> ---
>  corelib/lua_interface.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
> index 39ed01f..8698a1d 100644
> --- a/corelib/lua_interface.c
> +++ b/corelib/lua_interface.c
> @@ -550,7 +550,7 @@ int lua_parser_fn(lua_State *L, const char *fcn, struct img_type *img)
>  
>  	lua_getglobal(L, fcn);
>  	if(!lua_isfunction(L, lua_gettop(L))) {
> -		lua_close(L);
> +		lua_pop(L, 1);
>  		TRACE("Script : no %s in script, exiting", fcn);
>  		return -1;
>  	}
> 

Applied to -master, thanks !

Best regards,
Stefano Babic

Patch

diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index 39ed01f..8698a1d 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -550,7 +550,7 @@  int lua_parser_fn(lua_State *L, const char *fcn, struct img_type *img)
 
 	lua_getglobal(L, fcn);
 	if(!lua_isfunction(L, lua_gettop(L))) {
-		lua_close(L);
+		lua_pop(L, 1);
 		TRACE("Script : no %s in script, exiting", fcn);
 		return -1;
 	}