Skip to content

Medium: Keyframe interval of 0 causes panic (division by zero) #23

Description

@metis-sprock

Description

NewWriterWithKeyframeInterval accepts an arbitrary uint32 for the keyframe interval with no validation. When the interval is 0, a modulo operation at line 83 causes a division-by-zero panic at runtime.

Evidence

File: pkg/codec/tape.go:51-66,83

func NewWriterWithKeyframeInterval(w io.Writer, interval uint32) (*Writer, error) {
    // no validation on interval — 0 is accepted
    return &Writer{
        keyframeInterval: interval,
        // ...
    }, nil
}

// Later, during WriteFrame:
if w.frameCount % w.keyframeInterval == 0 {  // line 83 — panic if interval == 0

Impact

Runtime panic crashes the process. Any caller that passes 0 (e.g., from unchecked user config) gets an unrecoverable panic instead of an error.

Suggested Fix

Validate at construction time:

func NewWriterWithKeyframeInterval(w io.Writer, interval uint32) (*Writer, error) {
    if interval == 0 {
        return nil, fmt.Errorf("keyframe interval must be > 0")
    }
    // ...
}

🤖 Filed by Metis

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions