device: Tidy up add_more_links()

- Add documentation comment
- Use 'unsigned int' to make checkpatch happy
- Return early if no more links need to be added
- Add error handling if malloc fails
- Clean up whitespace

Change-Id: I70976ee2539b058721d0ae3c15edf279253cd9b7
Signed-off-by: Jacob Garber <jgarber1@ualberta.ca>
Found-by: Coverity CID 1229634
Reviewed-on: https://review.coreboot.org/c/coreboot/+/33238
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Reviewed-by: Paul Menzel <paulepanter@users.sourceforge.net>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
Jacob Garber 2019-06-05 17:03:30 -06:00 committed by Martin Roth
parent f77f7cdf89
commit 464f4d6ee2
2 changed files with 17 additions and 6 deletions

View File

@ -654,7 +654,13 @@ bool dev_is_active_bridge(struct device *dev)
return 0; return 0;
} }
void add_more_links(struct device *dev, unsigned total_links) /**
* Ensure the device has a minimum number of bus links.
*
* @param dev The device to add links to.
* @param total_links The minimum number of links to have.
*/
void add_more_links(struct device *dev, unsigned int total_links)
{ {
struct bus *link, *last = NULL; struct bus *link, *last = NULL;
int link_num = -1; int link_num = -1;
@ -668,15 +674,20 @@ void add_more_links(struct device *dev, unsigned total_links)
if (last) { if (last) {
int links = total_links - (link_num + 1); int links = total_links - (link_num + 1);
if (links > 0) { if (links > 0) {
link = malloc(links*sizeof(*link)); link = malloc(links * sizeof(*link));
if (!link) if (!link)
die("Couldn't allocate more links!\n"); die("Couldn't allocate more links!\n");
memset(link, 0, links*sizeof(*link)); memset(link, 0, links * sizeof(*link));
last->next = link; last->next = link;
} else {
/* No more links to add */
return;
} }
} else { } else {
link = malloc(total_links*sizeof(*link)); link = malloc(total_links * sizeof(*link));
memset(link, 0, total_links*sizeof(*link)); if (!link)
die("Couldn't allocate more links!\n");
memset(link, 0, total_links * sizeof(*link));
dev->link_list = link; dev->link_list = link;
} }

View File

@ -191,7 +191,7 @@ const char *bus_path(struct bus *bus);
void dev_set_enabled(struct device *dev, int enable); void dev_set_enabled(struct device *dev, int enable);
void disable_children(struct bus *bus); void disable_children(struct bus *bus);
bool dev_is_active_bridge(struct device *dev); bool dev_is_active_bridge(struct device *dev);
void add_more_links(struct device *dev, unsigned total_links); void add_more_links(struct device *dev, unsigned int total_links);
/* Option ROM helper functions */ /* Option ROM helper functions */
void run_bios(struct device *dev, unsigned long addr); void run_bios(struct device *dev, unsigned long addr);