Skip to content

Commit

Permalink
Add maxTriangleCount constant and validate triangle count for STL files
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisreddington authored Dec 9, 2024
1 parent 3d4a686 commit d15e2cc
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions stl/stl.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const (
// - Attribute count: 2 bytes
// Total: 50 bytes
triangleSize = (12 * 4) + 2

// maxTriangleCount defines the maximum number of triangles allowed in an STL file.
maxTriangleCount = uint64(math.MaxUint32)
)

// bufferWriter encapsulates common buffer writing operations
Expand Down Expand Up @@ -140,16 +143,14 @@ func WriteSTLBinary(filename string, triangles []types.Triangle) error {
return err
}

triangleCountInt := len(triangles)
if triangleCountInt < 0 {
return errors.New(errors.ValidationError, "triangle count cannot be negative", nil)
}
if triangleCountInt > int(math.MaxUint32) {
triangleCount := uint64(len(triangles))
if triangleCount > maxTriangleCount {
return errors.New(errors.ValidationError, "triangle count exceeds valid range for STL format", nil)
}

triangleCount := uint32(triangleCountInt)
if err := writeTriangleCount(writer, triangleCount); err != nil {
// Now safely convert to uint32 since we know it's in range
triangleCountUint32 := uint32(triangleCount)
if err := writeTriangleCount(writer, triangleCountUint32); err != nil {
return err
}

Expand Down

0 comments on commit d15e2cc

Please sign in to comment.