Skip to content

Commit 53e8891

Browse files
Merge pull request #29 from github/triangle-count-tweaks
Add validation for maximum triangle count in STL files
2 parents 3d4a686 + d15e2cc commit 53e8891

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

stl/stl.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ const (
3838
// - Attribute count: 2 bytes
3939
// Total: 50 bytes
4040
triangleSize = (12 * 4) + 2
41+
42+
// maxTriangleCount defines the maximum number of triangles allowed in an STL file.
43+
maxTriangleCount = uint64(math.MaxUint32)
4144
)
4245

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

143-
triangleCountInt := len(triangles)
144-
if triangleCountInt < 0 {
145-
return errors.New(errors.ValidationError, "triangle count cannot be negative", nil)
146-
}
147-
if triangleCountInt > int(math.MaxUint32) {
146+
triangleCount := uint64(len(triangles))
147+
if triangleCount > maxTriangleCount {
148148
return errors.New(errors.ValidationError, "triangle count exceeds valid range for STL format", nil)
149149
}
150150

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

0 commit comments

Comments
 (0)