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 some memory leaks #297

Merged
merged 3 commits into from Nov 21, 2022
Merged

Fix some memory leaks #297

merged 3 commits into from Nov 21, 2022

Conversation

daniloegea
Copy link
Collaborator

@daniloegea daniloegea commented Nov 17, 2022

* error.c: make sure the pointer 'line' is free'd after the last
  loop iteration. Also, free error_context before returning.

* generate.c: free the error object if an error is returned at some point.

* generate.c: free the options context allocated at the start of the main
  function.

* openvswitch.c: make sure the GString "cmds" is free'd before the
  function returns.

* parse.c: free the temporary list "values" before the function
  returns.

* parse.c: parsed_defs is set to NULL preventing
  netplan_parser_reset() to free the memory allocated for the
  hash table. Not setting it to NULL is safe because all the data is moved to
  another hash table, leaving parsed_defs with no elements. Not
  nullifying parsed_defs here allows netplan_parser_reset() to free
  it.

* types.c: in this particular case we g_strdup both key and value
  strings when inserting them in the hash table in
  parse.c:handle_generic_map(). Calling the destructor for both of
  them will make sure all the memory is free'd. When using
  free_hashtable_with_destructor in the future we'll need to make
  sure both key and value are not poiting to the same place to avoid
  a double free.

* util.c: release the glob_t allocated in find_yaml_glob().

* parse-nm.c: free "ap" when ssid can't be found (caught by clang
  build-scan).

* parse.c: filepath might be overwritten in some situations. Make
  sure the old one is free'd.

* parse.c: netdef->vxlan might be overwritten in some situations.
  Make sure the old one is free'd.

* types.c: free embedded_switch_mode when the netdef is reset.

Description

The before and after can be seen by running the generate program with valgrind

assuming you have an etc/netplan in /tmp/fakeroot:
valgrind --leak-check=full ./generate -r /tmp/fakeroot/

It can also be checked by compiling netplan with ASAN:

CFLAGS=-fsanitize=address meson setup build --prefix=/usr
meson compile -C build --verbose
and then
./generate -r /tmp/fakeroot/

Checklist

  • Runs make check successfully.
  • Retains 100% code coverage (make check-coverage).
  • New/changed keys in YAML format are documented.
  • (Optional) Adds example YAML for new feature.
  • (Optional) Closes an open bug in Launchpad.

    * generate.c: free the options context allocated at the start of the main
      function.

    * openvswitch.c: make sure the GString "cmds" is free'd before the
      function returns.

    * parse.c: free the temporary list "values" before the function
      returns.

    * parse.c: parsed_defs is set to NULL preventing
      netplan_parser_reset() to free the memory allocated for the
      hash table. Not setting it to NULL is safe because all the data is moved to
      another hash table, leaving parsed_defs with no elements. Not
      nullifying parsed_defs here allows netplan_parser_reset() to free
      it.

    * types.c: in this particular case we g_strdup both key and value
      strings when inserting them in the hash table in
      parse.c:handle_generic_map(). Calling the destructor for both of
      them will make sure all the memory is free'd. When using
      free_hashtable_with_destructor in the future we'll need to make
      sure both key and value are not poiting to the same place to avoid
      a double free.

    * util.c: release the glob_t allocated in find_yaml_glob().
    * error.c: make sure the pointer 'line' is free'd after the last
      loop iteration. Also, free error_context before returning.

    * generate.c: free the error object if an error is returned at some point.
    * parse-nm.c: free "ap" when ssid can't be found (caught by clang
      build-scan).

    * parse.c: filepath might be overwritten in some situations. Make
      sure the old one is free'd.

    * parse.c: netdef->vxlan might be overwritten in some situations.
      Make sure the old one is free'd.

    * types.c: free embedded_switch_mode when the netdef is reset.
Copy link
Collaborator

@slyon slyon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much, LGTM!
I really like the systematic approach of searching for memory leaks using valgrind!

@@ -371,7 +371,7 @@ netplan_netdef_write_ovs(const NetplanState* np_state, const NetplanNetDefinitio
/* Set controller target addresses */
if (def->ovs_settings.controller.addresses && def->ovs_settings.controller.addresses->len > 0) {
if (!write_ovs_bridge_controller_targets(settings, &(def->ovs_settings.controller), def->id, cmds, error))
return FALSE;
return FALSE;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, but makes sense to fix this up, while touching the file!

@slyon slyon merged commit 26ea3a6 into canonical:main Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants