From c4f57ff4927986ac2c45e1d4a84ad425a9c9aae9 Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Mon, 20 May 2013 17:32:48 +0200 Subject: [PATCH] pcuart: malloc error handling, cleanup --- sys/src/9/pc/uartisa.c | 10 ++++++++-- sys/src/9/pc/uartpci.c | 34 ++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/sys/src/9/pc/uartisa.c b/sys/src/9/pc/uartisa.c index f02080d70..f6b590724 100644 --- a/sys/src/9/pc/uartisa.c +++ b/sys/src/9/pc/uartisa.c @@ -18,16 +18,22 @@ uartisa(int ctlrno, ISAConf* isa) Uart *uart; char buf[64]; + uart = malloc(sizeof(Uart)); + if(uart == nil){ + print("uartisa: no memory for Uart\n"); + return nil; + } + io = isa->port; snprint(buf, sizeof(buf), "%s%d", isaphysuart.name, ctlrno); if(ioalloc(io, 8, 0, buf) < 0){ print("uartisa: I/O 0x%uX in use\n", io); + free(uart); return nil; } - uart = malloc(sizeof(Uart)); ctlr = i8250alloc(io, isa->irq, BUSUNKNOWN); - if(uart == nil || ctlr == nil){ + if(ctlr == nil){ iofree(io); free(uart); return nil; diff --git a/sys/src/9/pc/uartpci.c b/sys/src/9/pc/uartpci.c index ec8ce9e94..1f97bd930 100644 --- a/sys/src/9/pc/uartpci.c +++ b/sys/src/9/pc/uartpci.c @@ -21,17 +21,23 @@ uartpci(int ctlrno, Pcidev* p, int barno, int n, int freq, char* name, char buf[64]; Uart *head, *uart; + head = malloc(sizeof(Uart)*n); + if(head == nil){ + print("uartpci: no memory for Uarts\n"); + return nil; + } + io = p->mem[barno].bar & ~0x01; snprint(buf, sizeof(buf), "%s%d", pciphysuart.name, ctlrno); if(ioalloc(io, p->mem[barno].size, 0, buf) < 0){ print("uartpci: I/O 0x%uX in use\n", io); + free(head); return nil; } - head = uart = malloc(sizeof(Uart)*n); + uart = head; for(i = 0; i < n; i++){ - ctlr = i8250alloc(io, p->intl, p->tbdf); - io += iosize; + ctlr = i8250alloc(io + i*iosize, p->intl, p->tbdf); if(ctlr == nil) continue; @@ -44,16 +50,20 @@ uartpci(int ctlrno, Pcidev* p, int barno, int n, int freq, char* name, (uart-1)->next = uart; uart++; } - - if (head) { - if(perlehead != nil) - perletail->next = head; - else - perlehead = head; - for(perletail = head; perletail->next != nil; - perletail = perletail->next) - ; + if(head == uart){ + iofree(io); + free(head); + return nil; } + + if(perlehead != nil) + perletail->next = head; + else + perlehead = head; + for(perletail = head; perletail->next != nil; + perletail = perletail->next) + ; + return head; }