Skip to content

Commit 4a95a93

Browse files
committed
Merge pull request #10 from FunkyAss/fix-callback
Fix segmentation fault and attempt to call nil in callbacks
2 parents 93f3d0c + f307002 commit 4a95a93

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/Lua-cURL-callback.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
******************************************************************************/
2323

2424
#include <string.h>/* memcpy */
25+
#include <stdio.h> /* stdin, stdout */
2526

2627
#include "Lua-cURL.h"
2728
#include "Lua-utility.h"
@@ -95,19 +96,25 @@ int l_easy_clear_headerfunction(lua_State *L, CURL* curl) {
9596
l_easy_private *privatep = luaL_checkudata(L, 1, LUACURL_EASYMETATABLE);
9697
if (curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, NULL) != CURLE_OK)
9798
luaL_error(L, "%s", privatep->error);
99+
if (curl_easy_setopt(curl, CURLOPT_WRITEHEADER, NULL) != CURLE_OK)
100+
luaL_error(L, "%s", privatep->error);
98101
return 0;
99102
}
100103

101104
int l_easy_clear_writefunction(lua_State *L, CURL* curl) {
102105
l_easy_private *privatep = luaL_checkudata(L, 1, LUACURL_EASYMETATABLE);
103106
if (curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL) != CURLE_OK)
104107
luaL_error(L, "%s", privatep->error);
108+
if (curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout) != CURLE_OK)
109+
luaL_error(L, "%s", privatep->error);
105110
return 0;
106111
}
107112

108113
int l_easy_clear_readfunction(lua_State *L, CURL* curl) {
109114
l_easy_private *privatep = luaL_checkudata(L, 1, LUACURL_EASYMETATABLE);
110115
if (curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL) != CURLE_OK)
111116
luaL_error(L, "%s", privatep->error);
117+
if (curl_easy_setopt(curl, CURLOPT_READDATA, stdin) != CURLE_OK)
118+
luaL_error(L, "%s", privatep->error);
112119
return 0;
113120
}

src/Lua-cURL.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ int l_easy_perform(lua_State *L) {
128128
int headerfunction;
129129
/* do readcallback */
130130
int readfunction;
131+
/* curl_easy_perform return code*/
132+
CURLcode perform_status;
131133

132134
/* check optional callback table */
133135
luaL_opt(L, luaL_checktable, 2, lua_newtable(L));
@@ -155,8 +157,7 @@ int l_easy_perform(lua_State *L) {
155157

156158

157159
/* callback table is on top on stack */
158-
if (curl_easy_perform(curl) != CURLE_OK)
159-
luaL_error(L, "%s", privatep->error);
160+
perform_status = curl_easy_perform(curl);
160161

161162
/* unset callback functions */
162163
if (headerfunction)
@@ -165,6 +166,10 @@ int l_easy_perform(lua_State *L) {
165166
l_easy_clear_writefunction(L, privatep->curl);
166167
if (readfunction)
167168
l_easy_clear_readfunction(L, privatep->curl);
169+
170+
if (perform_status != CURLE_OK)
171+
return luaL_error(L, "%s", privatep->error);
172+
168173
return 0;
169174
}
170175

0 commit comments

Comments
 (0)