#1554 closed defect (fixed)

libi2c.c: Memory Leak

Reported by: Joel Sherrill Owned by: thomas.doerfler
Priority: normal Milestone: 4.11
Component: score Version: 4.10
Severity: normal Keywords:
Cc: chrisj@… Blocked By:
Blocking:

Description

Another fairly obvious issue found by Coverity. This is Coverity Id 17. At line 388, memory is malloc'ed. There appear to be two error paths in which this memory is not freed.

Also it is assumed that the malloc does succeed.

383 int
384 rtems_libi2c_register_bus (const char *name, rtems_libi2c_bus_t * bus)
385 {
386 int i;
387 rtems_status_code err;

Event alloc_fn: Called allocation function "malloc"
Event var_assign: Assigned variable "nmcpy" to storage returned from "malloc"
Also see events: [var_assign][leaked_storage][leaked_storage][pass_arg][pass_arg]

388 char *nmcpy = malloc (name ? strlen (name) + 1 : 20);
389 char tmp, *chpt;
390 struct stat sbuf;
391

Event pass_arg: Variable "nmcpy" not freed or pointed-to in function "strcpy"
Also see events: [alloc_fn][var_assign][leaked_storage][leaked_storage][pass_arg]
At conditional (1): "name != 0" taking true path

392 strcpy (nmcpy, name ? name : "/dev/i2c");
393
394
395 /* check */

At conditional (2): "*nmcpy != 47" taking true path

396 if ('/' != *nmcpy) {
397 safe_printf ( DRVNM "Bad name: must be an absolute path starting with '/'\n");

Event leaked_storage: Returned without freeing storage "nmcpy"
Also see events: [alloc_fn][var_assign][leaked_storage][pass_arg][pass_arg]

398 return -RTEMS_INVALID_NAME;
399 }
400 /* file must not exist */

Event pass_arg: Variable "nmcpy" not freed or pointed-to in function "stat" [model]
Also see events: [alloc_fn][var_assign][leaked_storage][leaked_storage][pass_arg]

401 if (!stat (nmcpy, &sbuf)) {
402 safe_printf ( DRVNM "Bad name: file exists already\n");

Event leaked_storage: Returned without freeing storage "nmcpy"
Also see events: [alloc_fn][var_assign][leaked_storage][pass_arg][pass_arg]

403 return -RTEMS_INVALID_NAME;
404 }
405

Attachments (1)

pr1554.diff (1.3 KB) - added by Joel Sherrill on Jun 14, 2010 at 2:55:19 PM.
Should address issue

Download all attachments as: .zip

Change History (2)

Changed on Jun 14, 2010 at 2:55:19 PM by Joel Sherrill

Attachment: pr1554.diff added

Should address issue

comment:1 Changed on Jun 21, 2010 at 3:27:57 PM by Joel Sherrill

Resolution: fixed
Status: newclosed

Patch committed to head, 4.10 and 4.9.

Note: See TracTickets for help on using tickets.