database: don't iterate if there are no entries

Checking the valid bit could lead to a segmentation fault.

Signed-off-by: Anna Schumaker <anna@ocarinaproject.net>
This commit is contained in:
Anna Schumaker 2014-03-23 14:45:47 -04:00 committed by Anna Schumaker
parent b75d13838f
commit 9f54c23995
2 changed files with 6 additions and 1 deletions

View File

@ -119,6 +119,9 @@ unsigned int Database<T> :: actual_size()
template <class T>
typename Database<T>::iterator Database<T> :: begin()
{
if (size() == 0)
return end();
iterator it = _db.begin();
if ( (*it).valid == true )
return it;

View File

@ -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