Skip to content

Commit f1b87bf

Browse files
committed
cue: disallow string and bytes indexing and slicing
According to spec update. Allowing these operations hurts interoperability. Users can now use builtin package functions instead. Note that string slicing was already not implemented according to spec, and thus broken. Change-Id: I5100f65b294c8bc4383f6019c5600834dc9221a9 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2920 Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent ff51e07 commit f1b87bf

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

cue/eval.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (x *indexExpr) evalPartial(ctx *context) (result evaluated) {
9191
const msgType = "invalid operation: %[5]s (type %[3]s does not support indexing)"
9292
const msgIndexType = "invalid %[5]s index %[1]s (type %[3]s)"
9393

94-
val := e.eval(x.x, listKind|structKind|stringKind|bytesKind, msgType, x)
94+
val := e.eval(x.x, listKind|structKind, msgType, x)
9595
k := val.kind()
9696
index := e.eval(x.index, stringKind|intKind, msgIndexType, k)
9797

@@ -136,7 +136,7 @@ func (x *sliceExpr) evalPartial(ctx *context) (result evaluated) {
136136
e := newEval(ctx, true)
137137
const msgType = "cannot slice %[2]s (type %[3]s)"
138138
const msgInvalidIndex = "invalid slice index %[1]s (type %[3]s)"
139-
val := e.eval(x.x, listKind|stringKind, msgType)
139+
val := e.eval(x.x, listKind, msgType)
140140
lo := e.evalAllowNil(x.lo, intKind, msgInvalidIndex)
141141
hi := e.evalAllowNil(x.hi, intKind, msgInvalidIndex)
142142
var low, high *numLit

cue/resolve_test.go

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -787,18 +787,20 @@ func TestResolve(t *testing.T) {
787787
e6: (*[]|{})[1]
788788
`,
789789
out: `<0>{a: 2, b: "bar", c: _|_("3":invalid list index "3" (type string)), l: [], d: _|_([]:index 0 out of bounds), e1: _|_("":invalid list index "" (type string)), e2: _|_(2:invalid operation: 2[2] (type int does not support indexing)), e3: _|_(true:invalid list index true (type bool)), e4: _|_([1,2,3]:index 3 out of bounds), e5: _|_(-1:invalid list index -1 (index must be non-negative)), e6: _|_([]:index 1 out of bounds)}`,
790-
}, {
791-
desc: "string index",
792-
in: `
793-
a0: "abc"[0]
794-
a1: "abc"[1]
795-
a2: "abc"[2]
796-
a3: "abc"[3]
797-
a4: "abc"[-1]
798-
799-
b: "zoëven"[2]
800-
`,
801-
out: `<0>{a0: "a", a1: "b", a2: "c", a3: _|_("abc":index 3 out of bounds), a4: _|_(-1:invalid string index -1 (index must be non-negative)), b: "ë"}`,
790+
// }, {
791+
// NOTE: string indexing no longer supported.
792+
// Keeping it around until this is no longer an experiment.
793+
// desc: "string index",
794+
// in: `
795+
// a0: "abc"[0]
796+
// a1: "abc"[1]
797+
// a2: "abc"[2]
798+
// a3: "abc"[3]
799+
// a4: "abc"[-1]
800+
801+
// b: "zoëven"[2]
802+
// `,
803+
// out: `<0>{a0: "a", a1: "b", a2: "c", a3: _|_("abc":index 3 out of bounds), a4: _|_(-1:invalid string index -1 (index must be non-negative)), b: "ë"}`,
802804
}, {
803805
desc: "disjunctions of lists",
804806
in: `
@@ -823,25 +825,27 @@ func TestResolve(t *testing.T) {
823825
824826
`,
825827
out: `<0>{a: [], b: [], e1: _|_(1:slice bounds out of range), e2: _|_([0]:negative slice index), e3: _|_([0]:invalid slice index: 1 > 0), e4: _|_(2:slice bounds out of range), e5: _|_(4:cannot slice 4 (type int)), e6: _|_("":invalid slice index "" (type string)), e7: _|_("9":invalid slice index "9" (type string))}`,
826-
}, {
827-
desc: "string slice",
828-
in: `
829-
a0: ""[0:0]
830-
a1: ""[:]
831-
a2: ""[0:]
832-
a3: ""[:0]
833-
b0: "abc"[0:0]
834-
b1: "abc"[0:1]
835-
b2: "abc"[0:2]
836-
b3: "abc"[0:3]
837-
b4: "abc"[3:3]
838-
b5: "abc"[1:]
839-
b6: "abc"[:2]
840-
841-
// TODO: supported extended graphemes, instead of just runes.
842-
u: "Spaß"[3:4]
843-
`,
844-
out: `<0>{a0: "", a1: "", a2: "", a3: "", b0: "", b1: "a", b2: "ab", b3: "abc", b4: "", b5: "bc", b6: "ab", u: "ß"}`,
828+
// }, {
829+
// NOTE: string indexing no longer supported.
830+
// Keeping it around until this is no longer an experiment.
831+
// desc: "string slice",
832+
// in: `
833+
// a0: ""[0:0]
834+
// a1: ""[:]
835+
// a2: ""[0:]
836+
// a3: ""[:0]
837+
// b0: "abc"[0:0]
838+
// b1: "abc"[0:1]
839+
// b2: "abc"[0:2]
840+
// b3: "abc"[0:3]
841+
// b4: "abc"[3:3]
842+
// b5: "abc"[1:]
843+
// b6: "abc"[:2]
844+
845+
// // TODO: supported extended graphemes, instead of just runes.
846+
// u: "Spaß"[3:4]
847+
// `,
848+
// out: `<0>{a0: "", a1: "", a2: "", a3: "", b0: "", b1: "a", b2: "ab", b3: "abc", b4: "", b5: "bc", b6: "ab", u: "ß"}`,
845849
}, {
846850
desc: "list types",
847851
in: `

0 commit comments

Comments
 (0)