Re: [PATCH] kcpuid: Fix potential dereferencing of null pointers

From: Thomas Gleixner
Date: Sun Sep 29 2024 - 06:52:45 EST


On Thu, Sep 26 2024 at 22:35, Remington Brasga wrote:
> if (!func->leafs) {
> func->leafs = malloc(sizeof(struct subleaf));
> - if (!func->leafs)
> + if (!func->leafs) {
> perror("malloc func leaf");
> + return false; // On malloc failure

Please get rid of these horrible and pointless tail comments.

Returning false here does not make sense. This simply should terminate
the program.

> + }
>
> func->nr = 1;
> } else {
> s = func->nr;
> func->leafs = realloc(func->leafs, (s + 1) * sizeof(*leaf));
> - if (!func->leafs)
> + if (!func->leafs) {
> perror("realloc f->leafs");
> + return false; // On realloc failure
> + }
>
> func->nr++;
> }
>
> + // Check for valid index
> + if (s >= func->nr) {

What's the point of this? s is guaranteed to be < func->nr, no?

Thanks,

tglx