Skip to content

Commit a5d4d66

Browse files
committed
fix(stashgen): refuse a built-in integer stored as a 128-bit kind when the code is generated
gensupport converts a built-in integer to a kind of 64 bits or fewer only, and intoKind took any integer target, so a field of a 128-bit EQL type would generate, build, and fail on every Encrypt. No producible EQL type has a 128-bit kind today; intoKind now refuses one, and lowering names the field and both kinds.
1 parent bee78c6 commit a5d4d66

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

‎languages/golang/encrypt/gensupport/widen.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ func isInteger(k Kind) bool {
9393
// integerAs is the integer, given as exactly one of a signed or an unsigned
9494
// value, as the Go type of kind, if it fits. A 128-bit kind has no arm: its
9595
// only Go types are encrypt.Int128 and Uint128, which asKind passes through,
96-
// and stashgen declares no 128-bit kind for a built-in integer field (no
97-
// producible EQL type has one), so a built-in integer never reaches one here;
98-
// if one did, it is refused as not fitting.
96+
// and stashgen refuses a built-in integer field stored as a 128-bit kind, so a
97+
// built-in integer never reaches one here; if one did, it is refused as not
98+
// fitting.
9999
func integerAs(kind Kind, signed *int64, unsigned *uint64) (any, bool) {
100100
inSigned := func(lo, hi int64) (int64, bool) {
101101
if signed != nil {

‎languages/golang/stashgen/engine.go‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ func lowerDeclaration(d Declaration, eqlTypes []EQLType) (*record.Plan, error) {
221221
rf.Kind = wire
222222
} else if _, _, integer := integerRange(eqlType.wire); integer && wireKind(f.GoType) != eqlType.wire && (f.GoType.Kind == KindInt128 || f.GoType.Kind == KindUint128) {
223223
return nil, &FieldError{Type: d.Type, Field: f.GoName, Reason: fmt.Sprintf("%s seals %s, and an %s is not converted to it", f.EQLType, eqlType.wire, f.GoType)}
224+
} else if integer && (f.GoType.Kind == KindInt || f.GoType.Kind == KindUint) {
225+
// intoKind takes every built-in integer except into a
226+
// 128-bit kind, which gensupport does not convert to.
227+
return nil, &FieldError{Type: d.Type, Field: f.GoName, Reason: fmt.Sprintf("%s seals %s, and the built-in integer %s is not converted to it", f.EQLType, eqlType.wire, f.GoType)}
224228
} else {
225229
// The engine checks the kind too (se_plan_check refuses a
226230
// "type" other than the EQL type's plaintext); saying it
@@ -393,10 +397,13 @@ func integerRange(k record.Kind) (signed bool, bits int, ok bool) {
393397
// integer) always converts; a wider one (an int64 into an integer) is range
394398
// checked when a value is sealed, and a value that does not fit is refused
395399
// with encrypt.ErrEncoding. False when t is not a built-in integer: an
396-
// encrypt.Int128 or Uint128 has no conversion to a narrower kind.
400+
// encrypt.Int128 or Uint128 has no conversion to a narrower kind. False for a
401+
// 128-bit target too: gensupport converts a built-in integer to a kind of 64
402+
// bits or fewer only, so such a field is refused here, when the code is
403+
// generated, and not on every Encrypt.
397404
func intoKind(t GoType, target record.Kind) (record.Kind, bool) {
398-
_, _, toInt := integerRange(target)
399-
if !toInt || (t.Kind != KindInt && t.Kind != KindUint) {
405+
_, bits, toInt := integerRange(target)
406+
if !toInt || bits > 64 || (t.Kind != KindInt && t.Kind != KindUint) {
400407
return record.Untyped, false
401408
}
402409
return target, true

‎languages/golang/stashgen/lower_test.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ func TestLoweringWidensIntoAnEQLTypeAndRefusesWhatDoesNotConvert(t *testing.T) {
4242
{Name: "Bigint", Plaintext: KindInt, Producible: true, wire: record.Int64},
4343
{Name: "Real", Plaintext: KindFloat, Producible: true, wire: record.Float32},
4444
{Name: "Double", Plaintext: KindFloat, Producible: true, wire: record.Float64},
45+
// No producible EQL type seals a 128-bit integer today; this one
46+
// holds what a built-in integer field would meet if one did.
47+
{Name: "Hugeint", Plaintext: KindInt128, Producible: true, wire: record.Int128},
4548
}
4649
field := func(goType GoType, eqlType string) Declaration {
4750
return Declaration{Type: "Row", Context: "rows", Fields: []Field{
@@ -57,6 +60,8 @@ func TestLoweringWidensIntoAnEQLTypeAndRefusesWhatDoesNotConvert(t *testing.T) {
5760
{GoType{Name: "encrypt.Uint128", Kind: KindUint128}, "Integer", "Integer seals int32, and an encrypt.Uint128 is not converted to it"},
5861
{basic("float64", KindFloat), "Real", "Real seals float32, and float64 seals as float64"},
5962
{basic("float32", KindFloat), "Double", "Double seals float64, and float32 seals as float32"},
63+
{basic("int64", KindInt), "Hugeint", "Hugeint seals int128, and the built-in integer int64 is not converted to it"},
64+
{basic("uint8", KindUint), "Hugeint", "Hugeint seals int128, and the built-in integer uint8 is not converted to it"},
6065
} {
6166
_, err := lowerDeclaration(field(c.goType, c.eqlType), eql)
6267
fe, ok := err.(*FieldError)

0 commit comments

Comments
 (0)