diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2007-06-11 23:44:54 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2007-06-11 23:44:54 +0000 |
commit | 3cd10f5ab6cda5c4a8db829dd38f25168edcfc4a (patch) | |
tree | f0c49eb250f5375414025a9e7022261e729ad905 /src/buffer.c | |
parent | 3074a8f56b5153f4477e662453102583d7b6f539 (diff) | |
download | luasocket-3cd10f5ab6cda5c4a8db829dd38f25168edcfc4a.tar.gz luasocket-3cd10f5ab6cda5c4a8db829dd38f25168edcfc4a.tar.bz2 luasocket-3cd10f5ab6cda5c4a8db829dd38f25168edcfc4a.zip |
Crashy bug fixed in recvraw.
Also fixed returns on closed socket.
Diffstat (limited to 'src/buffer.c')
-rw-r--r-- | src/buffer.c | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/src/buffer.c b/src/buffer.c index df1a0bc..de817b2 100644 --- a/src/buffer.c +++ b/src/buffer.c | |||
@@ -75,12 +75,12 @@ int buffer_meth_setstats(lua_State *L, p_buffer buf) { | |||
75 | \*-------------------------------------------------------------------------*/ | 75 | \*-------------------------------------------------------------------------*/ |
76 | int buffer_meth_send(lua_State *L, p_buffer buf) { | 76 | int buffer_meth_send(lua_State *L, p_buffer buf) { |
77 | int top = lua_gettop(L); | 77 | int top = lua_gettop(L); |
78 | p_timeout tm = timeout_markstart(buf->tm); | ||
79 | int err = IO_DONE; | 78 | int err = IO_DONE; |
80 | size_t size = 0, sent = 0; | 79 | size_t size = 0, sent = 0; |
81 | const char *data = luaL_checklstring(L, 2, &size); | 80 | const char *data = luaL_checklstring(L, 2, &size); |
82 | long start = (long) luaL_optnumber(L, 3, 1); | 81 | long start = (long) luaL_optnumber(L, 3, 1); |
83 | long end = (long) luaL_optnumber(L, 4, -1); | 82 | long end = (long) luaL_optnumber(L, 4, -1); |
83 | p_timeout tm = timeout_markstart(buf->tm); | ||
84 | if (start < 0) start = (long) (size+start+1); | 84 | if (start < 0) start = (long) (size+start+1); |
85 | if (end < 0) end = (long) (size+end+1); | 85 | if (end < 0) end = (long) (size+end+1); |
86 | if (start < 1) start = (long) 1; | 86 | if (start < 1) start = (long) 1; |
@@ -108,10 +108,10 @@ int buffer_meth_send(lua_State *L, p_buffer buf) { | |||
108 | \*-------------------------------------------------------------------------*/ | 108 | \*-------------------------------------------------------------------------*/ |
109 | int buffer_meth_receive(lua_State *L, p_buffer buf) { | 109 | int buffer_meth_receive(lua_State *L, p_buffer buf) { |
110 | int err = IO_DONE, top = lua_gettop(L); | 110 | int err = IO_DONE, top = lua_gettop(L); |
111 | p_timeout tm = timeout_markstart(buf->tm); | ||
112 | luaL_Buffer b; | 111 | luaL_Buffer b; |
113 | size_t size; | 112 | size_t size; |
114 | const char *part = luaL_optlstring(L, 3, "", &size); | 113 | const char *part = luaL_optlstring(L, 3, "", &size); |
114 | p_timeout tm = timeout_markstart(buf->tm); | ||
115 | /* initialize buffer with optional extra prefix | 115 | /* initialize buffer with optional extra prefix |
116 | * (useful for concatenating previous partial results) */ | 116 | * (useful for concatenating previous partial results) */ |
117 | luaL_buffinit(L, &b); | 117 | luaL_buffinit(L, &b); |
@@ -182,13 +182,14 @@ static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent) { | |||
182 | static int recvraw(p_buffer buf, size_t wanted, luaL_Buffer *b) { | 182 | static int recvraw(p_buffer buf, size_t wanted, luaL_Buffer *b) { |
183 | int err = IO_DONE; | 183 | int err = IO_DONE; |
184 | size_t total = 0; | 184 | size_t total = 0; |
185 | while (total < wanted && err == IO_DONE) { | 185 | while (err == IO_DONE) { |
186 | size_t count; const char *data; | 186 | size_t count; const char *data; |
187 | err = buffer_get(buf, &data, &count); | 187 | err = buffer_get(buf, &data, &count); |
188 | count = MIN(count, wanted - total); | 188 | count = MIN(count, wanted - total); |
189 | luaL_addlstring(b, data, count); | 189 | luaL_addlstring(b, data, count); |
190 | buffer_skip(buf, count); | 190 | buffer_skip(buf, count); |
191 | total += count; | 191 | total += count; |
192 | if (total >= wanted) break; | ||
192 | } | 193 | } |
193 | return err; | 194 | return err; |
194 | } | 195 | } |
@@ -198,14 +199,18 @@ static int recvraw(p_buffer buf, size_t wanted, luaL_Buffer *b) { | |||
198 | \*-------------------------------------------------------------------------*/ | 199 | \*-------------------------------------------------------------------------*/ |
199 | static int recvall(p_buffer buf, luaL_Buffer *b) { | 200 | static int recvall(p_buffer buf, luaL_Buffer *b) { |
200 | int err = IO_DONE; | 201 | int err = IO_DONE; |
202 | size_t total = 0; | ||
201 | while (err == IO_DONE) { | 203 | while (err == IO_DONE) { |
202 | const char *data; size_t count; | 204 | const char *data; size_t count; |
203 | err = buffer_get(buf, &data, &count); | 205 | err = buffer_get(buf, &data, &count); |
206 | total += count; | ||
204 | luaL_addlstring(b, data, count); | 207 | luaL_addlstring(b, data, count); |
205 | buffer_skip(buf, count); | 208 | buffer_skip(buf, count); |
206 | } | 209 | } |
207 | if (err == IO_CLOSED) return IO_DONE; | 210 | if (err == IO_CLOSED) { |
208 | else return err; | 211 | if (total > 0) return IO_DONE; |
212 | else return IO_CLOSED; | ||
213 | } else return err; | ||
209 | } | 214 | } |
210 | 215 | ||
211 | /*-------------------------------------------------------------------------*\ | 216 | /*-------------------------------------------------------------------------*\ |