Skip to content

Commit 87d3c3b

Browse files
committed
libstore/filetransfer: Handle exceptions in headerCallback
Callbacks *must* never throw exceptions on the curl thread!
1 parent 1e42e55 commit 87d3c3b

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/libstore/filetransfer.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ struct curlFileTransfer : public FileTransfer
207207
result.urls.push_back(effectiveUriCStr);
208208
}
209209

210-
size_t headerCallback(void * contents, size_t size, size_t nmemb)
211-
{
210+
size_t headerCallback(void * contents, size_t size, size_t nmemb) noexcept
211+
try {
212212
size_t realSize = size * nmemb;
213213
std::string line((char *) contents, realSize);
214214
printMsg(lvlVomit, "got header for '%s': %s", request.uri, trim(line));
@@ -261,6 +261,15 @@ struct curlFileTransfer : public FileTransfer
261261
}
262262
}
263263
return realSize;
264+
} catch (...) {
265+
#if LIBCURL_VERSION_NUM >= 0x075700
266+
/* https://curl.se/libcurl/c/CURLOPT_HEADERFUNCTION.html:
267+
You can also abort the transfer by returning CURL_WRITEFUNC_ERROR. */
268+
callbackException = std::current_exception();
269+
return CURL_WRITEFUNC_ERROR;
270+
#else
271+
return realSize;
272+
#endif
264273
}
265274

266275
static size_t headerCallbackWrapper(void * contents, size_t size, size_t nmemb, void * userp)

0 commit comments

Comments
 (0)