git: 9front

Download patch

ref: eda9f9125767a749c803c4a37e1abe9f914adabe
parent: cf08c854101ea8a9ee3e529071dc31b1e90a0377
author: cinap_lenrek <cinap_lenrek@gmx.de>
date: Mon May 20 13:32:48 EDT 2013

pcuart: malloc error handling, cleanup

--- a/sys/src/9/pc/uartisa.c
+++ b/sys/src/9/pc/uartisa.c
@@ -18,16 +18,22 @@
 	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;
--- a/sys/src/9/pc/uartpci.c
+++ b/sys/src/9/pc/uartpci.c
@@ -21,17 +21,23 @@
 	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 @@
 			(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;
 }
 
--