Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak when kubeconfig is invalid #169

Merged
merged 3 commits into from
Jan 17, 2023

Conversation

ityuhui
Copy link
Member

@ityuhui ityuhui commented Jan 16, 2023

  1. Fixes Memory Leak in kubeyaml_load_kubeconfig() in kubernetes/config/kube_config_yaml.c #134

  2. Add a test for this case

  3. Check and fix the code style

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jan 16, 2023
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jan 16, 2023
@ityuhui
Copy link
Member Author

ityuhui commented Jan 16, 2023

The valgrind result for old example

# make memcheck
valgrind --tool=memcheck --leak-check=full ./list_pod_bin
==36224== Memcheck, a memory error detector
==36224== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==36224== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==36224== Command: ./list_pod_bin
==36224==
kubeyaml_load_kubeconfig(): Cannot open the file non-existent-file.[No such file or directory]
load_kube_config(): Cannot load the kubeconfig non-existent-file
Cannot load kubernetes configuration.
==36224==
==36224== HEAP SUMMARY:
==36224==     in use at exit: 68,224 bytes in 8 blocks
==36224==   total heap usage: 1,311 allocs, 1,303 frees, 174,553 bytes allocated
==36224==
==36224== 64 bytes in 1 blocks are definitely lost in loss record 1 of 8
==36224==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==36224==    by 0x50325C8: yaml_malloc (in /usr/local/lib/libyaml.so)
==36224==    by 0x5032CD0: yaml_parser_initialize (in /usr/local/lib/libyaml.so)
==36224==    by 0x4A80E64: kubeyaml_load_kubeconfig (in /root/c/kubernetes/build/libkubernetes.so)
==36224==    by 0x4A8398F: load_kube_config (in /root/c/kubernetes/build/libkubernetes.so)
==36224==    by 0x109520: main (main.c:41)
==36224==
==36224== 64 bytes in 1 blocks are definitely lost in loss record 2 of 8
==36224==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==36224==    by 0x50325C8: yaml_malloc (in /usr/local/lib/libyaml.so)
==36224==    by 0x5032DB9: yaml_parser_initialize (in /usr/local/lib/libyaml.so)
==36224==    by 0x4A80E64: kubeyaml_load_kubeconfig (in /root/c/kubernetes/build/libkubernetes.so)
==36224==    by 0x4A8398F: load_kube_config (in /root/c/kubernetes/build/libkubernetes.so)
==36224==    by 0x109520: main (main.c:41)
==36224==
==36224== 256 bytes in 1 blocks are definitely lost in loss record 3 of 8
==36224==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==36224==    by 0x50325C8: yaml_malloc (in /usr/local/lib/libyaml.so)
==36224==    by 0x5032EA2: yaml_parser_initialize (in /usr/local/lib/libyaml.so)
==36224==    by 0x4A80E64: kubeyaml_load_kubeconfig (in /root/c/kubernetes/build/libkubernetes.so)
==36224==    by 0x4A8398F: load_kube_config (in /root/c/kubernetes/build/libkubernetes.so)
==36224==    by 0x109520: main (main.c:41)
==36224==
==36224== 384 bytes in 1 blocks are definitely lost in loss record 4 of 8
==36224==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==36224==    by 0x50325C8: yaml_malloc (in /usr/local/lib/libyaml.so)
==36224==    by 0x5032E2C: yaml_parser_initialize (in /usr/local/lib/libyaml.so)
==36224==    by 0x4A80E64: kubeyaml_load_kubeconfig (in /root/c/kubernetes/build/libkubernetes.so)
==36224==    by 0x4A8398F: load_kube_config (in /root/c/kubernetes/build/libkubernetes.so)
==36224==    by 0x109520: main (main.c:41)
==36224==
==36224== 640 bytes in 1 blocks are definitely lost in loss record 5 of 8
==36224==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==36224==    by 0x50325C8: yaml_malloc (in /usr/local/lib/libyaml.so)
==36224==    by 0x5032D43: yaml_parser_initialize (in /usr/local/lib/libyaml.so)
==36224==    by 0x4A80E64: kubeyaml_load_kubeconfig (in /root/c/kubernetes/build/libkubernetes.so)
==36224==    by 0x4A8398F: load_kube_config (in /root/c/kubernetes/build/libkubernetes.so)
==36224==    by 0x109520: main (main.c:41)
==36224==
==36224== 1,280 bytes in 1 blocks are definitely lost in loss record 6 of 8
==36224==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==36224==    by 0x50325C8: yaml_malloc (in /usr/local/lib/libyaml.so)
==36224==    by 0x5032C44: yaml_parser_initialize (in /usr/local/lib/libyaml.so)
==36224==    by 0x4A80E64: kubeyaml_load_kubeconfig (in /root/c/kubernetes/build/libkubernetes.so)
==36224==    by 0x4A8398F: load_kube_config (in /root/c/kubernetes/build/libkubernetes.so)
==36224==    by 0x109520: main (main.c:41)
==36224==
==36224== 16,384 bytes in 1 blocks are definitely lost in loss record 7 of 8
==36224==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==36224==    by 0x50325C8: yaml_malloc (in /usr/local/lib/libyaml.so)
==36224==    by 0x5032B2C: yaml_parser_initialize (in /usr/local/lib/libyaml.so)
==36224==    by 0x4A80E64: kubeyaml_load_kubeconfig (in /root/c/kubernetes/build/libkubernetes.so)
==36224==    by 0x4A8398F: load_kube_config (in /root/c/kubernetes/build/libkubernetes.so)
==36224==    by 0x109520: main (main.c:41)
==36224==
==36224== 49,152 bytes in 1 blocks are possibly lost in loss record 8 of 8
==36224==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==36224==    by 0x50325C8: yaml_malloc (in /usr/local/lib/libyaml.so)
==36224==    by 0x5032BB8: yaml_parser_initialize (in /usr/local/lib/libyaml.so)
==36224==    by 0x4A80E64: kubeyaml_load_kubeconfig (in /root/c/kubernetes/build/libkubernetes.so)
==36224==    by 0x4A8398F: load_kube_config (in /root/c/kubernetes/build/libkubernetes.so)
==36224==    by 0x109520: main (main.c:41)
==36224==
==36224== LEAK SUMMARY:
==36224==    definitely lost: 19,072 bytes in 7 blocks
==36224==    indirectly lost: 0 bytes in 0 blocks
==36224==      possibly lost: 49,152 bytes in 1 blocks
==36224==    still reachable: 0 bytes in 0 blocks
==36224==         suppressed: 0 bytes in 0 blocks
==36224==
==36224== For lists of detected and suppressed errors, rerun with: -s
==36224== ERROR SUMMARY: 8 errors from 8 contexts (suppressed: 0 from 0)
make: *** [Makefile:14: memcheck] Error 255
The valgrind result after fixing

# make memcheck
valgrind --tool=memcheck --leak-check=full ./list_pod_with_invalid_kubeconfig
==39351== Memcheck, a memory error detector
==39351== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==39351== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==39351== Command: ./list_pod_bin
==39351==
kubeyaml_load_kubeconfig(): Cannot open the file non-existent-file.[No such file or directory]
load_kube_config(): Cannot load the kubeconfig non-existent-file
Cannot load kubernetes configuration.
==39351==
==39351== HEAP SUMMARY:
==39351==     in use at exit: 0 bytes in 0 blocks
==39351==   total heap usage: 1,303 allocs, 1,303 frees, 106,329 bytes allocated
==39351==
==39351== All heap blocks were freed -- no leaks are possible
==39351==
==39351== For lists of detected and suppressed errors, rerun with: -s
==39351== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

@brendandburns
Copy link
Contributor

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 17, 2023
@k8s-ci-robot
Copy link
Contributor

[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 [brendandburns,ityuhui]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit 18431c6 into kubernetes-client:master Jan 17, 2023
@ityuhui ityuhui deleted the yh-mlk-0116 branch January 18, 2023 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory Leak in kubeyaml_load_kubeconfig() in kubernetes/config/kube_config_yaml.c
3 participants