diff --git a/mmdbtype/types.go b/mmdbtype/types.go index 3cf09cf..737991c 100644 --- a/mmdbtype/types.go +++ b/mmdbtype/types.go @@ -723,15 +723,15 @@ func writeCtrlByte(w writer, t DataType) (int64, error) { switch { case size < firstSize: firstByte |= byte(size) - case size <= secondSize: + case size < secondSize: firstByte |= 29 leftOver = size - firstSize leftOverSize = 1 - case size <= thirdSize: + case size < thirdSize: firstByte |= 30 leftOver = size - secondSize leftOverSize = 2 - case size <= maxSize: + case size < maxSize: firstByte |= 31 leftOver = size - thirdSize leftOverSize = 3 @@ -739,7 +739,7 @@ func writeCtrlByte(w writer, t DataType) (int64, error) { return 0, fmt.Errorf( "cannot store %d bytes; max size is %d", size, - maxSize, + maxSize-1, ) } diff --git a/tree_test.go b/tree_test.go index a8e6e0f..b2fa3f5 100644 --- a/tree_test.go +++ b/tree_test.go @@ -6,6 +6,7 @@ import ( "math/big" "net" "os" + "strings" "testing" "time" @@ -110,6 +111,36 @@ func TestTreeInsertAndGet(t *testing.T) { "utf8_string": "unicode! ☯ - ♫", } + stringsGetRecord := mmdbtype.Map{ + // firstSize + "size28": mmdbtype.String(strings.Repeat("*", 28)), + "size29": mmdbtype.String(strings.Repeat("*", 29)), + "size30": mmdbtype.String(strings.Repeat("*", 30)), + // secondSize + "size284": mmdbtype.String(strings.Repeat("*", 284)), + "size285": mmdbtype.String(strings.Repeat("*", 285)), + "size286": mmdbtype.String(strings.Repeat("*", 286)), + // thirdSize + "size65820": mmdbtype.String(strings.Repeat("*", 65820)), + "size65821": mmdbtype.String(strings.Repeat("*", 65821)), + "size65822": mmdbtype.String(strings.Repeat("*", 65822)), + // maxSize + "maxSizeMinus1": mmdbtype.String(strings.Repeat("*", 16843036)), + } + + var stringsLookupRecord any = map[string]any{ + "size28": strings.Repeat("*", 28), + "size29": strings.Repeat("*", 29), + "size30": strings.Repeat("*", 30), + "size284": strings.Repeat("*", 284), + "size285": strings.Repeat("*", 285), + "size286": strings.Repeat("*", 286), + "size65820": strings.Repeat("*", 65820), + "size65821": strings.Repeat("*", 65821), + "size65822": strings.Repeat("*", 65822), + "maxSizeMinus1": strings.Repeat("*", 16843036), + } + tests := []struct { name string disableIPv4Aliasing bool @@ -536,6 +567,26 @@ func TestTreeInsertAndGet(t *testing.T) { }, expectedNodeCount: 375, }, + { + name: "insertion of strings at boundary control byte size", + inserts: []testInsert{ + { + network: "1.1.1.1/32", + start: "1.1.1.1", + end: "1.1.1.1", + value: stringsGetRecord, + }, + }, + gets: []testGet{ + { + ip: "1.1.1.1", + expectedNetwork: "1.1.1.1/32", + expectedGetValue: stringsGetRecord, + expectedLookupValue: &stringsLookupRecord, + }, + }, + expectedNodeCount: 375, + }, } for _, recordSize := range []int{24, 28, 32} {