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

parser: plug a memory leak #309

Merged
merged 4 commits into from
Jan 10, 2023
Merged

parser: plug a memory leak #309

merged 4 commits into from
Jan 10, 2023

Conversation

daniloegea
Copy link
Collaborator

@daniloegea daniloegea commented Jan 5, 2023

The route->type field wasn't being free'ed in case of error. Caught by a unit test compiled with ASAN.

Description

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.

The route->type field wasn't being free'ed in case of error. Caught by a
unit test compiled with ASAN.
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.

The Route is defined as:

typedef struct {
    guint family;
    char* type;
    char* scope;
    guint table;

    char* from;
    char* to;
    char* via;

    gboolean onlink;

    /* valid metrics are valid positive integers.
     * invalid metrics are represented by METRIC_UNSPEC */
    guint metric;

    guint mtubytes;
    guint congestion_window;
    guint advertised_receive_window;
} NetplanIPRoute;

We're freeing route->type here, but what about scope/from/to/via on error? Should we be using free_route() util (from types.c) instead?

@daniloegea
Copy link
Collaborator Author

Yes, definitely. I didn't pay attention to the fact that the other fields could be allocated later.

A different error might happen later and we will end up not freeing
other fields.
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.

Thanks, having a closer look myself. I think we can further simplify this, by making use of the route_clear() function (instead of manual free & nullify), as defined using the CLEAR_FROM_FREE(free_route, route_clear, NetplanIPRoute); macro. Also, the route_clear function is already declared in types.h, so no need to add free_route into types-internal.h (and no need to make it non-static).

src/parse.c Outdated
@@ -1993,7 +1993,7 @@ handle_routes(NetplanParser* npp, yaml_node_t* node, const void* _, GError** err

err:
if (npp->current.route) {
g_free(npp->current.route);
free_route(npp->current.route);
npp->current.route = NULL;
Copy link
Collaborator

@slyon slyon Jan 10, 2023

Choose a reason for hiding this comment

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

Use route_clear() instead of manual free & nullify.

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.

lgtm, thanks!

@slyon slyon merged commit e5ff9f6 into canonical:main Jan 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants