parser: plug a memory leak - #309
Conversation
The route->type field wasn't being free'ed in case of error. Caught by a unit test compiled with ASAN.
slyon
left a comment
There was a problem hiding this comment.
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?
|
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.
There was a problem hiding this comment.
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).
| if (npp->current.route) { | ||
| g_free(npp->current.route); | ||
| free_route(npp->current.route); | ||
| npp->current.route = NULL; |
There was a problem hiding this comment.
Use route_clear() instead of manual free & nullify.
The route->type field wasn't being free'ed in case of error. Caught by a unit test compiled with ASAN.
Description
Checklist
make checksuccessfully.make check-coverage).