package
0.0.7
Repository: https://github.com/datadog/go-secure-sdk.git
Documentation: pkg.go.dev

# README

ioutil

Package ioutil provides I/O hardened operations.

Variables

ErrReaderTimedOut is raised when the reader doesn't received data for a predeterminined time.

var ErrReaderTimedOut = errors.New("reader timed out")

ErrTruncatedCopy is raised when the copy is larger than expected.

var ErrTruncatedCopy = errors.New("truncated copy due to too large input")

Functions

func LimitCopy

func LimitCopy(dst io.Writer, src io.Reader, maxSize uint64) (uint64, error)

LimitCopy uses a buffered CopyN and a hardlimit to stop read from the reader when the maxSize amount of data has been written to the given writer and raise an error.

root := os.DirFS("./testdata")

// Open 1Gb gzip bomb
bomb, err := root.Open("1g.gz")
if err != nil {
    panic(err)
}

// Pass through the GZIP decompression reader
gzr, err := gzip.NewReader(bomb)
if err != nil {
    panic(err)
}

// Copy decompressed data with hard limit to 1Mb.
//
// Why not using an io.LimitReader? Because the LimitReader truncate the
// data without raising an error.
_, err = LimitCopy(io.Discard, gzr, 1024)

Output:

truncated copy due to too large input

func LimitWriter

func LimitWriter(w io.Writer, limit int) io.Writer

LimitWriter create a new Writer that accepts at most 'limit' bytes.

out := bytes.Buffer{}
lw := LimitWriter(&out, 1024)

// Copy data from the reader
_, err := io.CopyN(lw, rand.Reader, 2048)
if err != nil {
    panic(err)
}

Output:

1024

func TimeoutReader

func TimeoutReader(reader io.Reader, timeout time.Duration) io.Reader

TimeoutReader create a timed-out limited reader instance.

// Can be any reader (os.Stdin, Sockets, etc.)
tr := TimeoutReader(&slowReader{
    // The reader will block for 1s.
    timeout: time.Second,
    err:     io.EOF,
}, time.Millisecond)

// Copy data from the reader
_, err := io.Copy(io.Discard, tr)

Output:

reader timed out

# Functions

LimitCopy uses a buffered CopyN and a hardlimit to stop read from the reader when the maxSize amount of data has been written to the given writer and raise an error.
LimitWriter create a new Writer that accepts at most 'limit' bytes.
TimeoutReader create a timed-out limited reader instance.

# Variables

ErrReaderTimedOut is raised when the reader doesn't received data for a predeterminined time.
ErrTruncatedCopy is raised when the copy is larger than expected.