From dan@kulesh.obluda.cz  Thu Jul 14 12:34:45 2005
Return-Path: <dan@kulesh.obluda.cz>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 36C1016A41C
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 14 Jul 2005 12:34:45 +0000 (GMT)
	(envelope-from dan@kulesh.obluda.cz)
Received: from kulesh.obluda.cz (kulesh.obluda.cz [193.179.22.243])
	by mx1.FreeBSD.org (Postfix) with ESMTP id C03DB43D45
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 14 Jul 2005 12:34:43 +0000 (GMT)
	(envelope-from dan@kulesh.obluda.cz)
Received: from kulesh.obluda.cz (localhost.eunet.cz [127.0.0.1])
	by kulesh.obluda.cz (8.13.3/8.13.3) with ESMTP id j6ECYf4I031085
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 14 Jul 2005 14:34:41 +0200 (CEST)
	(envelope-from dan@kulesh.obluda.cz)
Received: (from root@localhost)
	by kulesh.obluda.cz (8.13.3/8.13.1/Submit) id j6ECYfNm031084;
	Thu, 14 Jul 2005 14:34:41 +0200 (CEST)
	(envelope-from dan)
Message-Id: <200507141234.j6ECYfNm031084@kulesh.obluda.cz>
Date: Thu, 14 Jul 2005 14:34:41 +0200 (CEST)
From: Dan Lukes <dan@obluda.cz>
Reply-To: Dan Lukes <dan@obluda.cz>
To: FreeBSD-gnats-submit@freebsd.org
Subject: [ PATCH ] improper handling of malloc failures within libusbhid
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         83451
>Category:       kern
>Synopsis:       [libusbhid] [patch] improper handling of malloc failures within libusbhid
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Jul 14 12:40:16 GMT 2005
>Closed-Date:    
>Last-Modified:  Mon Jan 14 04:49:08 UTC 2008
>Originator:     Dan Lukes
>Release:        FreeBSD 5.4-STABLE i386
>Organization:
Obludarium
>Environment:
System: FreeBSD 5.4-STABLE #8: Sat Jul 9 16:31:08 CEST 2005 i386
lib/libusbhid/parse.c,v 1.10 2004/02/11 21:09:13 emax
lib/libusbhid/Makefile,v 1.8 2003/04/09 01:52:48 mdodd

>Description:
	Improper handling of malloc failures within libusbhid

	WARNS can be raised to 6
>How-To-Repeat:
>Fix:

--- patch begins here ---
--- lib/libusbhid/parse.c.ORIG	Wed Feb 25 22:05:47 2004
+++ lib/libusbhid/parse.c	Thu Jul 14 13:33:25 2005
@@ -93,8 +93,8 @@
 {
 	struct hid_data *s;
 
-	s = malloc(sizeof *s);
-	memset(s, 0, sizeof *s);
+	if ((s = calloc(1, sizeof *s)) == NULL)
+		return(NULL);
 	s->start = s->p = d->data;
 	s->end = d->data + d->size;
 	s->kindset = kindset;
@@ -346,6 +346,8 @@
 				break;
 			case 10: /* Push */
 				hi = malloc(sizeof *hi);
+				if (hi == NULL)
+					return(-6);
 				*hi = s->cur;
 				c->next = hi;
 				break;
@@ -413,7 +415,10 @@
 
 	memset(&h, 0, sizeof h);
 	size = 0;
-	for (d = hid_start_parse(r, 1<<k, id); hid_get_item(d, &h); ) {
+	d = hid_start_parse(r, 1<<k, id);
+	if (d == NULL)
+		return(-1);
+	for ( ; hid_get_item(d, &h); ) {
 		if (h.report_ID == id && h.kind == k) {
 			size = d->kindpos[k];
 		}
@@ -428,13 +433,17 @@
 {
 	hid_data_t d;
 
-	for (d = hid_start_parse(desc, 1<<k, id); hid_get_item(d, h); ) {
+	d = hid_start_parse(desc, 1<<k, id);
+	if (d == NULL)
+		goto err;
+	for ( ; hid_get_item(d, h); ) {
 		if (h->kind == k && !(h->flags & HIO_CONST) && h->usage == u) {
 			hid_end_parse(d);
 			return (1);
 		}
 	}
 	hid_end_parse(d);
+err:
 	h->report_size = 0;
 	return (0);
 }
--- lib/libusbhid/Makefile.ORIG	Sun Apr 13 20:10:31 2003
+++ lib/libusbhid/Makefile	Thu Jul 14 13:26:23 2005
@@ -21,4 +21,6 @@
 
 INCS=	usbhid.h
 
+WARNS+=	6
+
 .include <bsd.lib.mk>
--- patch ends here ---
>Release-Note:
>Audit-Trail:

From: Dan Lukes <dan@obluda.cz>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/83451: [ PATCH ] improper handling of malloc failures within
 libusbhid
Date: Thu, 14 Jul 2005 16:43:51 +0200

 	Please note the 'WARNS+=6' change within Makefile should be 'WARNS?=6' 
 instead
 
>Unformatted:
