Skip to contentSkip to conversation

p used before making sure it is not NULL in kernel/auditsc.c

PATCH Zhenwen Xu 2 participants 2 messages May 22, 2009 Audit

lore
Share & tools
lore

Zhenwen Xu

author
#0 lore raw

p used before making sure it is not NULL in kernel/auditsc.c

here we should make sure the p is not NULL then use it.

---
 kernel/auditsc.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
kernel/auditsc.c +3 -2
index 7d6ac7c..876fdd1 100644
@@ -341,13 +341,14 @@ static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
 {
 	struct audit_tree_refs *p = ctx->trees;
 	int left = ctx->tree_count;
+
+	if (!p)
+		return 0;
 	if (likely(left)) {
 		p->c[--left] = chunk;
 		ctx->tree_count = left;
 		return 1;
 	}
-	if (!p)
-		return 0;
 	p = p->next;
 	if (p) {
 		p->c[30] = chunk;
Signature · 5 lines
1.5.6.5

-- 
--------------------------------
http://zhwen.org - Open and Free
#1 lore raw
Quoted 3 lines · Zhenwen Xu

p used before making sure it is not NULL in kernel/auditsc.c

here we should make sure the p is not NULL then use it.

NAK. ->tree_count can become non-zero only after we had set ->trees non-NULL. It's "how much free slots is left in current->trees". If it's zero, we might have an empty chain (->trees is NULL, we bugger off and caller tries to allocate) *or* we might be at the end of the chain (->trees->next is NULL, same as previous) *or* we have more allocated blocks in the chain, so we move to the next block and store in there.

As a general note,

	if (foo) {
		...
		dereference p;
		...
	} else if (p) {
		...
		dereference p;
		...
	}

is a perfectly sane code, if foo is true only when p != NULL.