Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
d1eab239
Commit
d1eab239
authored
Feb 20, 2019
by
Javier Peletier
Committed by
Arnim Läuger
Feb 20, 2019
Browse files
ESP32: remove use of luaM_free in file module (#2631)
* file: remove use of luaM_free * added safe pushlstring function
parent
23397260
Changes
3
Hide whitespace changes
Inline
Side-by-side
components/base_nodemcu/include/lnodeaux.h
View file @
d1eab239
...
...
@@ -65,4 +65,9 @@ inline bool luaX_valid_ref(lua_ref_t ref) {
return
ref
>
0
;
}
// luaX_pushlstring is the same as lua_pushlstring except it will return nonzero in case of error
// (instead of throwing an exception and never returning) and will put the error message on the top of the stack.
int
luaX_pushlstring
(
lua_State
*
L
,
const
char
*
s
,
size_t
l
);
#endif
components/base_nodemcu/lnodeaux.c
View file @
d1eab239
...
...
@@ -111,7 +111,30 @@ void luaX_unset_ref(lua_State* L, lua_ref_t* ref) {
// luaX_set_ref pins a reference to a lua object, provided a registry
// or stack position
void
luaX_set_ref
(
lua_State
*
L
,
int
idx
,
lua_ref_t
*
ref
)
{
luaX_unset_ref
(
L
,
ref
);
// make sure we free previous reference
luaX_unset_ref
(
L
,
ref
);
// make sure we free previous reference
lua_pushvalue
(
L
,
idx
);
// push on the stack the referenced index
*
ref
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
// set the reference (pops 1 value)
}
// pushlstring_t is a helper struct to provide a protected lua_pushlstring call
typedef
struct
{
const
char
*
s
;
size_t
l
;
}
pushlstring_t
;
// safe_pushlstring is a private function meant to be called in protected mode.
static
int
safe_pushlstring
(
lua_State
*
L
)
{
pushlstring_t
*
ps
=
(
pushlstring_t
*
)
lua_touserdata
(
L
,
-
1
);
lua_pushlstring
(
L
,
ps
->
s
,
ps
->
l
);
return
1
;
}
// luaX_pushlstring is the same as lua_pushlstring except it will return nonzero in case of error
// (instead of throwing an exception and never returning) and will put the error message on the top of the stack.
int
luaX_pushlstring
(
lua_State
*
L
,
const
char
*
s
,
size_t
l
)
{
lua_pushcfunction
(
L
,
&
safe_pushlstring
);
pushlstring_t
*
ps
=
(
pushlstring_t
*
)
lua_newuserdata
(
L
,
sizeof
(
pushlstring_t
));
ps
->
s
=
s
;
ps
->
l
=
l
;
return
lua_pcall
(
L
,
1
,
1
,
0
);
}
components/modules/file.c
View file @
d1eab239
...
...
@@ -2,6 +2,7 @@
#include "module.h"
#include "lauxlib.h"
#include "lnodeaux.h"
#include "lmem.h"
#include "platform.h"
...
...
@@ -140,7 +141,6 @@ static int file_obj_free( lua_State *L )
if
(
ud
->
fd
)
{
// close file if it's still open
vfs_close
(
ud
->
fd
);
ud
->
fd
=
0
;
}
return
0
;
...
...
@@ -383,12 +383,7 @@ static int file_stat( lua_State* L )
// g_read()
static
int
file_g_read
(
lua_State
*
L
,
int
n
,
int16_t
end_char
,
int
fd
)
{
static
char
*
heap_mem
=
NULL
;
// free leftover memory
if
(
heap_mem
)
{
luaM_free
(
L
,
heap_mem
);
heap_mem
=
NULL
;
}
char
*
heap_mem
=
NULL
;
if
(
n
<=
0
)
n
=
FILE_READ_CHUNK
;
...
...
@@ -396,19 +391,19 @@ static int file_g_read( lua_State* L, int n, int16_t end_char, int fd )
if
(
end_char
<
0
||
end_char
>
255
)
end_char
=
EOF
;
if
(
!
fd
)
return
luaL_error
(
L
,
"open a file first"
);
char
*
p
;
int
i
;
size_t
bufsize
=
n
;
if
(
n
>
LUAL_BUFFERSIZE
)
{
// get buffer from heap
p
=
heap_mem
=
luaM_malloc
(
L
,
n
);
p
=
heap_mem
=
luaM_malloc
(
L
,
bufsize
);
}
else
{
// small chunks go onto the stack
p
=
alloca
(
n
);
p
=
alloca
(
bufsize
);
}
n
=
vfs_read
(
fd
,
p
,
n
);
...
...
@@ -424,21 +419,24 @@ static int file_g_read( lua_State* L, int n, int16_t end_char, int fd )
i
=
n
;
}
int
err
=
0
;
if
(
i
==
0
||
n
==
VFS_RES_ERR
)
{
if
(
heap_mem
)
{
luaM_free
(
L
,
heap_mem
);
heap_mem
=
NULL
;
}
lua_pushnil
(
L
);
return
1
;
}
else
{
vfs_lseek
(
fd
,
-
(
n
-
i
),
VFS_SEEK_CUR
);
err
=
luaX_pushlstring
(
L
,
p
,
i
);
// On error it will return nonzero and leave a message on top of the stack.
}
vfs_lseek
(
fd
,
-
(
n
-
i
),
VFS_SEEK_CUR
);
lua_pushlstring
(
L
,
p
,
i
);
if
(
heap_mem
)
{
luaM_free
(
L
,
heap_mem
);
heap_mem
=
NULL
;
luaM_freearray
(
L
,
heap_mem
,
bufsize
,
char
);
}
if
(
err
){
lua_error
(
L
);
// luaX_pushlstring failed and the error message is on top of the stack. Throw it.
// never returns
}
return
1
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment