kubernetes-client / c

Official C client library for Kubernetes
Apache License 2.0
144 stars 46 forks source link

Fix memory leak when a JSON of model fails to parse #47

Closed ityuhui closed 3 years ago

ityuhui commented 3 years ago

The valgrind test reports a memory leak for the example delete_pod

valgrind --tool=memcheck --leak-check=full ./delete_pod_bin
==32029== Memcheck, a memory error detector
==32029== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==32029== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==32029== Command: ./delete_pod_bin
==32029==
OK
The return code of HTTP request=200
The pod is deleted successfully.
==32029==
==32029== HEAP SUMMARY:
==32029==     in use at exit: 82 bytes in 3 blocks
==32029==   total heap usage: 8,728 allocs, 8,725 frees, 1,057,554 bytes allocated
==32029==
==32029== 82 (32 direct, 50 indirect) bytes in 1 blocks are definitely lost in loss record 3 of 3
==32029==    at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==32029==    by 0x4EEBFA1: v1_list_meta_create (v1_list_meta.c:14)
==32029==    by 0x4EEC2F2: v1_list_meta_parseFromJSON (v1_list_meta.c:130)
==32029==    by 0x4F1D1B2: v1_status_parseFromJSON (v1_status.c:206)
==32029==    by 0x5044D9C: CoreV1API_deleteNamespacedPod (CoreV1API.c:14789)
==32029==    by 0x108B45: delete_a_pod (main.c:10)
==32029==    by 0x108C94: main (main.c:53)
==32029==
==32029== LEAK SUMMARY:
==32029==    definitely lost: 32 bytes in 1 blocks
==32029==    indirectly lost: 50 bytes in 2 blocks
==32029==      possibly lost: 0 bytes in 0 blocks
==32029==    still reachable: 0 bytes in 0 blocks
==32029==         suppressed: 0 bytes in 0 blocks
==32029==
==32029== For counts of detected and suppressed errors, rerun with: -v
==32029== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

The reason is: Deleting a pod successfully will return a v1_pod_t other than a v1_status_t from Kubernetes API server, but the generated client code still wants to parse the JSON string to create a v1_status_t in the function v1_status_parseFromJSON, the detail can refer to https://github.com/kubernetes-client/c/issues/31#issuecomment-704321954

v1_status_t *v1_status_parseFromJSON(cJSON *v1_statusJSON) will return NULL because the parameter v1_statusJSON is not a valid v1_status_t JSON, but metadata_local_nonprim (member of v1_status_t) is created and its memory is not released before the function returns, this will cause memory leak.

I fixed the issue by this PR in the openapi-generator repo, now re-generate the C client to merge the fix.

After this fix, valgrind does not reports memory leak:

==28375== All heap blocks were freed -- no leaks are possible
ityuhui commented 3 years ago

/cc @brendandburns

ityuhui commented 3 years ago

Hi @brendandburns

Could you please review this PR if you have time ?

brendandburns commented 3 years ago

/lgtm /approve

(many apologies for the delay!)

k8s-ci-robot commented 3 years ago

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brendandburns, ityuhui

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files: - ~~[OWNERS](https://github.com/kubernetes-client/c/blob/master/OWNERS)~~ [brendandburns,ityuhui] Approvers can indicate their approval by writing `/approve` in a comment Approvers can cancel approval by writing `/approve cancel` in a comment
ityuhui commented 3 years ago

It doesn’t matter. Thank you @brendandburns