From 9f54c23995d6f59404eb7ff9e57683d96d3daa77 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sun, 23 Mar 2014 14:45:47 -0400 Subject: [PATCH] database: don't iterate if there are no entries Checking the valid bit could lead to a segmentation fault. Signed-off-by: Anna Schumaker --- include/database.hpp | 3 +++ tests/database | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/database.hpp b/include/database.hpp index 60217352..a8800128 100644 --- a/include/database.hpp +++ b/include/database.hpp @@ -119,6 +119,9 @@ unsigned int Database :: actual_size() template typename Database::iterator Database :: begin() { + if (size() == 0) + return end(); + iterator it = _db.begin(); if ( (*it).valid == true ) return it; diff --git a/tests/database b/tests/database index 35830ee5..2dfac707 100755 --- a/tests/database +++ b/tests/database @@ -7,7 +7,7 @@ function test_autosave { new_test "Database Test (n = $1, autosave = true)" src/database.run -a $1 - if [ ! -f $DATA_DIR/database.db ]; then + if [ ! -f $DATA_DIR/database.db ] && [ $1 != 0 ]; then echo "ERROR: $DATA_DIR/database.db doesn't exist!" exit 1 fi @@ -34,6 +34,8 @@ function run_test fi } +run_test 0 +echo run_test 10 echo run_test 100