safeconv

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package safeconv provides bounds-checked numeric conversions.

Go's plain conversion `int32(x)` silently wraps on overflow, which static analysers (gosec G115) flag as an integer-overflow hazard. The helpers here make the truncation explicit and saturate to the target type's range instead of wrapping — so a malformed/oversized request can never produce a negative or aliased value downstream.

Used by every kacho service that maps int64 proto fields (page sizes, counts) onto narrower internal types, so it lives in corelib.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClampInt32

func ClampInt32(v int64) int32

ClampInt32 saturates an int64 into the int32 range. Values below math.MinInt32 clamp to math.MinInt32; values above math.MaxInt32 clamp to math.MaxInt32. This is the safe replacement for a bare `int32(v)`.

func ClampNonNegInt32

func ClampNonNegInt32(v int64) int32

ClampNonNegInt32 saturates an int64 into the [0, math.MaxInt32] range. Intended for page sizes and counts that must never be negative.

func IntToInt32

func IntToInt32(v int) int32

IntToInt32 saturates a platform int into the int32 range.

func IntToUint32

func IntToUint32(v int) uint32

IntToUint32 saturates a platform int into the [0, math.MaxUint32] range, flooring negatives at 0.

Types

This section is empty.

Jump to

Keyboard shortcuts

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