traces.go (1232B)
1 // Copyright 2022 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package traces 6 7 import ( 8 "golang.org/x/vuln/internal/govulncheck" 9 ) 10 11 // Compact returns a summarization of finding.Trace. The first 12 // returned element is the vulnerable symbol and the last element 13 // is the exit point of the user module. There can also be two 14 // elements in between, if applicable, which are the two elements 15 // preceding the user module exit point. 16 func Compact(finding *govulncheck.Finding) []*govulncheck.Frame { 17 if len(finding.Trace) < 1 { 18 return nil 19 } 20 iTop := len(finding.Trace) - 1 21 topModule := finding.Trace[iTop].Module 22 // search for the exit point of the top module 23 for i, frame := range finding.Trace { 24 if frame.Module == topModule { 25 iTop = i 26 break 27 } 28 } 29 30 if iTop == 0 { 31 // all in one module, reset to the end 32 iTop = len(finding.Trace) - 1 33 } 34 35 compact := []*govulncheck.Frame{finding.Trace[0]} 36 if iTop > 1 { 37 if iTop > 2 { 38 compact = append(compact, finding.Trace[iTop-2]) 39 } 40 compact = append(compact, finding.Trace[iTop-1]) 41 } 42 if iTop > 0 { 43 compact = append(compact, finding.Trace[iTop]) 44 } 45 return compact 46 }