Skip to content

Commit

Permalink
[FAB-7470] Fix peer chaincode upgrade SIGSEGV panic
Browse files Browse the repository at this point in the history
Before this change ugrading the chaincode following the Reconfigure
your network tutorial fails with a panic:

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

goroutine 1 [running]:
github.com/hyperledger/fabric/peer/chaincode.chaincodeUpgrade(0x0, 0xc420
3c4720, 0x0, 0x0)
	/opt/gopath/src/github.com/hyperledger/fabric/peer/chaincode/upgr
ade.go:141 +0x4b
[...]
main.main()
	/opt/gopath/src/github.com/hyperledger/fabric/peer/main.go:112 +0
x493

After this change, the command succeeds as expected. This CR adds a
related test to cover the issue.

$ go test -v -run TestUpgradeCmdWithNilCF github.com/hyperledger/fabric/p
eer/chaincode
=== RUN   TestUpgradeCmdWithNilCF
--- FAIL: TestUpgradeCmdWithNilCF (0.02s)
        Error Trace:    upgrade_test.go:86
			asm_amd64.s:509
			panic.go:491
			panic.go:63
			signal_unix.go:367
			upgrade.go:135
			upgrade.go:43
			command.go:599
			command.go:689
			command.go:648
			upgrade_test.go:98
	Error:		Received unexpected error "runtime error: invalid
 memory address or nil pointer dereference"
	Messages:	'peer chaincode upgrade' command should have fail
ed without a panic

FAIL
exit status 1
FAIL	github.com/hyperledger/fabric/peer/chaincode	0.042s

$ go test -v -run TestUpgradeCmdWithNilCF github.com/hyperledger/fabric/p
eer/chaincode
=== RUN   TestUpgradeCmdWithNilCF
Error: Error getting endorser client chaincode: error trying to connect t
o local peer: context deadline exceeded
Usage:
  upgrade [flags]
[...]
--- PASS: TestUpgradeCmdWithNilCF (3.03s)
PASS
ok  	github.com/hyperledger/fabric/peer/chaincode	3.045s

Before CR:
$ go test -cover github.com/hyperledger/fabric/peer/chaincode
ok  	github.com/hyperledger/fabric/peer/chaincode	0.559s
	coverage: 81.3% of statements

After CR:
$ go test -cover github.com/hyperledger/fabric/peer/chaincode
ok  	github.com/hyperledger/fabric/peer/chaincode	3.611s
	coverage: 81.8% of statements

Change-Id: I65a7846c77481ff7dcddb410aebe74b8d6eb005c
Signed-off-by: Arnaud J Le Hors <lehors@us.ibm.com>
  • Loading branch information
lehors committed Dec 15, 2017
1 parent 2b428bf commit 419350d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
13 changes: 7 additions & 6 deletions peer/chaincode/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ func upgradeCmd(cf *ChaincodeCmdFactory) *cobra.Command {
ValidArgs: []string{"1"},
RunE: func(cmd *cobra.Command, args []string) error {
cf1 := cf
return chaincodeUpgrade(cf1, func() error {
if cf1 == nil {
var err error
if cf1 == nil {
cf1, err = InitCmdFactory(true, true)
if err != nil {
return err
}
cf1, err = InitCmdFactory(true, true)
if err != nil {
return err
}
}
return chaincodeUpgrade(cf1, func() error {
var err error
env, err := upgrade(cmd, cf1)
if err != nil {
return err
Expand Down
24 changes: 24 additions & 0 deletions peer/chaincode/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,27 @@ func TestUpgradeCmdSendTXFail(t *testing.T) {
}
}
}

func TestUpgradeCmdWithNilCF(t *testing.T) {

// trap possible SIGSEV panic
defer func() {
var err error = nil
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
}
assert.NoError(t, err, "'peer chaincode upgrade' command should have failed without a panic")
}()

channelID = ""
InitMSP()

cmd := upgradeCmd(nil)
addFlags(cmd)

args := []string{"-C", "mychannel", "-n", "example02", "-p", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02",
"-v", "anotherversion", "-c", "{\"Function\":\"init\",\"Args\": [\"param\",\"1\"]}"}
cmd.SetArgs(args)
err := cmd.Execute()
assert.Error(t, err, "'peer chaincode upgrade' command should have failed without a panic")
}

0 comments on commit 419350d

Please sign in to comment.