block/curl: Improve type safety of s->timeout.
PATCH 1/1 block/curl: Improve type safety of s->timeout.
This patch is identical to the previous version, except that I have rebased it on top of current qemu HEAD.
Previous discussion:
https://lists.gnu.org/archive/html/qemu-devel/2014-10/threads.html#00518
PATCH 1/1 block/curl: Improve type safety of s->timeout.
qemu_opt_get_number returns a uint64_t, and curl_easy_setopt expects a long (not an int).
Store the timeout (which is a positive number of seconds) as a uint64_t. Check that the number given by the user is reasonable. Cast it to long before calling curl_easy_setopt.
Example error message after this change has been applied:
$ ./qemu-img create -f qcow2 /tmp/test.qcow2 \
-b 'json: { "file.driver":"https",
"file.url":"https://foo/bar",
"file.timeout":-1 }'
qemu-img: /tmp/test.qcow2: Could not open 'json: { "file.driver":"https", "file.url":"https://foo/bar", "file.timeout":-1 }': timeout parameter is too large or negative: Invalid argumentSigned-off-by Richard W.M. Jones <[email protected]>
Reviewed-by Laszlo Ersek <[email protected]>
--- block/curl.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
block/curl.c +6 -2
@@ -112,7 +112,7 @@ typedef struct BDRVCURLState {char *url;size_t readahead_size;bool sslverify; -int timeout; +uint64_t timeout;char *cookie;bool accept_range;AioContext *aio_context;
@@ -390,7 +390,7 @@ static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s)if (s->cookie) {curl_easy_setopt (state->curl,CURLOPT_COOKIE , s->cookie); } -curl_easy_setopt (state->curl,CURLOPT_TIMEOUT , s->timeout); +curl_easy_setopt (state->curl,CURLOPT_TIMEOUT , (long )s->timeout);curl_easy_setopt (state->curl,CURLOPT_WRITEFUNCTION , (void *)curl_read_cb);curl_easy_setopt (state->curl,CURLOPT_WRITEDATA , (void *)state);
@@ -546,6 +546,10 @@ static int curl_open(BlockDriverState *bs, QDict *options, int flags, s->timeout =qemu_opt_get_number (opts,CURL_BLOCK_OPT_TIMEOUT ,CURL_TIMEOUT_DEFAULT ); +if (s->timeout >100000 ) { +error_setg (errp,"timeout parameter is too large or negative" ); + goto out_noclean; + } s->sslverify =qemu_opt_get_bool (opts,CURL_BLOCK_OPT_SSLVERIFY , true);
Signature · 1 line
2.0.4