Skip to content

Commit bc57a0b

Browse files
committed
Remove overly restrictive validation on header feature count in write
For FileWriter, there's no good reason for Header() to enforce an artificially restrictive feature count. Although you cannot *index* more than math.MaxInt features, you can *write* more than that many using the streaming approach with multiple calls to Data(). There's an analogous restriction in FileReader.Header() which could be relaxed in the following way: no validation in Header, but if you try to call Index() or IndexSearch(), it blows up; and if you try to call DataRem() with more than math.MaxInt features remaining, it blows up.
1 parent 9a598fc commit bc57a0b

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

flatgeobuf/file_writer.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package flatgeobuf
66

77
import (
88
"io"
9-
"math"
109

1110
"github.com/gogama/flatgeobuf/flatgeobuf/flat"
1211
"github.com/gogama/flatgeobuf/packedrtree"
@@ -19,13 +18,13 @@ type FileWriter struct {
1918
w io.Writer
2019
// numFeatures is the number of features recorded in the FlatGeobuf
2120
// header.
22-
numFeatures int
21+
numFeatures uint64
2322
// nodeSize is the index node size recorded in the FlatGeobuf
2423
// header.
2524
nodeSize uint16
2625
// featureIndex is the index of the next feature to write, a number
2726
// in the range [0, numFeatures]
28-
featureIndex int
27+
featureIndex uint64
2928
}
3029

3130
// NewFileWriter creates a new FlatGeobuf file writer based on an
@@ -66,10 +65,6 @@ func (w *FileWriter) Header(hdr *flat.Header) (n int, err error) {
6665
err = wrapErr("failed to get header feature count", err)
6766
return
6867
}
69-
if numFeatures > math.MaxInt {
70-
err = wrapErr("header feature count overflows int", err)
71-
return
72-
}
7368

7469
// Cache index node size and check for illegal value.
7570
var nodeSize uint16
@@ -114,7 +109,7 @@ func (w *FileWriter) Header(hdr *flat.Header) (n int, err error) {
114109
}
115110

116111
// Save cached feature count and index node size.
117-
w.numFeatures = int(numFeatures)
112+
w.numFeatures = numFeatures
118113
w.nodeSize = nodeSize
119114

120115
// Transition into the state for writing index.
@@ -145,7 +140,7 @@ func (w *FileWriter) index(index *packedrtree.PackedRTree) (n int, err error) {
145140
w.state = beforeIndex
146141

147142
// Ensure index parameters agree with header parameters.
148-
if w.numFeatures != index.NumRefs() {
143+
if w.numFeatures != uint64(index.NumRefs()) {
149144
err = fmtErr("feature count mismatch (header=%d, index=%d)", w.numFeatures, index.NumRefs())
150145
w.state = afterHeader // Go back to header state.
151146
return
@@ -254,7 +249,7 @@ func (w *FileWriter) IndexData(p []flat.Feature) (n int, err error) {
254249
// FileReader.DataRem, and from flat.GetSizePrefixedRootAsFeature.
255250
func (w *FileWriter) Data(p []flat.Feature) (n int, err error) {
256251
// Ensure we can fit all the requested features.
257-
if err = w.canWriteData(len(p)); err != nil {
252+
if err = w.canWriteData(uint64(len(p))); err != nil {
258253
return
259254
}
260255

@@ -319,7 +314,7 @@ func (w *FileWriter) canWriteIndex() error {
319314
return nil
320315
}
321316

322-
func (w *FileWriter) canWriteData(n int) error {
317+
func (w *FileWriter) canWriteData(n uint64) error {
323318
if w.err != nil {
324319
return w.err
325320
}

flatgeobuf/file_writer_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import (
99
"errors"
1010
"testing"
1111

12-
"github.com/stretchr/testify/mock"
13-
1412
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/mock"
1514
"github.com/stretchr/testify/require"
1615
)
1716

@@ -26,7 +25,19 @@ func TestNewFileWriter(t *testing.T) {
2625
}
2726

2827
func TestFileWriter_Header(t *testing.T) {
28+
t.Run("Error", func(t *testing.T) {
29+
t.Run("Header Already Called", func(t *testing.T) {
30+
// TODO
31+
})
2932

33+
t.Run("Nil Header", func(t *testing.T) {
34+
// TODO
35+
})
36+
37+
t.Run("Corrupt Header: Failed to Get Feature Count", func(t *testing.T) {
38+
// TODO
39+
})
40+
})
3041
}
3142

3243
func TestFileWriter_Index(t *testing.T) {

0 commit comments

Comments
 (0)