I'm working with a kernel module, which is causing a null pointer dereference when inserted. I've tracked the error down to this function in swnode.c (not part of the kernel module, but is called by the module):
static struct fwnode_handle *
software_node_get_next_child(const struct fwnode_handle *fwnode,
struct fwnode_handle *child)
{
struct swnode *p = to_swnode(fwnode);
struct swnode *c = to_swnode(child);
if (!p || list_empty(&p->children) ||
(c && list_is_last(&c->entry, &p->children)))
return NULL;
if (c) {
c = list_next_entry(c, entry);
if (c->node)
pr_info("child node named %s\n", c->node->name);
} else {
c = list_first_entry(&p->children, struct swnode, entry);
}
return fwnode_handle_get(&c->fwnode);
}
I added the pr_info("child node named %s\n", c->node->name);
call for debugging, and that line causes the null pointer dereference. Prior to that the error was with return fwnode_handle_get(&c->fwnode)
which caused an oops complaining I was executing things in NX memory; it's apparent that c->fwnode is NULL, so I can try to work out why that is, I'm just wondering why my debug print caused an error too.
This situation confuses me; I'm explicitly checking that c
and c->node
are not null, in a way that I thought should protect against this kind of error (based on answers like this). The dereference operations are against c (because, by my understanding, c->member
is equivalent to (*c).member
) and c->node
. So; why, given the pr_info
call should only be evaluated if c
and c->node
are not null does it cause a null pointer dereference?
EDIT:
Close voters need to read both question and threads more carefully. This issue is not caused by a typo. The missing curly braces in the original example (which has since been edited to include them) are not the cause of the issue.