Skip to content

Commit 6659da2

Browse files
committed
cue: fix linter warnings and divide by zero error
Spec says the result of divide by zero is an error, not infinity. Change-Id: I47faa0cc91325d9a729cbb444aaa924b553604ef Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2864 Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent ed08085 commit 6659da2

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

cue/binop.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ func (x *bound) binOp(ctx *context, src source, op op, other evaluated) evaluate
438438

439439
case diff == 2:
440440
if k&floatKind == 0 && x.op == opGtr && y.op == opLss {
441-
apd.BaseContext.Add(&d, d.SetInt64(1), &a.v)
441+
_, _ = apd.BaseContext.Add(&d, d.SetInt64(1), &a.v)
442442
n := *a
443443
n.k = k
444444
n.v = d
@@ -902,22 +902,37 @@ func (x *numLit) binOp(ctx *context, src source, op op, other evaluated) evaluat
902902
case opLss, opLeq, opEql, opNeq, opGeq, opGtr:
903903
return cmpTonode(src, op, x.v.Cmp(&y.v))
904904
case opAdd:
905-
ctx.Add(&n.v, &x.v, &y.v)
905+
_, _ = ctx.Add(&n.v, &x.v, &y.v)
906906
case opSub:
907-
ctx.Sub(&n.v, &x.v, &y.v)
907+
_, _ = ctx.Sub(&n.v, &x.v, &y.v)
908908
case opMul:
909-
ctx.Mul(&n.v, &x.v, &y.v)
909+
_, _ = ctx.Mul(&n.v, &x.v, &y.v)
910910
case opQuo:
911-
ctx.Quo(&n.v, &x.v, &y.v)
912-
ctx.Reduce(&n.v, &n.v)
911+
cond, _ := ctx.Quo(&n.v, &x.v, &y.v)
912+
if cond.DivisionByZero() {
913+
return ctx.mkErr(src, "divide by zero")
914+
}
915+
_, _, _ = ctx.Reduce(&n.v, &n.v)
913916
n.k = floatKind
914917
case opIDiv:
918+
if y.v.IsZero() {
919+
return ctx.mkErr(src, "divide by zero")
920+
}
915921
intOp(ctx, n, (*big.Int).Div, x, y)
916922
case opIMod:
923+
if y.v.IsZero() {
924+
return ctx.mkErr(src, "divide by zero")
925+
}
917926
intOp(ctx, n, (*big.Int).Mod, x, y)
918927
case opIQuo:
928+
if y.v.IsZero() {
929+
return ctx.mkErr(src, "divide by zero")
930+
}
919931
intOp(ctx, n, (*big.Int).Quo, x, y)
920932
case opIRem:
933+
if y.v.IsZero() {
934+
return ctx.mkErr(src, "divide by zero")
935+
}
921936
intOp(ctx, n, (*big.Int).Rem, x, y)
922937
}
923938
return n
@@ -938,11 +953,11 @@ type intFunc func(z, x, y *big.Int) *big.Int
938953

939954
func intOp(ctx *context, n *numLit, fn intFunc, a, b *numLit) {
940955
var x, y apd.Decimal
941-
ctx.RoundToIntegralValue(&x, &a.v)
956+
_, _ = ctx.RoundToIntegralValue(&x, &a.v)
942957
if x.Negative {
943958
x.Coeff.Neg(&x.Coeff)
944959
}
945-
ctx.RoundToIntegralValue(&y, &b.v)
960+
_, _ = ctx.RoundToIntegralValue(&y, &b.v)
946961
if y.Negative {
947962
y.Coeff.Neg(&y.Coeff)
948963
}
@@ -990,7 +1005,8 @@ func (x *durationLit) binOp(ctx *context, src source, op op, other evaluated) ev
9901005
}
9911006
n.v.SetInt64(int64(x.d))
9921007
d := apd.New(int64(y.d), 0)
993-
ctx.Quo(&n.v, &n.v, d)
1008+
// TODO: check result if this code becomes undead.
1009+
_, _ = ctx.Quo(&n.v, &n.v, d)
9941010
return n
9951011
case opIRem:
9961012
n := &numLit{

cue/resolve_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ func TestBasicRewrite(t *testing.T) {
136136
sum: -1 + +2 // 1
137137
div1: 2.0 / 3 * 6 // 4
138138
div2: 2 / 3 * 6 // 4
139+
divZero: 1.0 / 0
139140
b: 1 != 4
140141
141142
v1: 1.0T/2.0
@@ -158,6 +159,7 @@ func TestBasicRewrite(t *testing.T) {
158159
`sum: 1, ` +
159160
`div1: 4.00000000000000000000000, ` +
160161
`div2: 4.00000000000000000000000, ` +
162+
`divZero: _|_((1.0 / 0):divide by zero), ` +
161163
`b: true, ` +
162164
`v1: 5e+11, ` +
163165
`v2: true, ` +

cue/types_test.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -342,20 +342,12 @@ func TestFloat(t *testing.T) {
342342
kind: FloatKind,
343343
}, {
344344
value: "1/0",
345-
float: "∞",
346-
float64: math.Inf(1),
347-
prec: 2,
348-
fmt: 'f',
349-
err: ErrAbove.Error(),
350-
kind: FloatKind,
351-
}, {
352-
value: "-1/0",
353-
float: "-∞",
354-
float64: math.Inf(-1),
345+
float: "",
346+
float64: 0,
355347
prec: 2,
356348
fmt: 'f',
357-
err: ErrBelow.Error(),
358-
kind: FloatKind,
349+
err: "divide by zero",
350+
kind: BottomKind,
359351
}, {
360352
value: "1.797693134862315708145274237317043567982e+308",
361353
float: "1.8e+308",

0 commit comments

Comments
 (0)