From 11d54f3deb618e652eb9afab1115d0c50a52b096 Mon Sep 17 00:00:00 2001
From: "Guanyou.Chen" <chenguanyou@xiaomi.com>
Date: Fri, 1 Nov 2024 17:54:07 +0800
Subject: [PATCH] bugfix command "help -r" segv fault

When the ELF Note does not contain CPU registers,
attempting to retrieve online CPU registers will cause a crash.

After:
CPU 6:
help: registers not collected for cpu 6
...

Signed-off-by: Guanyou.Chen <chenguanyou@xiaomi.com>
---
 netdump.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/netdump.c b/netdump.c
index 8ea5159..435793b 100644
--- a/netdump.c
+++ b/netdump.c
@@ -2780,6 +2780,10 @@ display_regs_from_elf_notes(int cpu, FILE *ofp)
 				nd->nt_prstatus_percpu[cpu];
 		else
                 	note64 = (Elf64_Nhdr *)nd->nt_prstatus;
+		if (!note64) {
+			error(INFO, "registers not collected for cpu %d\n", cpu);
+			return;
+		}
 		len = sizeof(Elf64_Nhdr);
 		len = roundup(len + note64->n_namesz, 4);
 		len = roundup(len + note64->n_descsz, 4);
@@ -2820,6 +2824,10 @@ display_regs_from_elf_notes(int cpu, FILE *ofp)
 				nd->nt_prstatus_percpu[cpu];
 		else
                 	note32 = (Elf32_Nhdr *)nd->nt_prstatus;
+		if (!note32) {
+			error(INFO, "registers not collected for cpu %d\n", cpu);
+			return;
+		}
 		len = sizeof(Elf32_Nhdr);
 		len = roundup(len + note32->n_namesz, 4);
 		len = roundup(len + note32->n_descsz, 4);
@@ -2857,6 +2865,10 @@ display_regs_from_elf_notes(int cpu, FILE *ofp)
 		else
 			note64 = (Elf64_Nhdr *)nd->nt_prstatus;
 
+		if (!note64) {
+			error(INFO, "registers not collected for cpu %d\n", cpu);
+			return;
+		}
 		prs = (struct ppc64_elf_prstatus *)
 			((char *)note64 + sizeof(Elf64_Nhdr) + note64->n_namesz);
 		prs = (struct ppc64_elf_prstatus *)roundup((ulong)prs, 4);
@@ -2903,6 +2915,10 @@ display_regs_from_elf_notes(int cpu, FILE *ofp)
 				nd->nt_prstatus_percpu[cpu];
 		else
                 	note64 = (Elf64_Nhdr *)nd->nt_prstatus;
+		if (!note64) {
+			error(INFO, "registers not collected for cpu %d\n", cpu);
+			return;
+		}
 		len = sizeof(Elf64_Nhdr);
 		len = roundup(len + note64->n_namesz, 4);
 		len = roundup(len + note64->n_descsz, 4);
-- 
2.34.1

