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

Fix crash when cfg.Painter is nil after another SetConfig() call #221

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tpodowd
Copy link
Contributor

@tpodowd tpodowd commented Feb 27, 2023

The following example code calls SetConfig() one more time after setting initializing readline instance with a different config. This is a contrived example, but this is useful when an application has a mode change and needs a different config (not just the prompt as in this example).

package main

import (
	"fmt"
	"strings"
	"github.com/chzyer/readline"
)

func main() {
	l, err := readline.NewEx(&readline.Config{})
	if err != nil {
		panic(err)
	}
	defer l.Close()

	l.SetConfig(&readline.Config{Prompt: "> "})
	line, err := l.Readline()
	fmt.Printf("you: %s\n", strings.TrimSpace(line))
}

Without the fix, this program will crash like this:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x4c23b4]

goroutine 1 [running]:
github.com/chzyer/readline.(*RuneBuffer).output(0xc0000b21e0, 0x51f648, 0xc0000b6050, 0x0)
	/home/tpodowd/work/readline/runebuf.go:494 +0xd4
github.com/chzyer/readline.(*RuneBuffer).print(0xc0000b21e0)
	/home/tpodowd/work/readline/runebuf.go:475 +0x2b
github.com/chzyer/readline.(*RuneBuffer).Refresh(0xc0000b21e0, 0x0)
	/home/tpodowd/work/readline/runebuf.go:465 +0xbf
github.com/chzyer/readline.(*Operation).Runes(0xc0000bc000, 0x0, 0x0, 0x0, 0x0, 0x0)
	/home/tpodowd/work/readline/operation.go:390 +0xf2
github.com/chzyer/readline.(*Operation).String(...)
	/home/tpodowd/work/readline/operation.go:377
github.com/chzyer/readline.(*Instance).Readline(0xc0000a0060, 0xc0000b4120, 0xc0000b4000, 0x0, 0x4649c5)
	/home/tpodowd/work/readline/readline.go:257 +0x2f
main.main()
	/home/tpodowd/work/readline/example/paint/paint.go:17 +0xdd
exit status 2

The following example code calls SetConfig() one more time after
setting initializing readline instance with a different config.
This is a contrived example, but this is useful when an
application has a mode change and needs a different config
(not just the prompt as in this example).

```
package main

import (
	"fmt"
	"strings"
	"github.com/chzyer/readline"
)

func main() {
	l, err := readline.NewEx(&readline.Config{})
	if err != nil {
		panic(err)
	}
	defer l.Close()

	l.SetConfig(&readline.Config{Prompt: "> "})
	line, err := l.Readline()
	fmt.Printf("you: %s\n", strings.TrimSpace(line))
}
```

Without the fix, this program will crash like this:
```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x4c23b4]

goroutine 1 [running]:
github.com/chzyer/readline.(*RuneBuffer).output(0xc0000b21e0, 0x51f648, 0xc0000b6050, 0x0)
	/home/tpodowd/work/readline/runebuf.go:494 +0xd4
github.com/chzyer/readline.(*RuneBuffer).print(0xc0000b21e0)
	/home/tpodowd/work/readline/runebuf.go:475 +0x2b
github.com/chzyer/readline.(*RuneBuffer).Refresh(0xc0000b21e0, 0x0)
	/home/tpodowd/work/readline/runebuf.go:465 +0xbf
github.com/chzyer/readline.(*Operation).Runes(0xc0000bc000, 0x0, 0x0, 0x0, 0x0, 0x0)
	/home/tpodowd/work/readline/operation.go:390 +0xf2
github.com/chzyer/readline.(*Operation).String(...)
	/home/tpodowd/work/readline/operation.go:377
github.com/chzyer/readline.(*Instance).Readline(0xc0000a0060, 0xc0000b4120, 0xc0000b4000, 0x0, 0x4649c5)
	/home/tpodowd/work/readline/readline.go:257 +0x2f
main.main()
	/home/tpodowd/work/readline/example/paint/paint.go:17 +0xdd
exit status 2
```
slingamn added a commit to ergochat/readline that referenced this pull request Mar 9, 2023
See chzyer/readline#221 ;
we are consolidating (*Config).Init() calls into NewFromConfig and SetConfig,
and consolidating all preparatory mungeing of the config into (*Config).Init.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant