Skip to content

cleanup(libscap): remove always-false NULL check in get_source_plugin_stats_v2 #3047

Description

@Ququrruzza

What happened

get_source_plugin_stats_v2 in the source_plugin engine has a NULL check on
stats that can never be true.

userspace/libscap/engine/source_plugin/source_plugin.c (master), around
line 290:

metrics_v2* stats = handle->m_stats;
if(!stats) {                 // always false
    *nstats = 0;
    *rc = SCAP_FAILURE;
    return NULL;
}

m_stats is an array member of the engine struct, not a pointer -
userspace/libscap/engine/source_plugin/source_plugin.h:

metrics_v2 m_stats[MAX_SOURCE_PLUGIN_COUNTERS_STATS];

So stats = handle->m_stats always decays to the address of the first element,
which is non-null whenever handle is valid (and handle is already
dereferenced two lines above without a check). The if(!stats) branch is dead
code.

Flagged by static analysis as a redundant comparison that is always false at
source_plugin.c:291, and confirmed by reading the code.

Why it matters

No functional impact - the branch never runs, so behaviour is unchanged. It's
dead code that trips static analyzers and is slightly misleading (it reads like
a real allocation-failure guard, but m_stats is never allocated or nullable).

Suggested change

Drop the check:

metrics_v2* stats = handle->m_stats;

/* SOURCE PLUGIN STATS COUNTERS */
for(uint32_t stat = 0; stat < MAX_SOURCE_PLUGIN_COUNTERS_STATS; stat++) {
    ...
}

Note

The same metrics_v2* stats = <array member>; if(!stats) pattern likely exists
in other engines' get_stats_v2 implementations, so this could be swept in one
cleanup if wanted.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions