diff --git a/bob/learn/libsvm/file.cpp b/bob/learn/libsvm/file.cpp
index 09ad465ad84bfb0a47e11392185aeb2ff0003c0c..a6720d2832551f8da61707f991cd42303dca57a4 100644
--- a/bob/learn/libsvm/file.cpp
+++ b/bob/learn/libsvm/file.cpp
@@ -488,8 +488,10 @@ static PyObject* PyBobLearnLibsvmFile_read_all
     auto bzval = PyBlitzArrayCxx_AsBlitz<double,2>(values);
     blitz::Range all = blitz::Range::all();
     int k = 0;
-    while (self->cxx->good()) {
+    
+    while ((self->cxx->good()) && ((size_t)k < self->cxx->samples())) {
       blitz::Array<double,1> v_ = (*bzval)(k, all);
+      
       int label = 0;
       bool ok = self->cxx->read_(label, v_);
       if (ok) (*bzlab)(k) = label;
diff --git a/bob/learn/libsvm/test_machine.py b/bob/learn/libsvm/test_machine.py
index 001610e15279b188a7b1df06721529330d238cb2..c05d912647da2bed404b2eb94a2dcf25ddaf3de6 100644
--- a/bob/learn/libsvm/test_machine.py
+++ b/bob/learn/libsvm/test_machine.py
@@ -131,6 +131,7 @@ def test_data_loading():
   nose.tools.eq_(data.fail(), False)
   nose.tools.eq_(data.eof(), False)
 
+
   #tries loading the data, one by one
   all_data = []
   all_labels = []
@@ -153,13 +154,18 @@ def test_data_loading():
     counter += 1
     entry = data.read()
 
+
   #tries loading the file all in a single shot
   data.reset()
   labels, data = data.read_all()
+
   assert numpy.array_equal(labels, all_labels)
   for k, l in zip(data, all_data):
     assert numpy.array_equal(k, l)
 
+
+
+
   #makes sure the first 3 examples are correctly read
   ex = []
   ex.append(numpy.array([0.708333 , 1, 1, -0.320755 , -0.105023 , -1, 1,
@@ -169,10 +175,12 @@ def test_data_loading():
   ex.append(numpy.array([0.166667, 1, -0.333333, -0.433962, -0.383562, -1,
     -1, 0.0687023, -1, -0.903226, -1, -1, 1], 'float64'))
   ls = [+1, -1, +1]
+
   for k, (l, e) in enumerate(zip(ls, ex)):
     nose.tools.eq_( l, labels[k] )
     assert numpy.array_equal(e, data[k])
 
+
 @nose.tools.raises(RuntimeError)
 def test_raises():