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

lua-resty-mysql variable self.packet_no bug #212

Closed
vensontao opened this issue Dec 13, 2016 · 4 comments
Closed

lua-resty-mysql variable self.packet_no bug #212

vensontao opened this issue Dec 13, 2016 · 4 comments

Comments

@vensontao
Copy link

lualib/resty/mysql.lua:191: bad argument #1 to 'strchar' (invalid value)

in mysql.lua file, the fucntion _send_packet(self, req, size)
self.packet_no = self.packet_no + 1
local packet = _set_byte3(size) .. strchar(self.packet_no) .. req

the variables self.packet_no can be 255 +1 =256, produce errors: bad argument #1 to 'strchar' (invalid value)
detail error information see openresty group url:
https://groups.google.com/forum/#!topic/openresty/j3a-x75x0D0

@agentzh
Copy link
Member

agentzh commented Dec 13, 2016

@vensontao Yeah, this is a corner case that the client sends more than 255 packets through a single resty.mysql object. We should check the integer overflow. Will you try the following patch on your side?

diff --git a/lib/resty/mysql.lua b/lib/resty/mysql.lua
index 008a267..fc8bb74 100644
--- a/lib/resty/mysql.lua
+++ b/lib/resty/mysql.lua
@@ -184,11 +184,16 @@ end
 local function _send_packet(self, req, size)
     local sock = self.sock

-    self.packet_no = self.packet_no + 1
+    local packet_no = self.packet_no
+    packet_no = packet_no + 1
+    if packet_no >= 256 then
+        packet_no = 1
+    end
+    self.packet_no = packet_no

     -- print("packet no: ", self.packet_no)

-    local packet = _set_byte3(size) .. strchar(self.packet_no) .. req
+    local packet = _set_byte3(size) .. strchar(packet_no) .. req

     -- print("sending packet: ", _dump(packet))

@vensontao
Copy link
Author

Great, This problem has been solved. thanks very much!

@vensontao
Copy link
Author

vensontao commented Dec 14, 2016 via email

@vensontao vensontao reopened this Dec 15, 2016
@vensontao
Copy link
Author

we can fix this issue in the next version of openresty/lua-resty-mysql.

mrytsr pushed a commit to bilibili/lua-resty-mysql that referenced this issue Apr 22, 2018
Mentioned by this issue openresty/openresty#212
Maybe better fix with one line
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

No branches or pull requests

2 participants