Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 25 Jan 2011 14:47:54 GMT
From:      Sebastian Huber <sebastian.huber@embedded-brains.de>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   kern/154287: Avoid malloc(0) implementation dependency in device_get_children()
Message-ID:  <201101251447.p0PElsSf073901@red.freebsd.org>
Resent-Message-ID: <201101251450.p0PEo81W095876@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         154287
>Category:       kern
>Synopsis:       Avoid malloc(0) implementation dependency in device_get_children()
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Tue Jan 25 14:50:07 UTC 2011
>Closed-Date:
>Last-Modified:
>Originator:     Sebastian Huber
>Release:        9-current
>Organization:
embedded brains GmbH
>Environment:
>Description:
The device_get_children() implementation of the device(9) API depends on a particular malloc(0) behavior.  This makes it less portable to other operating systems.  The following patch avoids a malloc(0).
>How-To-Repeat:

>Fix:
Index: kern/subr_bus.c
===================================================================
--- kern/subr_bus.c     (revision 216297)
+++ kern/subr_bus.c     (working copy)
@@ -2090,14 +2090,19 @@
                count++;
        }
 
-       list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
-       if (!list)
-               return (ENOMEM);
+       if (count) {
+               list = malloc(count * sizeof(device_t), M_TEMP,
+                   M_NOWAIT|M_ZERO);
+               if (!list)
+                       return (ENOMEM);
 
-       count = 0;
-       TAILQ_FOREACH(child, &dev->children, link) {
-               list[count] = child;
-               count++;
+               count = 0;
+               TAILQ_FOREACH(child, &dev->children, link) {
+                       list[count] = child;
+                       count++;
+               }
+       } else {
+               list = NULL;
        }
 
        *devlistp = list;

>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201101251447.p0PElsSf073901>