hajimehoshi / ebiten

Ebitengine - A dead simple 2D game engine for Go
https://ebitengine.org
Apache License 2.0
11.01k stars 660 forks source link

vector: StrokeLine with 0 length panics #2589

Closed mymmrac closed 1 year ago

mymmrac commented 1 year ago

Ebitengine Version

v2.5.0-alpha.13

Operating System

Go Version (go version)

go version go1.20.1 linux/amd64

What steps will reproduce the problem?

Code: https://go.dev/play/p/iCreoo1rtqJ

If you do the same without debug (in debug I am calling screen.Bounds() it will not crash, at least I didn't experience that once), but with enabled debug it crashes really frequently

Stack trace
panic: graphics: x must be positive

goroutine 66 [running]:
github.com/hajimehoshi/ebiten/v2/internal/graphics.InternalImageSize(...)
        /home/mymmrac/.go/pkg/mod/github.com/hajimehoshi/ebiten/v2@v2.5.0-alpha.13/internal/graphics/math.go:26
github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl.(*Graphics).NewImage(0xc000192700, 0x0, 0x0)
        /home/mymmrac/.go/pkg/mod/github.com/hajimehoshi/ebiten/v2@v2.5.0-alpha.13/internal/graphicsdriver/opengl/graphics.go:109 +0x17d
github.com/hajimehoshi/ebiten/v2/internal/graphicscommand.(*newImageCommand).Exec(0xc0001b79c0, {0x82f7a0?, 0xc000192700?}, 0x7f77bc5d08d8?)
        /home/mymmrac/.go/pkg/mod/github.com/hajimehoshi/ebiten/v2@v2.5.0-alpha.13/internal/graphicscommand/command.go:500 +0x84
github.com/hajimehoshi/ebiten/v2/internal/graphicscommand.(*commandQueue).flush(0xaea3a0, {0x82f7a0?, 0xc000192700?}, 0x1)
        /home/mymmrac/.go/pkg/mod/github.com/hajimehoshi/ebiten/v2@v2.5.0-alpha.13/internal/graphicscommand/command.go:240 +0x5c4
github.com/hajimehoshi/ebiten/v2/internal/graphicscommand.(*commandQueue).Flush.func1()
        /home/mymmrac/.go/pkg/mod/github.com/hajimehoshi/ebiten/v2@v2.5.0-alpha.13/internal/graphicscommand/command.go:168 +0x32
github.com/hajimehoshi/ebiten/v2/internal/thread.(*OSThread).Loop.func1(0xc0004dfef8?, 0xc0004dfed0?)
        /home/mymmrac/.go/pkg/mod/github.com/hajimehoshi/ebiten/v2@v2.5.0-alpha.13/internal/thread/thread.go:51 +0x53
github.com/hajimehoshi/ebiten/v2/internal/thread.(*OSThread).Loop(0xc000024140, {0x82cc40, 0xc0000ae0a0})
        /home/mymmrac/.go/pkg/mod/github.com/hajimehoshi/ebiten/v2@v2.5.0-alpha.13/internal/thread/thread.go:52 +0x85
github.com/hajimehoshi/ebiten/v2/internal/ui.(*userInterfaceImpl).Run.func1()
        /home/mymmrac/.go/pkg/mod/github.com/hajimehoshi/ebiten/v2@v2.5.0-alpha.13/internal/ui/run_glfw_notsinglethread.go:50 +0x5d
golang.org/x/sync/errgroup.(*Group).Go.func1()
        /home/mymmrac/.go/pkg/mod/golang.org/x/sync@v0.1.0/errgroup/errgroup.go:75 +0x64
created by golang.org/x/sync/errgroup.(*Group).Go
        /home/mymmrac/.go/pkg/mod/golang.org/x/sync@v0.1.0/errgroup/errgroup.go:72 +0xa5

What is the expected result?

Window resizes & games does not crash

What happens instead?

Game crashes

Anything else you feel useful to add?

Similar issues in past were: #807, #832 and #1198

hajimehoshi commented 1 year ago

Thanks!

I couldn't reproduce the issue on my Linux (Parallels). I'll try other machines.

hajimehoshi commented 1 year ago

Oh I think I could reproduce the issue by not pressing F3

$ go run ./examples/test/main.go
Starting...
panic: graphics: x must be positive

goroutine 11 [running]:
github.com/hajimehoshi/ebiten/v2/internal/graphics.InternalImageSize(...)
        /media/psf/Home/ebiten/internal/graphics/math.go:26
github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl.(*Graphics).NewImage(0xc000474b00, 0x0, 0x0)
        /media/psf/Home/ebiten/internal/graphicsdriver/opengl/graphics.go:109 +0x17d
github.com/hajimehoshi/ebiten/v2/internal/graphicscommand.(*newImageCommand).Exec(0xc000561a60, {0x8558e0?, 0xc000474b00?}, 0x7f795f1c05b8?)
        /media/psf/Home/ebiten/internal/graphicscommand/command.go:500 +0x84
github.com/hajimehoshi/ebiten/v2/internal/graphicscommand.(*commandQueue).flush(0xb0c520, {0x8558e0?, 0xc000474b00?}, 0x1)
        /media/psf/Home/ebiten/internal/graphicscommand/command.go:240 +0x603
github.com/hajimehoshi/ebiten/v2/internal/graphicscommand.(*commandQueue).Flush.func1()
        /media/psf/Home/ebiten/internal/graphicscommand/command.go:168 +0x32
github.com/hajimehoshi/ebiten/v2/internal/thread.(*OSThread).Loop.func1(0xc0000bbef8?, 0xc0000bbed0?)
        /media/psf/Home/ebiten/internal/thread/thread.go:51 +0x53
github.com/hajimehoshi/ebiten/v2/internal/thread.(*OSThread).Loop(0xc000025580, {0x853250, 0xc000079900})
        /media/psf/Home/ebiten/internal/thread/thread.go:52 +0x85
github.com/hajimehoshi/ebiten/v2/internal/ui.(*userInterfaceImpl).Run.func1()
        /media/psf/Home/ebiten/internal/ui/run_glfw_notsinglethread.go:50 +0x5d
golang.org/x/sync/errgroup.(*Group).Go.func1()
        /home/parallels/go/pkg/mod/golang.org/x/sync@v0.1.0/errgroup/errgroup.go:75 +0x64
created by golang.org/x/sync/errgroup.(*Group).Go
        /home/parallels/go/pkg/mod/golang.org/x/sync@v0.1.0/errgroup/errgroup.go:72 +0xa5
mymmrac commented 1 year ago

Yep, by default debug lines are on => issue present, if you disable it with F3 => no issues

Sorry for misliading explanations

hajimehoshi commented 1 year ago

Minimized case:

package main

import (
    "image/color"

    "github.com/hajimehoshi/ebiten/v2"
    "github.com/hajimehoshi/ebiten/v2/vector"
)

type Game struct {
}

func (g *Game) Update() error {
    return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
    vector.StrokeLine(screen, 0, 0, 0, 0, 2, color.White)
}

func (g *Game) Layout(_, _ int) (int, int) {
    panic("unreachable")
}

func (g *Game) LayoutF(logicalWindowWidth, logicalWindowHeight float64) (float64, float64) {
    return logicalWindowWidth, logicalWindowHeight
}

func main() {
    ebiten.SetWindowTitle("Ebiten Test")
    if err := ebiten.RunGame(&Game{}); err != nil {
        panic(err)
    }
}
mymmrac commented 1 year ago

Hmm, this panics not because of resize, but because in vector.StrokeLine all coords are 0, change at least one of them to non-zero value, and it will not panic

Not panics:

vector.StrokeLine(screen, 1, 0, 0, 0, 2, color.White)
mymmrac commented 1 year ago

Yep, did some testing, seems resizing of window is not related to this issue