kubernetes-client / c

Official C client library for Kubernetes
Apache License 2.0
141 stars 45 forks source link

Unexpected processing behavior of null value(string type) in yaml/json #161

Closed k8sgogo closed 1 year ago

k8sgogo commented 1 year ago

Unexpected processing behavior of null value(string type) in yaml/json

{
  "kind": "NodeList",
  "apiVersion": "v1",
  "metadata": {
    "continue": ""
  },
  "items": [
    {
      "kind": "Node",
 ...
      "status": {
 ...
        "phase": null
      }
    }
  ]
}

I noticed an old issue in #141 , which missed one senario: if a subfield is null which original type is string, the status structure is null when parsed from the above JSON

The code is as follows. cJSON_IsString(phase) is false, and causes goto end inv1_node_status_parseFromJSON

// v1_node_status->phase
cJSON *phase = cJSON_GetObjectItemCaseSensitive(v1_node_statusJSON, "phase");
if (phase) { 
if(!cJSON_IsString(phase))
{
goto end; //String
}
}

We have made the following quick fixes.

--- a/c/kubernetes/model/v1_node_status.c
+++ b/c/kubernetes/model/v1_node_status.c
@@ -478,16 +478,23 @@ v1_node_status_t *v1_node_status_parseFromJSON(cJSON *v1_node_statusJSON){

     // v1_node_status->phase
     cJSON *phase = cJSON_GetObjectItemCaseSensitive(v1_node_statusJSON, "phase");
-    if (phase) { 
+    if (phase) {
+    if(cJSON_IsNull(phase)) {
+        phase = NULL;
+    } else {
     if(!cJSON_IsString(phase))
     {
     goto end; //String
     }
     }
+    }

Null phase will be ok in final v1_node_status_create.

v1_node_status_local_var = v1_node_status_create (
    addresses ? addressesList : NULL,
    allocatable ? allocatableList : NULL,
    capacity ? capacityList : NULL,
    conditions ? conditionsList : NULL,
    config ? config_local_nonprim : NULL,
    daemon_endpoints ? daemon_endpoints_local_nonprim : NULL,
    images ? imagesList : NULL,
    node_info ? node_info_local_nonprim : NULL,
    phase ? strdup(phase->valuestring) : NULL,
    volumes_attached ? volumes_attachedList : NULL,
    volumes_in_use ? volumes_in_useList : NULL
    );

We would like to ask you to fix this issue. Thanks in advance.

ityuhui commented 1 year ago

Thanks for reporting this issue. We'll fix it soon.