Repository: genjidb/genji
Pull request #276 (2020-10-18 17:25:54)

Fix fuzz workflow failing when there are no crashers
Interrupting go-fuzz always returns non-zero error code, so we should ignore it. Check whether `crashers` directory exists instead.


codecov-io: # [Codecov](https://codecov.io/gh/genjidb/genji/pull/276?src=pr&el=h1) Report
> Merging [#276](https://codecov.io/gh/genjidb/genji/pull/276?src=pr&el=desc) into [master](https://codecov.io/gh/genjidb/genji/commit/0ca41de8c5604b829f11bca60ccc00dc366ced8e?el=desc) will **decrease** coverage by `0.03%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/genjidb/genji/pull/276/graphs/tree.svg?width=650&height=150&src=pr&token=nwR1GpvtyH)](https://codecov.io/gh/genjidb/genji/pull/276?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #276      +/-   ##
==========================================
- Coverage   61.32%   61.29%   -0.04%     
==========================================
  Files          68       68              
  Lines        6348     6348              
==========================================
- Hits         3893     3891       -2     
- Misses       1938     1940       +2     
  Partials      517      517              
```


| [Impacted Files](https://codecov.io/gh/genjidb/genji/pull/276?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [engine/memoryengine/store.go](https://codecov.io/gh/genjidb/genji/pull/276/diff?src=pr&el=tree#diff-ZW5naW5lL21lbW9yeWVuZ2luZS9zdG9yZS5nbw==) | `91.96% <0.00%> (-1.79%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/genjidb/genji/pull/276?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/genjidb/genji/pull/276?src=pr&el=footer). Last update [0ca41de...fc99d2e](https://codecov.io/gh/genjidb/genji/pull/276?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/document/value.go b/document/value.go
index 47e9aefb..c38c96c4 100644
--- a/document/value.go
+++ b/document/value.go
@@ -483,12 +483,13 @@ func calculateFloats(a, b Value, operator byte) (res Value, err error) {
 
 		return NewDoubleValue(xa / xb), nil
 	case '%':
-		if xb == 0 {
+		mod := math.Mod(xa, xb)
+
+		if math.IsNaN(mod) {
 			return NewNullValue(), nil
 		}
 
-		ia, ib := int64(xa), int64(xb)
-		return NewDoubleValue(float64(ia % ib)), nil
+		return NewDoubleValue(mod), nil
 	case '&':
 		ia, ib := int64(xa), int64(xb)
 		return NewIntegerValue(ia & ib), nil


Test patch:
diff --git a/.github/workflows/test-fuzz.yaml b/.github/workflows/test-fuzz.yaml
index 5438e136..bac331ac 100644
--- a/.github/workflows/test-fuzz.yaml
+++ b/.github/workflows/test-fuzz.yaml
@@ -85,7 +85,10 @@ jobs:
       - name: Run tests
         run: |
           go run github.com/dvyukov/go-fuzz/go-fuzz-build -func ${{ matrix.func }} && timeout -s INT -k 30 300 \
-          go run github.com/dvyukov/go-fuzz/go-fuzz -workdir=testdata/fuzz/${{ matrix.func }} -dumpcover
+          go run github.com/dvyukov/go-fuzz/go-fuzz -workdir=testdata/fuzz/${{ matrix.func }} -dumpcover || true
+          if [ -d crashers ]; then
+            exit 1
+          fi
         working-directory: genji/fuzz
 
       - name: Prepare report
diff --git a/document/value_test.go b/document/value_test.go
index c0a08a3b..b6750b4a 100644
--- a/document/value_test.go
+++ b/document/value_test.go
@@ -253,7 +253,7 @@ func TestValueMod(t *testing.T) {
 		{"integer(10)%integer(8)", document.NewIntegerValue(10), document.NewIntegerValue(8), document.NewIntegerValue(2), false},
 		{"integer(10)%float64(8)", document.NewIntegerValue(10), document.NewDoubleValue(8), document.NewDoubleValue(2), false},
 		{"int64(maxint)%float64(maxint)", document.NewIntegerValue(math.MaxInt64), document.NewDoubleValue(math.MaxInt64), document.NewDoubleValue(0), false},
-		{"double(> maxint)%int64(100)", document.NewDoubleValue(math.MaxInt64 + 1000), document.NewIntegerValue(100), document.NewDoubleValue(-8), false},
+		{"double(> maxint)%int64(100)", document.NewDoubleValue(math.MaxInt64 + 1000), document.NewIntegerValue(100), document.NewDoubleValue(8), false},
 		{"int64(100)%float64(> maxint)", document.NewIntegerValue(100), document.NewDoubleValue(math.MaxInt64 + 1000), document.NewDoubleValue(100), false},
 		{"integer(120)%text('120')", document.NewIntegerValue(120), document.NewTextValue("120"), document.NewNullValue(), false},
 		{"text('120')%text('120')", document.NewTextValue("120"), document.NewTextValue("120"), document.NewNullValue(), false},
diff --git a/sql/parser/parser_test.go b/sql/parser/parser_test.go
index 0c287762..e7a151e7 100644
--- a/sql/parser/parser_test.go
+++ b/sql/parser/parser_test.go
@@ -4,9 +4,10 @@ import (
 	"context"
 	"testing"
 
+	"github.com/stretchr/testify/require"
+
 	"github.com/genjidb/genji/sql/planner"
 	"github.com/genjidb/genji/sql/query"
-	"github.com/stretchr/testify/require"
 )
 
 func TestParserMultiStatement(t *testing.T) {
@@ -43,3 +44,10 @@ func TestParserMultiStatement(t *testing.T) {
 		})
 	}
 }
+
+func TestParserDivideByZero(t *testing.T) {
+	// See https://github.com/genjidb/genji/issues/268
+	require.NotPanics(t, func() {
+		_, _ = ParseQuery(context.Background(), "SELECT * FROM t LIMIT 0 % .5")
+	})
+}