saferio

package standard library
master (86a1a99) Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2024 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package saferio provides I/O functions that avoid allocating large amounts of memory unnecessarily. This is intended for packages that read data from an io.Reader where the size is part of the input data but the input may be corrupt, or may be provided by an untrustworthy attacker.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadData

func ReadData(r io.Reader, n uint64) ([]byte, error)

ReadData reads n bytes from the input stream, but avoids allocating all n bytes if n is large. This avoids crashing the program by allocating all n bytes in cases where n is incorrect.

The error is io.EOF only if no bytes were read. If an io.EOF happens after reading some but not all the bytes, ReadData returns io.ErrUnexpectedEOF.

func ReadDataAt

func ReadDataAt(r io.ReaderAt, n uint64, off int64) ([]byte, error)

ReadDataAt reads n bytes from the input stream at off, but avoids allocating all n bytes if n is large. This avoids crashing the program by allocating all n bytes in cases where n is incorrect.

func SliceCap

func SliceCap[E any](c uint64) int

SliceCap is like SliceCapWithSize but using generics.

func SliceCapWithSize added in go1.22.0

func SliceCapWithSize(size, c uint64) int

SliceCapWithSize returns the capacity to use when allocating a slice. After the slice is allocated with the capacity, it should be built using append. This will avoid allocating too much memory if the capacity is large and incorrect.

A negative result means that the value is always too big.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL