code-intelligence.com/cifuzz@v0.40.0/pkg/minijail/output_filter.go (about)

     1  package minijail
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"regexp"
     7  )
     8  
     9  var ignoredPattern = regexp.MustCompile(`^libminijail\[\d+]: child process \d+ exited with status \d+`)
    10  
    11  type OutputFilter struct {
    12  	nextWriter io.Writer
    13  	buf        *bytes.Buffer
    14  }
    15  
    16  func NewOutputFilter(nextWriter io.Writer) *OutputFilter {
    17  	return &OutputFilter{nextWriter: nextWriter, buf: bytes.NewBuffer([]byte{})}
    18  }
    19  
    20  func (w *OutputFilter) Write(p []byte) (int, error) {
    21  	// To be able to match lines, we only print up to the last newline
    22  	// and store everything not printed in the buffer
    23  	index := bytes.LastIndexByte(p, '\n')
    24  	if index == -1 {
    25  		w.buf.Write(p)
    26  		return len(p), nil
    27  	}
    28  
    29  	toPrint, toStore := p[:index+1], p[index+1:]
    30  
    31  	// Prepend the bytes stored in the buffer to the bytes we're about
    32  	// to print
    33  	toPrint = append(w.buf.Bytes(), toPrint...)
    34  
    35  	w.buf.Reset()
    36  	w.buf.Write(toStore)
    37  
    38  	if ignoredPattern.Match(toPrint) {
    39  		return len(p), nil
    40  	}
    41  
    42  	written, err := w.nextWriter.Write(toPrint)
    43  	numBytesNotWritten := len(toPrint) - written
    44  	return len(p) - numBytesNotWritten, err
    45  }
    46  
    47  func IsIgnoredLine(line string) bool {
    48  	return ignoredPattern.MatchString(line)
    49  }