Skip to content

Commit 419350d

Browse files
committed
[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 <lehors@us.ibm.com>
1 parent 2b428bf commit 419350d

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

peer/chaincode/upgrade.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ func upgradeCmd(cf *ChaincodeCmdFactory) *cobra.Command {
4040
ValidArgs: []string{"1"},
4141
RunE: func(cmd *cobra.Command, args []string) error {
4242
cf1 := cf
43-
return chaincodeUpgrade(cf1, func() error {
43+
if cf1 == nil {
4444
var err error
45-
if cf1 == nil {
46-
cf1, err = InitCmdFactory(true, true)
47-
if err != nil {
48-
return err
49-
}
45+
cf1, err = InitCmdFactory(true, true)
46+
if err != nil {
47+
return err
5048
}
49+
}
50+
return chaincodeUpgrade(cf1, func() error {
51+
var err error
5152
env, err := upgrade(cmd, cf1)
5253
if err != nil {
5354
return err

peer/chaincode/upgrade_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,27 @@ func TestUpgradeCmdSendTXFail(t *testing.T) {
140140
}
141141
}
142142
}
143+
144+
func TestUpgradeCmdWithNilCF(t *testing.T) {
145+
146+
// trap possible SIGSEV panic
147+
defer func() {
148+
var err error = nil
149+
if r := recover(); r != nil {
150+
err = fmt.Errorf("%v", r)
151+
}
152+
assert.NoError(t, err, "'peer chaincode upgrade' command should have failed without a panic")
153+
}()
154+
155+
channelID = ""
156+
InitMSP()
157+
158+
cmd := upgradeCmd(nil)
159+
addFlags(cmd)
160+
161+
args := []string{"-C", "mychannel", "-n", "example02", "-p", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02",
162+
"-v", "anotherversion", "-c", "{\"Function\":\"init\",\"Args\": [\"param\",\"1\"]}"}
163+
cmd.SetArgs(args)
164+
err := cmd.Execute()
165+
assert.Error(t, err, "'peer chaincode upgrade' command should have failed without a panic")
166+
}

0 commit comments

Comments
 (0)