[*_dump] Fix segfault when given a tiny metadata file

This commit is contained in:
Joe Thornber 2017-09-21 10:22:38 +01:00
parent 7079b1ec9e
commit 48e7ab89a5
6 changed files with 37 additions and 3 deletions

View File

@ -13,7 +13,11 @@ namespace {
// FIXME: duplication
transaction_manager::ptr
open_tm(block_manager<>::ptr bm) {
space_map::ptr sm(new core_map(bm->get_nr_blocks()));
auto nr_blocks = bm->get_nr_blocks();
if (!nr_blocks)
throw runtime_error("Metadata is not large enough for superblock.");
space_map::ptr sm(new core_map(nr_blocks));
sm->inc(SUPERBLOCK_LOCATION);
transaction_manager::ptr tm(new transaction_manager(bm, sm));
return tm;

View File

@ -11,7 +11,11 @@ namespace {
// FIXME: duplication
transaction_manager::ptr
open_tm(block_manager<>::ptr bm) {
space_map::ptr sm(new core_map(bm->get_nr_blocks()));
auto nr_blocks = bm->get_nr_blocks();
if (!nr_blocks)
throw runtime_error("Metadata is not large enough for superblock.");
space_map::ptr sm(new core_map(nr_blocks));
sm->inc(SUPERBLOCK_LOCATION);
transaction_manager::ptr tm(new transaction_manager(bm, sm));
return tm;

View File

@ -245,6 +245,11 @@
(receive (stdout stderr) (run-fail "cache_dump")
(assert-starts-with "No input file provided." stderr)))
(define-scenario (cache-dump small-input-file)
"Fails with small input file"
(with-temp-file-sized ((md "cache.bin" 512))
(run-fail "cache_dump" md)))
(define-scenario (cache-dump restore-is-noop)
"cache_dump followed by cache_restore is a noop."
(with-valid-metadata (md)

View File

@ -180,6 +180,14 @@
(assert-eof stdout)
(assert-starts-with "Couldn't stat file" stderr)))))
;;;-----------------------------------------------------------
;;; era_dump scenarios
;;;-----------------------------------------------------------
(define-scenario (era-dump small-input-file)
"Fails with small input file"
(with-temp-file-sized ((md "era.bin" 512))
(run-fail "era_dump" md)))
(define-scenario (era-dump restore-is-noop)
"era_dump followed by era_restore is a noop."
(with-valid-metadata (md)

View File

@ -187,6 +187,15 @@
(receive (stdout _) (thin-restore "-i" xml "-o" md "--quiet")
(assert-eof stdout)))))
;;;-----------------------------------------------------------
;;; thin_dump scenarios
;;;-----------------------------------------------------------
(define-scenario (thin-dump small-input-file)
"Fails with small input file"
(with-temp-file-sized ((md "thin.bin" 512))
(run-fail "thin_dump" md)))
(define-scenario (thin-dump restore-is-noop)
"thin_dump followed by thin_restore is a noop."
(with-valid-metadata (md)

View File

@ -42,7 +42,11 @@ namespace {
transaction_manager::ptr
open_tm(block_manager<>::ptr bm) {
space_map::ptr sm(new core_map(bm->get_nr_blocks()));
auto nr_blocks = bm->get_nr_blocks();
if (!nr_blocks)
throw runtime_error("Metadata is not large enough for superblock.");
space_map::ptr sm(new core_map(nr_blocks));
sm->inc(SUPERBLOCK_LOCATION);
transaction_manager::ptr tm(new transaction_manager(bm, sm));
return tm;