Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32: remove use of luaM_free in file module #2631

Merged
merged 2 commits into from
Feb 20, 2019

Conversation

jpeletier
Copy link
Contributor

Works toward fixing #2377 for ESP32

  • This PR is for the dev branch rather than for master.
  • This PR is compliant with the other contributing guidelines as well (if not, please describe why).
  • I have thoroughly tested my contribution.
  • The code changes are reflected in the documentation at docs/en/*.

modifications in file_g_read() to avoid use of luaM_free()

@@ -383,32 +383,27 @@ 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code was added to free memory which was left allocated by a previous call. Standard execution flow doesn't need that, but heap_mem isn't freed in case lua_pushlstring() throws a memory exception as noted in #1541 (comment).

If we keep considering this corner case then bufsize would also need to be declared static.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devsaurus I added a new function to make a protected call to lua_pushlstring() and avoid this issue. The new luaX_pushlstring() returns nonzero instead of throwing an exception, allowing you to free memory. This function could be useful elsewhere.

Please check it out and let me know :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent solution!

Copy link
Member

@devsaurus devsaurus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good and is ready to merge IMO.

@devsaurus devsaurus merged commit d1eab23 into nodemcu:dev-esp32 Feb 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants