Skip to content

Commit 2ddb5ec

Browse files
committed
check readonly on batch append
1 parent 208c7a9 commit 2ddb5ec

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

core/rawdb/freezer_batch.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ func (batch *freezerTableBatch) reset() {
115115
// precautionary parameter to ensure data correctness, but the table will reject already
116116
// existing data.
117117
func (batch *freezerTableBatch) Append(item uint64, data interface{}) error {
118+
// Sanity check. In principle this method shouldn't be called at all
119+
// in readonly mode.
120+
if batch.t.readonly {
121+
return fmt.Errorf("permission error: table in readonly mode")
122+
}
118123
if item != batch.curItem {
119124
return fmt.Errorf("%w: have %d want %d", errOutOrderInsertion, item, batch.curItem)
120125
}
@@ -135,6 +140,11 @@ func (batch *freezerTableBatch) Append(item uint64, data interface{}) error {
135140
// precautionary parameter to ensure data correctness, but the table will reject already
136141
// existing data.
137142
func (batch *freezerTableBatch) AppendRaw(item uint64, blob []byte) error {
143+
// Sanity check. In principle this method shouldn't be called at all
144+
// in readonly mode.
145+
if batch.t.readonly {
146+
return fmt.Errorf("permission error: table in readonly mode")
147+
}
138148
if item != batch.curItem {
139149
return fmt.Errorf("%w: have %d want %d", errOutOrderInsertion, item, batch.curItem)
140150
}

core/rawdb/freezer_table_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -903,13 +903,12 @@ func TestFreezerReadonlyBasics(t *testing.T) {
903903
if !bytes.Equal(v, exp) {
904904
t.Errorf("retrieved value is incorrect")
905905
}
906-
// Now write some data. This should fail either during AppendRaw or Commit
906+
// Now write some data. Append/AppendRaw should fail.
907907
batch := f.newBatch()
908-
writeErr := batch.AppendRaw(32, make([]byte, 1))
909-
if writeErr == nil {
910-
writeErr = batch.commit()
908+
if err := batch.AppendRaw(32, make([]byte, 1)); err == nil {
909+
t.Errorf("Writing to readonly table should fail")
911910
}
912-
if writeErr == nil {
913-
t.Fatalf("Writing to readonly table should fail")
911+
if err := batch.Append(0, make([]byte, 1)); err == nil {
912+
t.Errorf("Writing to readonly table should fail")
914913
}
915914
}

0 commit comments

Comments
 (0)