File: extract.go

package info (click to toggle)
golang-golang-x-vuln 1.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,400 kB
  • sloc: sh: 161; asm: 40; makefile: 7
file content (63 lines) | stat: -rw-r--r-- 1,570 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build go1.18
// +build go1.18

package scan

import (
	"encoding/json"
	"fmt"
	"io"
	"sort"

	"golang.org/x/vuln/internal/derrors"
	"golang.org/x/vuln/internal/vulncheck"
)

const (
	// extractModeID is the unique name of the extract mode protocol
	extractModeID      = "govulncheck-extract"
	extractModeVersion = "0.1.0"
)

// header information for the blob output.
type header struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

// runExtract dumps the extracted abstraction of binary at cfg.patterns to out.
// It prints out exactly two blob messages, one with the header and one with
// the vulncheck.Bin as the body.
func runExtract(cfg *config, out io.Writer) (err error) {
	defer derrors.Wrap(&err, "govulncheck")

	bin, err := createBin(cfg.patterns[0])
	if err != nil {
		return err
	}
	sortBin(bin) // sort for easier testing and validation
	header := header{
		Name:    extractModeID,
		Version: extractModeVersion,
	}

	enc := json.NewEncoder(out)

	if err := enc.Encode(header); err != nil {
		return fmt.Errorf("marshaling blob header: %v", err)
	}
	if err := enc.Encode(bin); err != nil {
		return fmt.Errorf("marshaling blob body: %v", err)
	}
	return nil
}

func sortBin(bin *vulncheck.Bin) {
	sort.SliceStable(bin.PkgSymbols, func(i, j int) bool {
		return bin.PkgSymbols[i].Pkg+"."+bin.PkgSymbols[i].Name < bin.PkgSymbols[j].Pkg+"."+bin.PkgSymbols[j].Name
	})
}