Re: sizeof(ptr) or sizeof(*ptr)?

From:
Date: Sun Feb 27 2005 - 18:17:53 EST


Quoting Matthew Dharm <mdharm-kernel@xxxxxxxxxxxxxxxxxx>:
> [...]
> us->host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
>
> This is actually correct as-is. We're allocating a host, and asking for
> the sizeof(pointer) in the 'extra storage' section. We then store the
> pointer (not what it points to) in the extra storage section a few lines
> down.

Thanks for clarifying that. I guess the weekend effect got me, because at a
certain point I was starting to read the scsi_host_alloc as if it were a
kmalloc or something... :P

Anyway, after improving the tool and checking for false positives, there is only
one more suspicious piece of code in drivers/acpi/video.c:561

status = acpi_video_device_lcd_query_levels(device, &obj);

if (obj && obj->type == ACPI_TYPE_PACKAGE && obj->package.count >= 2) {
int count = 0;
union acpi_object *o;

br = kmalloc(sizeof &br, GFP_KERNEL);
if (!br) {
printk(KERN_ERR "can't allocate memory\n");
} else {
memset(br, 0, sizeof &br);
br->levels = kmalloc(obj->package.count * sizeof &br->levels, GFP_KERNEL);
if (!br->levels)
goto out;

"br" if of type "struct acpi_video_device_brightness *".

"sizeof &br" doesn't make much sense there (besides the unconventional use of
sizeof without parenthesis) because the rest of the code seem to suggest that a
whole structure should have been allocated. This is the last case I've seen,
and I've added the maintainer to the cc list, so that he can check the code for
correctness.

Well, sorry about bothering you with a false positive (that I should've
recognised on my own, anyway), and thanks for the help.

--
Paulo Marques - www.grupopie.com

All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke (1729 - 1797)

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/