satellite/metabase: add defensive check on copy object

- instead of closing over the outer err variable, potentially
  overwriting some errors or something, declare local variables.

- double check that we got the number of rows we expected to get
  and error otherwise. this prevents a possible source of inserting
  bogus rows into the database.

Change-Id: I30662be2727afe0a90e4215a182fedc2648d1169
This commit is contained in:
Jeff Wendling 2022-06-23 18:31:12 -04:00 committed by JT Olio
parent e00423e2e9
commit 3a63939fc9

View File

@ -249,7 +249,7 @@ func (db *DB) FinishCopyObject(ctx context.Context, opts FinishCopyObject) (obje
`, originalObject.StreamID, originalObject.SegmentCount))(func(rows tagsql.Rows) error { `, originalObject.StreamID, originalObject.SegmentCount))(func(rows tagsql.Rows) error {
index := 0 index := 0
for rows.Next() { for rows.Next() {
err = rows.Scan( err := rows.Scan(
&positions[index], &positions[index],
&expiresAts[index], &expiresAts[index],
&rootPieceIDs[index], &rootPieceIDs[index],
@ -263,9 +263,14 @@ func (db *DB) FinishCopyObject(ctx context.Context, opts FinishCopyObject) (obje
index++ index++
} }
if err = rows.Err(); err != nil { if err := rows.Err(); err != nil {
return err return err
} }
if index != int(originalObject.SegmentCount) {
return Error.New("could not load all of the segment information")
}
return nil return nil
}) })
if err != nil { if err != nil {