@@ -309,7 +309,9 @@ class StaticBagOfCellsDbLazyImpl : public StaticBagOfCellsDb {
309
309
return 0 ;
310
310
}
311
311
td::Slice offset_view;
312
- CHECK (info_.offset_byte_size <= 8 );
312
+ if (info_.offset_byte_size > 8 ) {
313
+ return td::Status::Error (PSTRING () << " bag-of-cell error: invalid offset_byte_size " << info_.offset_byte_size );
314
+ }
313
315
char arr[8 ];
314
316
td::RwMutex::ReadLock guard;
315
317
if (info_.has_index ) {
@@ -321,19 +323,25 @@ class StaticBagOfCellsDbLazyImpl : public StaticBagOfCellsDb {
321
323
offset_view = td::Slice (index_data_).substr ((td::int64)idx * info_.offset_byte_size , info_.offset_byte_size );
322
324
}
323
325
324
- CHECK (offset_view.size () == (size_t )info_.offset_byte_size );
326
+ if (offset_view.size () != (size_t )info_.offset_byte_size ) {
327
+ return td::Status::Error (PSTRING () << " bag-of-cell error: invalid offset view size" << offset_view.size ());
328
+ }
325
329
return td::narrow_cast<std::size_t >(info_.read_offset (offset_view.ubegin ()));
326
330
}
327
331
328
332
td::Result<td::int64> load_root_idx (int root_i) {
329
- CHECK (root_i >= 0 && root_i < info_.root_count );
333
+ if (root_i < 0 || root_i >= info_.root_count ) {
334
+ return td::Status::Error (PSTRING () << " bag-of-cell error: invalid root index " << root_i);
335
+ }
330
336
if (!info_.has_roots ) {
331
337
return 0 ;
332
338
}
333
339
char arr[8 ];
334
340
TRY_RESULT (idx_view, data_.view (td::MutableSlice (arr, info_.ref_byte_size ),
335
341
info_.roots_offset + (td::int64)root_i * info_.ref_byte_size ));
336
- CHECK (idx_view.size () == (size_t )info_.ref_byte_size );
342
+ if (idx_view.size () != (size_t )info_.ref_byte_size ) {
343
+ return td::Status::Error (PSTRING () << " bag-of-cell error: invalid idx_view size" << idx_view.size ());
344
+ }
337
345
return info_.read_ref (idx_view.ubegin ());
338
346
}
339
347
@@ -343,8 +351,9 @@ class StaticBagOfCellsDbLazyImpl : public StaticBagOfCellsDb {
343
351
bool should_cache;
344
352
};
345
353
td::Result<CellLocation> get_cell_location (int idx) {
346
- CHECK (idx >= 0 );
347
- CHECK (idx < info_.cell_count );
354
+ if (idx < 0 || idx >= info_.cell_count ) {
355
+ return td::Status::Error (PSTRING () << " bag-of-cell error: invalid cell index " << idx);
356
+ }
348
357
TRY_STATUS (preload_index (idx));
349
358
TRY_RESULT (from, load_idx_offset (idx - 1 ));
350
359
TRY_RESULT (till, load_idx_offset (idx));
@@ -357,10 +366,15 @@ class StaticBagOfCellsDbLazyImpl : public StaticBagOfCellsDb {
357
366
res.should_cache = res.end % 2 == 1 ;
358
367
res.end /= 2 ;
359
368
}
360
- CHECK (std::numeric_limits<std::size_t >::max () - res.begin >= info_.data_offset );
361
- CHECK (std::numeric_limits<std::size_t >::max () - res.end >= info_.data_offset );
369
+ if (std::numeric_limits<std::size_t >::max () - res.begin < info_.data_offset ||
370
+ std::numeric_limits<std::size_t >::max () - res.end < info_.data_offset ) {
371
+ return td::Status::Error (PSTRING () << " bag-of-cell error: invalid cell location (1) " << res.begin << " :" << res.end );
372
+ }
362
373
res.begin += static_cast <std::size_t >(info_.data_offset );
363
374
res.end += static_cast <std::size_t >(info_.data_offset );
375
+ if (res.begin > res.end ) {
376
+ return td::Status::Error (PSTRING () << " bag-of-cell error: invalid cell location (2) " << res.begin << " :" << res.end );
377
+ }
364
378
return res;
365
379
}
366
380
@@ -396,8 +410,6 @@ class StaticBagOfCellsDbLazyImpl : public StaticBagOfCellsDb {
396
410
if (info_.has_index ) {
397
411
return td::Status::OK ();
398
412
}
399
-
400
- CHECK (idx < info_.cell_count );
401
413
if (index_i_.load (std::memory_order_relaxed) > idx) {
402
414
return td::Status::OK ();
403
415
}
@@ -407,12 +419,17 @@ class StaticBagOfCellsDbLazyImpl : public StaticBagOfCellsDb {
407
419
auto buf_slice = td::MutableSlice (buf.data (), buf.size ());
408
420
for (; index_i_ <= idx; index_i_++) {
409
421
auto offset = td::narrow_cast<size_t >(info_.data_offset + index_offset_);
410
- CHECK (data_.size () >= offset);
422
+ if (data_.size () < offset) {
423
+ return td::Status::Error (PSLICE () << " bag-of-cells error: invalid offset " << offset
424
+ << " (size=" << data_.size () << " )" );
425
+ }
411
426
TRY_RESULT (cell, data_.view (buf_slice.copy ().truncate (data_.size () - offset), offset));
412
427
CellSerializationInfo cell_info;
413
428
TRY_STATUS (cell_info.init (cell, info_.ref_byte_size ));
414
429
index_offset_ += cell_info.end_offset ;
415
- LOG_CHECK ((unsigned )info_.offset_byte_size <= 8 ) << info_.offset_byte_size ;
430
+ if ((unsigned )info_.offset_byte_size > 8 ) {
431
+ return td::Status::Error (PSTRING () << " bag-of-cell error: invalid offset_byte_size " << info_.offset_byte_size );
432
+ }
416
433
td::uint8 tmp[8 ];
417
434
info_.write_offset (tmp, index_offset_);
418
435
auto guard = index_data_rw_mutex_.lock_write ();
@@ -488,7 +505,10 @@ class StaticBagOfCellsDbLazyImpl : public StaticBagOfCellsDb {
488
505
bool should_cache) {
489
506
deserialize_cell_cnt_.add (1 );
490
507
Ref<Cell> refs[4 ];
491
- CHECK (cell_info.refs_cnt <= 4 );
508
+ if (cell_info.refs_cnt > 4 ) {
509
+ return td::Status::Error (PSLICE () << " invalid bag-of-cells cell #" << idx << " has " << cell_info.refs_cnt
510
+ << " refs" );
511
+ }
492
512
auto * ref_ptr = cell_slice.ubegin () + cell_info.refs_offset ;
493
513
for (int k = 0 ; k < cell_info.refs_cnt ; k++, ref_ptr += info_.ref_byte_size ) {
494
514
int ref_idx = td::narrow_cast<int >(info_.read_ref (ref_ptr));
0 commit comments