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

bugfix: preserve 'empty_array_mt' behavior upon multiple loadings of the module #16

Closed

Conversation

thibaultcha
Copy link
Member

@thibaultcha thibaultcha commented Dec 17, 2016

Prior to this fix, when the module would be loaded several times
(by-passing the package.loaded lookup), the lua_cjson_new() function would
override the empty_array_mt table in the Lua Registry with a new one.
Comparison for equality between those tables would then fail, and the
encoding behavior would be broken.

This was discovered after loading cjson and cjson.safe in the same
application, resulting in two calls to lua_cjson_new() :D

Changelog:

  • only create a table in the registry if such a table isn't already created.
  • new test cases

module

Prior to this fix, when the module would be loaded several times
(by-passing `package.loaded`), the `lua_cjson_new` function would
override the `empty_array_mt` table in the registry with a new one.
Comparison for equality between those tables would then fail, and the
behavior would be broken.

This was discovered after loading `cjson` *and* `cjson.safe` in the same
application, resulting in two calls to `lua_cjson_new`.
@thibaultcha
Copy link
Member Author

@doujiang24 Would you be interested in reviewing this change as well? Thanks :)

Copy link
Member

@doujiang24 doujiang24 left a comment

Choose a reason for hiding this comment

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

@thibaultcha Sure, Nice Job 👍

* If multiple calls to lua_cjson_new() are made,
* this prevents overriding the table at the given
* registry's index with a new one.
*/
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add lua_pop(L, 1) is better here?

Copy link
Member Author

Choose a reason for hiding this comment

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

To pop the nil value from the stack? Yes, indeed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed :)

@agentzh
Copy link
Member

agentzh commented Dec 18, 2016

Merged with minor edits in the commit log message. Thanks!

@agentzh agentzh closed this Dec 18, 2016
@thibaultcha
Copy link
Member Author

Thanks! @doujiang24 Thanks for taking a look!

thibaultcha added a commit to Kong/kong that referenced this pull request Dec 20, 2016
Use a workaround until openresty/lua-cjson#16 is
included in an OpenResty formal release.
hbagdi added a commit to hbagdi/kong that referenced this pull request Nov 4, 2017
Since openresty/lua-cjson#16 has been merged
and Openresty we use includes this fix, we can get rid off the
workarounds in our code.
thibaultcha pushed a commit to Kong/kong that referenced this pull request Nov 8, 2017
Since openresty/lua-cjson#16 has been
merged in OpenResty, we can get rid of the workarounds in
place for this bug.

From #3013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants