Skip to content

Commit 369762b

Browse files
committed
[Multi-Thread] Remove cJSON_GetErrorPtr in examples and authentication plugin code to ensure thread safety
1 parent 5bbb86d commit 369762b

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

examples/multi_thread/watch_pod.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ static void on_pod_event_comes(const char *event_string)
1212
return;
1313
}
1414

15-
cJSON *event_json_obj = cJSON_Parse(event_string);
15+
char *type = NULL;
16+
v1_pod_t *pod = NULL;
17+
18+
const char *parse_end = NULL;
19+
cJSON *event_json_obj = cJSON_ParseWithOpts(event_string, &parse_end, 1);
1620
if (!event_json_obj) {
17-
fprintf(stderr, "%s: Cannot create JSON from string.[%s].\n", fname, cJSON_GetErrorPtr());
21+
fprintf(stderr, "%s: Cannot create JSON from string: [%s].\n", fname, parse_end);
1822
goto end;
1923
}
2024

@@ -23,15 +27,15 @@ static void on_pod_event_comes(const char *event_string)
2327
fprintf(stderr, "%s: Cannot get type in watch event.\n", fname);
2428
goto end;
2529
}
26-
char *type = strdup(json_value_type->valuestring);
30+
type = strdup(json_value_type->valuestring);
2731
printf("type: %s\n", type);
2832

2933
cJSON *json_value_object = cJSON_GetObjectItem(event_json_obj, WATCH_EVENT_KEY_OBJECT);
3034
if (!json_value_object || json_value_object->type != cJSON_Object) {
3135
fprintf(stderr, "%s: Cannot get object in watch event.\n", fname);
3236
goto end;
3337
}
34-
v1_pod_t *pod = v1_pod_parseFromJSON(json_value_object);
38+
pod = v1_pod_parseFromJSON(json_value_object);
3539
if (!pod) {
3640
fprintf(stderr, "%s: Cannot get pod from watch event object.\n", fname);
3741
goto end;

examples/watch_list_pod/main.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ void on_pod_event_comes(const char *event_string)
1818
}
1919
printf("\nwatch event raw string:\n%s\n\n", event_string);
2020

21-
cJSON *event_json_obj = cJSON_Parse(event_string);
21+
char *type = NULL;
22+
v1_pod_t *pod = NULL;
23+
24+
const char *parse_end = NULL;
25+
cJSON *event_json_obj = cJSON_ParseWithOpts(event_string, &parse_end, 1);
2226
if (!event_json_obj) {
23-
fprintf(stderr, "%s: Cannot create JSON from string.[%s].\n", fname, cJSON_GetErrorPtr());
27+
fprintf(stderr, "%s: Cannot create JSON from string: [%s].\n", fname, parse_end);
2428
goto end;
2529
}
2630

@@ -29,15 +33,15 @@ void on_pod_event_comes(const char *event_string)
2933
fprintf(stderr, "%s: Cannot get type in watch event.\n", fname);
3034
goto end;
3135
}
32-
char *type = strdup(json_value_type->valuestring);
36+
type = strdup(json_value_type->valuestring);
3337
printf("type: %s\n", type);
3438

3539
cJSON *json_value_object = cJSON_GetObjectItem(event_json_obj, WATCH_EVENT_KEY_OBJECT);
3640
if (!json_value_object || json_value_object->type != cJSON_Object) {
3741
fprintf(stderr, "%s: Cannot get object in watch event.\n", fname);
3842
goto end;
3943
}
40-
v1_pod_t *pod = v1_pod_parseFromJSON(json_value_object);
44+
pod = v1_pod_parseFromJSON(json_value_object);
4145
if (!pod) {
4246
fprintf(stderr, "%s: Cannot get pod from watch event object.\n", fname);
4347
goto end;

kubernetes/config/authn_plugin/authn_plugin_util.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ int shc_request(char **p_http_response, int *p_http_response_length, char *type,
1515
int rc = http_client->response_code;
1616
switch (rc) {
1717
case HTTP_RC_OK:
18-
*p_http_response = strndup((char *)http_client->dataReceived, http_client->dataReceivedLen);
18+
*p_http_response = strndup((char *) http_client->dataReceived, http_client->dataReceivedLen);
1919
*p_http_response_length = http_client->dataReceivedLen;
2020
break;
2121
default:
2222
printf("%s: response_code=%ld\n", fname, http_client->response_code);
2323
if (http_client->dataReceived) {
24-
printf("%s: %s\n", fname, (char *)http_client->dataReceived);
24+
printf("%s: %s\n", fname, (char *) http_client->dataReceived);
2525
}
2626
break;
2727
}
@@ -50,9 +50,10 @@ char *shc_get_string_from_json(const char *json_string, const char *key)
5050
return NULL;
5151
}
5252

53-
cJSON *json = cJSON_Parse(json_string);
53+
const char *parse_end = NULL;
54+
cJSON *json = cJSON_ParseWithOpts(json_string, &parse_end, 1);
5455
if (!json) {
55-
fprintf(stderr, "%s: Cannot create JSON from string.[%s].\n", fname, cJSON_GetErrorPtr());
56+
fprintf(stderr, "%s: Cannot create JSON from string: [%s].\n", fname, parse_end);
5657
return NULL;
5758
}
5859
cJSON *value = cJSON_GetObjectItem(json, key);

kubernetes/config/authn_plugin/plugins/oidc/libkubernetes_oidc.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ static time_t get_token_expiration_time(const char *token_string)
7070
goto end;
7171
}
7272

73-
cJSON *payload_JSON = cJSON_Parse(b64decode);
73+
const char *parse_end = NULL;
74+
cJSON *payload_JSON = cJSON_ParseWithOpts(b64decode, &parse_end, 1);
7475
if (!payload_JSON) {
75-
fprintf(stderr, "%s: Cannot create JSON from string.[%s].\n", fname, cJSON_GetErrorPtr());
76+
fprintf(stderr, "%s: Cannot create JSON from string.[%s].\n", fname, parse_end);
7677
goto end;
7778
}
7879
cJSON *json_value = cJSON_GetObjectItem(payload_JSON, OIDC_ID_TOKEN_EXP);

0 commit comments

Comments
 (0)