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 <nichelnich@gmail.com>
This commit is contained in:
		
				
					committed by
					
						
						Christian Marangi
					
				
			
			
				
	
			
			
			
						parent
						
							79844637df
						
					
				
				
					commit
					2b0b28f479
				
			@@ -101,6 +101,24 @@ speed_str(int speed)
 | 
				
			|||||||
	return "unknown";
 | 
						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
 | 
					static void
 | 
				
			||||||
print_attr_val(const struct switch_attr *attr, const struct switch_val *val)
 | 
					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);
 | 
								printf("\t%s: ", attr->name);
 | 
				
			||||||
			if (swlib_get_attr(dev, attr, val) < 0)
 | 
								if (swlib_get_attr(dev, attr, val) < 0)
 | 
				
			||||||
				printf("???");
 | 
									printf("???");
 | 
				
			||||||
			else
 | 
								else {
 | 
				
			||||||
				print_attr_val(attr, val);
 | 
									print_attr_val(attr, val);
 | 
				
			||||||
 | 
									free_attr_val(attr, val);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			putchar('\n');
 | 
								putchar('\n');
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		attr = attr->next;
 | 
							attr = attr->next;
 | 
				
			||||||
@@ -354,6 +374,7 @@ int main(int argc, char **argv)
 | 
				
			|||||||
			goto out;
 | 
								goto out;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		print_attr_val(a, &val);
 | 
							print_attr_val(a, &val);
 | 
				
			||||||
 | 
							free_attr_val(a, &val);
 | 
				
			||||||
		putchar('\n');
 | 
							putchar('\n');
 | 
				
			||||||
		break;
 | 
							break;
 | 
				
			||||||
	case CMD_LOAD:
 | 
						case CMD_LOAD:
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user