Skip to content

Commit a56b6aa

Browse files
committed
bugfix: don't use string.format to concat binary string
1 parent 19e6462 commit a56b6aa

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

examples/client.lua

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ local fd = assert(socket.connect("127.0.0.1", 8888))
88

99
local function send_package(fd, pack)
1010
local size = #pack
11-
local package = string.format("%c%c%s",
12-
bit32.extract(size,8,8),
13-
bit32.extract(size,0,8),
14-
pack)
11+
local package = string.char(bit32.extract(size,8,8)) ..
12+
string.char(bit32.extract(size,0,8))..
13+
pack
1514

1615
socket.send(fd, package)
1716
end

examples/login/client.lua

+10-13
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,13 @@ print("login ok, subid=", subid)
9393

9494
local function send_request(v, session)
9595
local size = #v + 4
96-
local package = string.format("%c%c%s%c%c%c%c",
97-
bit32.extract(size,8,8),
98-
bit32.extract(size,0,8),
99-
v,
100-
bit32.extract(session,24,8),
101-
bit32.extract(session,16,8),
102-
bit32.extract(session,8,8),
103-
bit32.extract(session,0,8)
104-
)
96+
local package = string.char(bit32.extract(size,8,8))..
97+
string.char(bit32.extract(size,0,8))..
98+
v..
99+
string.char(bit32.extract(session,24,8))..
100+
string.char(bit32.extract(session,16,8))..
101+
string.char(bit32.extract(session,8,8))..
102+
string.char(bit32.extract(session,0,8))
105103

106104
socket.send(fd, package)
107105
return v, session
@@ -135,10 +133,9 @@ local readpackage = unpack_f(unpack_package)
135133

136134
local function send_package(fd, pack)
137135
local size = #pack
138-
local package = string.format("%c%c%s",
139-
bit32.extract(size,8,8),
140-
bit32.extract(size,0,8),
141-
pack)
136+
local package = string.char(bit32.extract(size,8,8))..
137+
string.char(bit32.extract(size,0,8))..
138+
pack
142139

143140
socket.send(fd, package)
144141
end

examples/simpleweb.lua

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ local socket = require "socket"
33
local httpd = require "http.httpd"
44
local sockethelper = require "http.sockethelper"
55
local urllib = require "http.url"
6+
local table = table
7+
local string = string
68

79
local mode = ...
810

0 commit comments

Comments
 (0)