From 419350da5ba21a9e760d105c0588f4003082fb95 Mon Sep 17 00:00:00 2001 From: Arnaud J Le Hors Date: Fri, 15 Dec 2017 15:44:10 +0100 Subject: [PATCH] [FAB-7470] Fix peer chaincode upgrade SIGSEGV panic 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 --- peer/chaincode/upgrade.go | 13 +++++++------ peer/chaincode/upgrade_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/peer/chaincode/upgrade.go b/peer/chaincode/upgrade.go index a16ad2b0c67..5de8c1a3155 100644 --- a/peer/chaincode/upgrade.go +++ b/peer/chaincode/upgrade.go @@ -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 diff --git a/peer/chaincode/upgrade_test.go b/peer/chaincode/upgrade_test.go index e86ebb72208..fde30c6da60 100644 --- a/peer/chaincode/upgrade_test.go +++ b/peer/chaincode/upgrade_test.go @@ -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") +}