Skip to contentSkip to conversation

selftests/bpf: Fix null pointer check in skb_pkt_end.c

PATCH In review Prabhav Kumar Vaish 2 participants 3 messages Apr 22, 2025 – Apr 23, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: Fix null pointer check in skb_pkt_end.c

Ensure that 'tcp' is checked for NULL before dereferencing. This resolves a potential null pointer dereference warning reported by static analysis.

---
 tools/testing/selftests/bpf/progs/skb_pkt_end.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/skb_pkt_end.c +2 -2
index 3bb4451524a1..db33ff2839f7 100644
@@ -45,10 +45,10 @@ int main_prog(struct __sk_buff *skb)
 		goto out;
 
 	tcp = (void*)(ip + 1);
-	if (tcp->dest != 0)
-		goto out;
 	if (!tcp)
 		goto out;
+	if (tcp->dest != 0)
+		goto out;
 
 	urg_ptr = tcp->urg_ptr;
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 13 lines · Prabhav Kumar Vaish

Ensure that 'tcp' is checked for NULL before dereferencing. This resolves a potential null pointer dereference warning reported by static analysis.

---
  tools/testing/selftests/bpf/progs/skb_pkt_end.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/skb_pkt_end.c +0 -0
index 3bb4451524a1..db33ff2839f7 100644

tools/testing/selftests/bpf/progs/skb_pkt_end.c int main_prog(struct __sk_buff *skb) · line 48

Quoted 6 lines
tools/testing/selftests/bpf/progs/skb_pkt_end.c +0 -2
@@ -45,10 +45,10 @@ int main_prog(struct __sk_buff *skb)
  		goto out;
 
  	tcp = (void*)(ip + 1);
-	if (tcp->dest != 0)
-		goto out;
  	if (!tcp)

This case will never be hit, so this change is not doing anything other than silencing the static checker. Take a look at commit 9cc873e85800 ("selftests/bpf: Add skb_pkt_end test"). The test was written to have a specific llvm generated code. You will need to check the generated code is still testing what it is supposed to test.

pw-bot: cr

Quoted 6 lines
tools/testing/selftests/bpf/progs/skb_pkt_end.c +2 -0
  		goto out;
+	if (tcp->dest != 0)
+		goto out;
 
  	urg_ptr = tcp->urg_ptr;
 
#2 lore raw

Re: [PATCH] selftests/bpf: Fix potential null pointer dereference in skb_pkt_end.c

From: Prabhav Kumar <[email protected]>

Hi Martin,
Thank you for the feedback.

You're right — I reviewed commit 9cc873e85800 and now understand that the test is designed to validate specific LLVM code generation, not just runtime behavior. I see how my change, although addressing the warning, could unintentionally alter the generated instructions and defeat the purpose of the test.

I'll drop this patch to preserve the original test intent. Thanks again for the context!

Best Regards,
Prabhav