From: nichel Chen Date: Thu, 8 Jun 2023 00:04:56 +0000 (+0800) Subject: swconfig: fix memory leak when cli call swlib_get_attr() X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=2b0b28f479a3aae58f0eae7032481b298ac520f8;p=openwrt%2Fstaging%2Fblocktrron.git swconfig: fix memory leak when cli call swlib_get_attr() The cli is a one-time run, and memory leaks would have been irrelevant. But people call libsw with cli programs as samples. Doing a good job of memory management calls means that people who call libsw are not so easy to make mistakes. Signed-off-by: nichel Chen --- diff --git a/package/network/config/swconfig/src/cli.c b/package/network/config/swconfig/src/cli.c index eab6c64742..2601f346db 100644 --- a/package/network/config/swconfig/src/cli.c +++ b/package/network/config/swconfig/src/cli.c @@ -101,6 +101,24 @@ speed_str(int speed) return "unknown"; } +static void +free_attr_val(const struct switch_attr *attr, const struct switch_val *val) +{ + switch (attr->type) { + case SWITCH_TYPE_STRING: + free(val->value.s); + break; + case SWITCH_TYPE_PORTS: + free(val->value.ports); + break; + case SWITCH_TYPE_LINK: + free(val->value.link); + break; + default: + break; + } +} + static void print_attr_val(const struct switch_attr *attr, const struct switch_val *val) { @@ -150,8 +168,10 @@ show_attrs(struct switch_dev *dev, struct switch_attr *attr, struct switch_val * printf("\t%s: ", attr->name); if (swlib_get_attr(dev, attr, val) < 0) printf("???"); - else + else { print_attr_val(attr, val); + free_attr_val(attr, val); + } putchar('\n'); } attr = attr->next; @@ -354,6 +374,7 @@ int main(int argc, char **argv) goto out; } print_attr_val(a, &val); + free_attr_val(a, &val); putchar('\n'); break; case CMD_LOAD: