kubernetes-client / c

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

Memory Leak in CoreV1API #96

Closed minerba closed 2 years ago

minerba commented 2 years ago

Hi

I found some defect that Memory Leak in CoreV1API.c

In the "CoreV1API_deleteNamespacedPod" function, the localVarToReplace variable calls malloc, but if the __namespace variable is NULL, it moves to end and the function ends without calling free. Other functions are also confirmed to have memory leak.

It needs to be fixed as soon as possible.

ityuhui commented 2 years ago

Could you please provide a sample (yaml) or gdb call stack ?

The example delete_pod has passed valgrind test. No memory leak is reported.

minerba commented 2 years ago

Hi @ityuhui

In normal cases, memory leak does not occur, but if (__namespace == NULL) Memory leak occurs.

  char* localVarToReplace_name = malloc(sizeOfPathParams_name);
    sprintf(localVarToReplace_name, "{%s}", "name");

    localVarPath = strReplace(localVarPath, localVarToReplace_name, name);

    // Path Params
    long sizeOfPathParams_namespace = strlen(name)+3 + strlen(_namespace)+3 + strlen("{ namespace }");
    if(_namespace == NULL) {
        goto end;
    }

============= end ==============

end:
    free(localVarPath);
    return NULL;
ityuhui commented 2 years ago

Thank you @minerba I got it.

It's a problem. But I think its severity is low because only if _namespace == NULL, it will reproduce. So I will fix this some times later.


The fix solution is:

  1. define all pathParams at the begining and initialize their value to "NULL"
  2. free all non-NULL pathParams at the end code block. https://github.com/OpenAPITools/openapi-generator/blob/fbb61658dfbcc2c1f5087511b0f74756553791ad/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache#L116
minerba commented 2 years ago

@ityuhui thanks for your reply. I hope it will be fixed soon

k8s-triage-robot commented 2 years ago

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot commented 2 years ago

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

k8s-triage-robot commented 2 years ago

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

k8s-ci-robot commented 2 years ago

@k8s-triage-robot: Closing this issue.

In response to [this](https://github.com/kubernetes-client/c/issues/96#issuecomment-1125169875): >The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs. > >This bot triages issues and PRs according to the following rules: >- After 90d of inactivity, `lifecycle/stale` is applied >- After 30d of inactivity since `lifecycle/stale` was applied, `lifecycle/rotten` is applied >- After 30d of inactivity since `lifecycle/rotten` was applied, the issue is closed > >You can: >- Reopen this issue or PR with `/reopen` >- Mark this issue or PR as fresh with `/remove-lifecycle rotten` >- Offer to help out with [Issue Triage][1] > >Please send feedback to sig-contributor-experience at [kubernetes/community](https://github.com/kubernetes/community). > >/close > >[1]: https://www.kubernetes.dev/docs/guide/issue-triage/ Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.