Skip to content

envconfig does not initialize pointer type which implements TextUnmarshaler #157

Open
@cuonglm

Description

@cuonglm

This program:

package main

import (
	"fmt"
	"log"

	"github.com/kelseyhightower/envconfig"
)

type U string

func (u *U) UnmarshalText(b []byte) error {
	*u = U(string(b))
	return nil
}

type Env struct {
	Users []*U
}

func main() {
	var e Env
	err := envconfig.Process("myapp", &e)
	if err != nil {
		log.Fatal(err.Error())
	}
	fmt.Println("Users:")
	for _, u := range e.Users {
		fmt.Printf("  %s\n", *u)
	}
}

panics:

$ go run main.go
go: found github.com/kelseyhightower/envconfig in github.com/kelseyhightower/envconfig v1.4.0
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x10f9af7]

goroutine 1 [running]:
main.(*U).UnmarshalText(0x0, 0xc00010c0b0, 0x3, 0x8, 0x3, 0x8)
	/Users/cuonglm/main.go:13 +0x57
github.com/kelseyhightower/envconfig.processField(0xc00001822c, 0x3, 0x11160c0, 0xc00012e180, 0x196, 0xc00012e180, 0x196)
	/Users/cuonglm/go/pkg/mod/github.com/kelseyhightower/envconfig@v1.4.0/envconfig.go:251 +0x1262
github.com/kelseyhightower/envconfig.processField(0xc00001822c, 0xe, 0x1108c80, 0xc00012e080, 0x197, 0x0, 0x0)
	/Users/cuonglm/go/pkg/mod/github.com/kelseyhightower/envconfig@v1.4.0/envconfig.go:312 +0xf83
github.com/kelseyhightower/envconfig.Process(0x11375da, 0x5, 0x1105da0, 0xc00012e080, 0x1105520, 0x1162780)
	/Users/cuonglm/go/pkg/mod/github.com/kelseyhightower/envconfig@v1.4.0/envconfig.go:215 +0x47a
main.main()
	/Users/cuonglm/main.go:23 +0x73

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions