From d1ff29b3988a7c0aed3c0394ccfb3904f0588ddf Mon Sep 17 00:00:00 2001
From: Philipp Storz <philipp.storz@bareos.com>
Date: Tue, 25 Apr 2017 10:50:44 +0200
Subject: [PATCH] Don't return empty volname if volume is on unwanted vols list

When the volume was on the unwanted_list, the loop was
continued but the number of sql rows was not checked, so that
the code continued and returned an empty volumename.

Fixes #691: Backup job fails with "No Volume name given."
---
 src/cats/sql_find.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/cats/sql_find.c b/src/cats/sql_find.c
index ef716b820..c354c8a98 100644
--- a/src/cats/sql_find.c
+++ b/src/cats/sql_find.c
@@ -435,9 +435,18 @@ int db_find_next_volume(JCR *jcr, B_DB *mdb, int item, bool InChanger, MEDIA_DBR
       }
 
       /*
-       * See if this is not on the unwanted volumes list.
+       * Skip if volume is on unwanted volumes list.
+       * We need to reduce the number of rows by one
+       * and jump out if no more rows are available
        */
       if (unwanted_volumes && is_on_unwanted_volumes_list(row[1], unwanted_volumes)) {
+         Dmsg1(50, "Volume %s is on unwanted_volume_list, skipping\n", row[1]);
+         num_rows-- ;
+         if ( num_rows <= 0) {
+            Dmsg1(50, "No more volumes in result, bailing out\n", row[1]);
+            sql_free_result(mdb);
+            goto bail_out;
+         }
          continue;
       }
 
