Skip to content

Commit 6c78d72

Browse files
committed
fix: guard possible nil panics
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent 4b8f432 commit 6c78d72

File tree

11 files changed

+82
-7
lines changed

11 files changed

+82
-7
lines changed

cmd/gouroboros/chainsync.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func testChainSync(f *globalFlags) {
170170
os.Exit(1)
171171
}
172172
}()
173-
oConn, err = ouroboros.New(
173+
o, err := ouroboros.New(
174174
ouroboros.WithConnection(conn),
175175
ouroboros.WithNetworkMagic(uint32(f.networkMagic)),
176176
ouroboros.WithErrorChan(errorChan),
@@ -183,6 +183,11 @@ func testChainSync(f *globalFlags) {
183183
fmt.Printf("ERROR: %s\n", err)
184184
os.Exit(1)
185185
}
186+
if o == nil {
187+
fmt.Println("ERROR: empty connection")
188+
os.Exit(1)
189+
}
190+
oConn = o
186191

187192
var point common.Point
188193
if chainSyncFlags.tip {
@@ -260,6 +265,9 @@ func chainSyncRollForwardHandler(
260265
blockSlot := v.SlotNumber()
261266
blockHash, _ := hex.DecodeString(v.Hash())
262267
var err error
268+
if oConn == nil {
269+
return fmt.Errorf("empty ouroboros connection, aborting!")
270+
}
263271
block, err = oConn.BlockFetch().Client.GetBlock(common.NewPoint(blockSlot, blockHash))
264272
if err != nil {
265273
return err

ledger/allegra/pparams_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ func TestAllegraUtxorpc(t *testing.T) {
132132
result := inputParams.Utxorpc()
133133

134134
if !reflect.DeepEqual(result, expectedUtxorpc) {
135-
t.Fatalf("Utxorpc() test failed for Allegra:\nExpected: %#v\nGot: %#v", expectedUtxorpc, result)
135+
t.Fatalf(
136+
"Utxorpc() test failed for Allegra:\nExpected: %#v\nGot: %#v",
137+
expectedUtxorpc,
138+
result,
139+
)
136140
}
137141
}

ledger/alonzo/pparams.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ func (p *AlonzoProtocolParameters) Update(
7070
}
7171

7272
func (p *AlonzoProtocolParameters) UpdateFromGenesis(genesis *AlonzoGenesis) {
73+
if genesis == nil {
74+
return
75+
}
7376
// XXX: do we need to convert this?
7477
p.AdaPerUtxoByte = genesis.LovelacePerUtxoWord
7578
p.MaxValueSize = genesis.MaxValueSize
@@ -144,7 +147,9 @@ func (p *AlonzoProtocolParameters) Utxorpc() *cardano.PParams {
144147
MaxValueSize: uint64(p.MaxValueSize),
145148
CollateralPercentage: uint64(p.CollateralPercentage),
146149
MaxCollateralInputs: uint64(p.MaxCollateralInputs),
147-
CostModels: common.ConvertToUtxorpcCardanoCostModels(p.CostModels),
150+
CostModels: common.ConvertToUtxorpcCardanoCostModels(
151+
p.CostModels,
152+
),
148153
Prices: &cardano.ExPrices{
149154
Memory: &cardano.RationalNumber{
150155
Numerator: int32(p.ExecutionCosts.MemPrice.Num().Int64()),

ledger/alonzo/pparams_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ func TestAlonzoUtxorpc(t *testing.T) {
294294
result := inputParams.Utxorpc()
295295

296296
if !reflect.DeepEqual(result, expectedUtxorpc) {
297-
t.Fatalf("Utxorpc() test failed for Alonzo:\nExpected: %#v\nGot: %#v", expectedUtxorpc, result)
297+
t.Fatalf(
298+
"Utxorpc() test failed for Alonzo:\nExpected: %#v\nGot: %#v",
299+
expectedUtxorpc,
300+
result,
301+
)
298302
}
299303
}

ledger/conway/pparams.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ func (p *ConwayProtocolParameters) Update(
211211
}
212212

213213
func (p *ConwayProtocolParameters) UpdateFromGenesis(genesis *ConwayGenesis) {
214+
if genesis == nil {
215+
return
216+
}
214217
p.MinCommitteeSize = genesis.MinCommitteeSize
215218
p.CommitteeTermLimit = genesis.CommitteeTermLimit
216219
p.GovActionValidityPeriod = genesis.GovActionValidityPeriod

ledger/mary/pparams_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ func TestMaryUtxorpc(t *testing.T) {
145145
result := inputParams.Utxorpc()
146146

147147
if !reflect.DeepEqual(result, expectedUtxorpc) {
148-
t.Fatalf("Utxorpc() test failed for Mary:\nExpected: %#v\nGot: %#v", expectedUtxorpc, result)
148+
t.Fatalf(
149+
"Utxorpc() test failed for Mary:\nExpected: %#v\nGot: %#v",
150+
expectedUtxorpc,
151+
result,
152+
)
149153
}
150154
}

ledger/shelley/pparams.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ func (p *ShelleyProtocolParameters) Update(
9898
}
9999

100100
func (p *ShelleyProtocolParameters) UpdateFromGenesis(genesis *ShelleyGenesis) {
101+
if genesis == nil {
102+
return
103+
}
101104
genesisParams := genesis.ProtocolParameters
102105
p.MinFeeA = genesisParams.MinFeeA
103106
p.MinFeeB = genesisParams.MinFeeB

ledger/shelley/pparams_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ func TestShelleyUtxorpc(t *testing.T) {
154154
result := inputParams.Utxorpc()
155155

156156
if !reflect.DeepEqual(result, expectedUtxorpc) {
157-
t.Fatalf("Utxorpc() test failed for Shelley:\nExpected: %#v\nGot: %#v", expectedUtxorpc, result)
157+
t.Fatalf(
158+
"Utxorpc() test failed for Shelley:\nExpected: %#v\nGot: %#v",
159+
expectedUtxorpc,
160+
result,
161+
)
158162
}
159163
}

ledger/verify_block.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ func VerifyBlock(block BlockHexCbor) (error, bool, string, uint64, uint64) {
5252
headerUnmarshalError.Error(),
5353
), false, "", 0, 0
5454
}
55+
if header == nil {
56+
return fmt.Errorf(
57+
"VerifyBlock: header returned empty",
58+
), false, "", 0, 0
59+
}
5560
isKesValid, errKes := VerifyKes(header, slotPerKesPeriod)
5661
if errKes != nil {
5762
return fmt.Errorf(

protocol/handshake/server.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,21 @@ func (s *Server) handleProposeVersions(msg protocol.Message) error {
129129
// Decode protocol parameters for selected version
130130
versionInfo := protocol.GetProtocolVersion(proposedVersion)
131131
versionData := s.config.ProtocolVersionMap[proposedVersion]
132+
if versionData == nil {
133+
msgRefuse := NewMsgRefuse(
134+
[]any{
135+
RefuseReasonDecodeError,
136+
proposedVersion,
137+
fmt.Errorf("handshake failed: refused due to empty version data"),
138+
},
139+
)
140+
if err := s.SendMessage(msgRefuse); err != nil {
141+
return err
142+
}
143+
return fmt.Errorf(
144+
"handshake failed: refused due to empty version data",
145+
)
146+
}
132147
proposedVersionData, err := versionInfo.NewVersionDataFromCborFunc(
133148
msgProposeVersions.VersionMap[proposedVersion],
134149
)
@@ -148,6 +163,22 @@ func (s *Server) handleProposeVersions(msg protocol.Message) error {
148163
err,
149164
)
150165
}
166+
if proposedVersionData == nil {
167+
msgRefuse := NewMsgRefuse(
168+
[]any{
169+
RefuseReasonDecodeError,
170+
proposedVersion,
171+
fmt.Errorf("handshake failed: refused due to empty version map"),
172+
},
173+
)
174+
if err := s.SendMessage(msgRefuse); err != nil {
175+
return err
176+
}
177+
return fmt.Errorf(
178+
"handshake failed: refused due to empty version map",
179+
)
180+
}
181+
151182
// Check network magic
152183
if proposedVersionData.NetworkMagic() != versionData.NetworkMagic() {
153184
errMsg := fmt.Sprintf(

0 commit comments

Comments
 (0)