Skip to content

Commit

Permalink
Fix terrible type converting (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
qishipai authored Jul 7, 2024
1 parent 0226e0d commit 630a4dc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
11 changes: 6 additions & 5 deletions curl_cffi/curl.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,18 @@ def setopt(self, option: CurlOpt, value: Any) -> int:
input_option = {
# this should be int in curl, but cffi requires pointer for void*
# it will be convert back in the glue c code.
0: "int*",
0: "long*",
10000: "char*",
20000: "void*",
30000: "int*", # offset type
30000: "int64_t*", # offset type
40000: "void*", # blob type
}
# print("option", option, "value", value)

# Convert value
value_type = input_option.get(int(option / 10000) * 10000)
if value_type == "int*":
c_value = ffi.new("int*", value)
value_type = input_option.get((option // 10000) * 10000)
if value_type == "long*" or value_type == "int64_t*":
c_value = ffi.new(value_type, value)
elif option == CurlOpt.WRITEDATA:
c_value = ffi.new_handle(value)
self._write_handle = c_value
Expand Down
12 changes: 7 additions & 5 deletions ffi/shim.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include "shim.h"

#define INTEGER_OPTION_MAX 10000
#include "shim.h"

int _curl_easy_setopt(void* curl, int option, void* parameter) {
// printf("****** hijack test begins: \n");
// int val = curl_easy_setopt(instance->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
// printf("****** hijack test ends. opt: %d, val: %d, result is: %d\n", CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0, val);
CURLoption opt_value = (CURLoption) option;
// CURLoption opt_value = (CURLoption) option;
// printf("option: %d, setopt parameter: %d\n", option, *(int*)parameter);
// for integer options, we need to convert param from pointers to integers
if (option < INTEGER_OPTION_MAX) {
return (int)curl_easy_setopt(curl, (CURLoption)option, *(int*)parameter);
if (option < CURLOPTTYPE_OBJECTPOINT) {
return (int)curl_easy_setopt(curl, (CURLoption)option, *(long*)parameter);
}
if (CURLOPTTYPE_OFF_T <= option && option < CURLOPTTYPE_BLOB) {
return (int)curl_easy_setopt(curl, (CURLoption)option, *(curl_off_t*)parameter);
}
return (int)curl_easy_setopt(curl, (CURLoption)option, parameter);
}

0 comments on commit 630a4dc

Please sign in to comment.