jezek / xgb

The X Go Binding is a low-level API to communicate with the X server. It is modeled on XCB and supports many X extensions.
Other
130 stars 13 forks source link

createWindowRequest crashes when three or more values given. #8

Closed IoIxD closed 2 years ago

IoIxD commented 2 years ago
err = xproto.CreateWindowChecked(X, screen.RootDepth, windowID, screen.Root,
        0, 0, 320, 240, 0,
        xproto.WindowClassInputOutput, screen.RootVisual, 
        0xffffffff,
        xproto.CwBackPixel | xproto.CwEventMask | xproto.CwOverrideRedirect).Check()
    if(err != nil) {return}

...results in this...

panic: runtime error: index out of range [2] with length 2

goroutine 1 [running]:
github.com/jezek/xgb/xproto.createWindowRequest(0x2fa0?, 0x18, 0x4400001, 0x6c4, 0x0, 0x0, 0x140, 0xc8, 0x0, 0x1, ...)
        /home/gavin/go/pkg/mod/github.com/jezek/xgb@v1.0.1/xproto/xproto.go:8472 +0x534
github.com/jezek/xgb/xproto.CreateWindowChecked(0xc00015c000, 0x80?, 0x0?, 0x515601?, 0x0?, 0x0?, 0xdd10?, 0x13?, 0xc0?, 0x1, ...)
        /home/gavin/go/pkg/mod/github.com/jezek/xgb@v1.0.1/xproto/xproto.go:8416 +0x175
github.com/Terminal-Wars/TermUI.NewWindowComplex(0x140, 0xc8, 0x0?, 0x0?, {0xc00013de58, 0x2, 0x2})
        /home/gavin/Projects/TerminalWars/TermUI/window.go:65 +0x20c
github.com/Terminal-Wars/TermUI.NewRawWindow(0xc710?, 0x5?, {0xc00013deec?, 0xc00005c708?, 0x440131?})
        /home/gavin/Projects/TerminalWars/TermUI/window.go:43 +0x70
main.main()
        /home/gavin/Projects/TerminalWars/TermUI/examples/popup/popup.go:13 +0x52
exit status 2

It doesn't matter whether it's OverrideRedirect or CwCursor, it crashes either way at this point:

    for i := 0; i < xgb.PopCount(int(ValueMask)); i++ {
>>>     xgb.Put32(buf[b:], ValueList[i])
        b += 4
    }

This would lead me to believe that I need to set something in the value mask in addition to adding something to the value list, but upon further research it would seem that I only even need to set the exposure mask (also, xgb.PopCount(int(ValueMask)) returns 3). Interesting still, I tried to multiply the equation used to determine the size of buf by 2, and I still get the error. Switching Put32 to Put64 and making ValueList[i] a uint64 certainly doesn't help either. There's probably something I'm missing but if there's a bug then I'm certainly not sure what's causing it.

jezek commented 2 years ago

You call err = xproto.CreateWindowChecked(..., 0xffffffff, xproto.CwBackPixel | xproto.CwEventMask | xproto.CwOverrideRedirect), but the function is defined as func CreateWindowChecked(..., ValueMask uint32, ValueList []uint32) CreateWindowCookie. The last parameter should be a slice ([] uint32) and the parameter before should be the masks. How did you even compile? (I see, you pasted here bad example here.)

See usage in tests or in examples or in an example I did for previous issue. The idea is, that first comes the mask (concatenating mask types) and then a slice with the same amount of values for each mask type (mask values have to be ordered by mask type number ascending). See xproto docs for available mask values (all should have counterparts in xgb).

I see in your code you give 3 mask types, but the flags slice holds only 2 values. You need the same count of slice values as there are mask types (in your case 3).

IoIxD commented 2 years ago

Ah, I see. Also yeah I forgot to surround the uint32 in the example with []uint32{} as I normally would.

Modifying my code to have defaultFlags return an array of flags that are as long as I want them to removes the error. Thank you!