Skip to content

Commit eda8a7d

Browse files
author
Jason Yellick
committed
[FAB-4427] Fix unchecked errors in configtxgen
There was an unchecked file read in configtxgen which caused missing files to be ignored when read. This ordinarilly would not have resulted in a crash, but additionally, the envelope extract utility method did not have a nil check on the block data, which caused a crash. This CR fixes both of these issues (adding tests for each). Change-Id: Ie21b397f86a9c88da4b488c81f3f82df8b2ab3c7 Signed-off-by: Jason Yellick <[email protected]>
1 parent 52853f8 commit eda8a7d

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

common/configtx/tool/configtxgen/main.go

+4
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ func doOutputAnchorPeersUpdate(conf *genesisconfig.Profile, channelID string, ou
174174
func doInspectBlock(inspectBlock string) error {
175175
logger.Info("Inspecting block")
176176
data, err := ioutil.ReadFile(inspectBlock)
177+
if err != nil {
178+
return fmt.Errorf("Could not read block %s", inspectBlock)
179+
}
180+
177181
logger.Info("Parsing genesis block")
178182
block := &cb.Block{}
179183
err = proto.Unmarshal(data, block)

common/configtx/tool/configtxgen/main_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func TestMain(m *testing.M) {
4242
os.Exit(testResult)
4343
}
4444

45+
func TestInspectMissing(t *testing.T) {
46+
assert.Error(t, doInspectBlock("NonSenseBlockFileThatDoesn'tActuallyExist"), "Missing block")
47+
}
48+
4549
func TestInspectBlock(t *testing.T) {
4650
blockDest := tmpDir + string(os.PathSeparator) + "block"
4751

protos/utils/commonutils.go

+4
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ func ExtractEnvelopeOrPanic(block *cb.Block, index int) *cb.Envelope {
138138

139139
// ExtractEnvelope retrieves the requested envelope from a given block and unmarshals it.
140140
func ExtractEnvelope(block *cb.Block, index int) (*cb.Envelope, error) {
141+
if block.Data == nil {
142+
return nil, fmt.Errorf("No data in block")
143+
}
144+
141145
envelopeCount := len(block.Data.Data)
142146
if index < 0 || index >= envelopeCount {
143147
return nil, fmt.Errorf("Envelope index out of bounds")

protos/utils/commonutils_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ func TestUnmarshalEnvelopeOfType(t *testing.T) {
153153

154154
}
155155

156+
func TestExtractEnvelopeNilData(t *testing.T) {
157+
block := &cb.Block{}
158+
_, err := ExtractEnvelope(block, 0)
159+
assert.Error(t, err, "Nil data")
160+
}
161+
156162
func TestExtractEnvelopeWrongIndex(t *testing.T) {
157163
block := testBlock()
158164
if _, err := ExtractEnvelope(block, len(block.GetData().Data)); err == nil {

0 commit comments

Comments
 (0)