perf script: Check session->header.env.arch before using it

When perf.data is not written cleanly, we would like to process existing
data as much as possible (please see f_header.data.size == 0 condition
in perf_session__read_header). However, perf.data with partial data may
crash perf. Specifically, we see crash in 'perf script' for NULL
session->header.env.arch.

Fix this by checking session->header.env.arch before using it to determine
native_arch. Also split the if condition so it is easier to read.

Committer notes:

If it is a pipe, we already assume is a native arch, so no need to check
session->header.env.arch.

Signed-off-by: Song Liu <songliubraving@fb.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: kernel-team@fb.com
Cc: stable@vger.kernel.org
Link: http://lore.kernel.org/lkml/20211004053238.514936-1-songliubraving@fb.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by Song Liu and committed by Arnaldo Carvalho de Melo 29c77550 09572948

Changed files
+8 -4
tools
+8 -4
tools/perf/builtin-script.c
··· 4039 4039 goto out_delete; 4040 4040 4041 4041 uname(&uts); 4042 - if (data.is_pipe || /* assume pipe_mode indicates native_arch */ 4043 - !strcmp(uts.machine, session->header.env.arch) || 4044 - (!strcmp(uts.machine, "x86_64") && 4045 - !strcmp(session->header.env.arch, "i386"))) 4042 + if (data.is_pipe) { /* Assume pipe_mode indicates native_arch */ 4046 4043 native_arch = true; 4044 + } else if (session->header.env.arch) { 4045 + if (!strcmp(uts.machine, session->header.env.arch)) 4046 + native_arch = true; 4047 + else if (!strcmp(uts.machine, "x86_64") && 4048 + !strcmp(session->header.env.arch, "i386")) 4049 + native_arch = true; 4050 + } 4047 4051 4048 4052 script.session = session; 4049 4053 script__setup_sample_type(&script);