Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: fix VerifierError output with empty log #743

Merged
merged 1 commit into from Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion internal/errors.go
Expand Up @@ -20,7 +20,7 @@ func ErrorWithLog(err error, log []byte) *VerifierError {
// and trimming trailing whitespace before interpreting as a Go string.
truncated := false
if i := bytes.IndexByte(log, 0); i != -1 {
if i == len(log)-1 && !bytes.HasSuffix(log[:i], []byte{'\n'}) {
if i > 0 && i == len(log)-1 && log[i-1] != '\n' {
// The null byte is at the end of the buffer and it's not preceded
// by a newline character. Most likely the buffer was too short.
truncated = true
Expand All @@ -33,6 +33,10 @@ func ErrorWithLog(err error, log []byte) *VerifierError {
}

log = bytes.Trim(log, whitespace)
if len(log) == 0 {
return &VerifierError{err, nil, false}
}

logLines := bytes.Split(log, []byte{'\n'})
lines := make([]string, 0, len(logLines))
for _, line := range logLines {
Expand Down
13 changes: 7 additions & 6 deletions internal/errors_test.go
Expand Up @@ -22,15 +22,16 @@ func TestVerifierErrorWhitespace(t *testing.T) {
)

err := ErrorWithLog(errors.New("test"), b)
qt.Assert(t, err.Error(), qt.Equals, "test: unreachable insn 28")

want := "test: unreachable insn 28"
got := err.Error()
err = ErrorWithLog(errors.New("test"), nil)
qt.Assert(t, err.Error(), qt.Equals, "test")

t.Log(got)
err = ErrorWithLog(errors.New("test"), []byte("\x00"))
qt.Assert(t, err.Error(), qt.Equals, "test")

if want != got {
t.Fatalf("\nwant: %s\ngot: %s", want, got)
}
err = ErrorWithLog(errors.New("test"), []byte(" "))
qt.Assert(t, err.Error(), qt.Equals, "test")
}

func TestVerifierError(t *testing.T) {
Expand Down