kern/154287: Avoid malloc(0) implementation dependency in device_get_children()

Sebastian Huber sebastian.huber at embedded-brains.de
Tue Jan 25 14:50:08 UTC 2011


>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:


More information about the freebsd-bugs mailing list