fp-device: Return error if trying to list a storage-less device

Devices with no storage don't allow listing prints, and if we try to do
that, we'd end up in trying to call a NULL function pointer, causing a crash

So always check if the device has storage before calling the list vfunc, and
if we fail, return an error.

Include an unit-test to verify this situation
This commit is contained in:
Marco Trevisan (Treviño) 2020-02-08 13:38:18 +01:00 committed by Benjamin Berg
parent 15a90eb451
commit 355cae1bbd
2 changed files with 27 additions and 0 deletions

View file

@ -1213,6 +1213,14 @@ fp_device_list_prints (FpDevice *device,
return; return;
} }
if (!fp_device_has_storage (device))
{
g_task_return_error (task,
fpi_device_error_new_msg (FP_DEVICE_ERROR_NOT_SUPPORTED,
"Device has no storage"));
return;
}
priv->current_action = FPI_DEVICE_ACTION_LIST; priv->current_action = FPI_DEVICE_ACTION_LIST;
priv->current_task = g_steal_pointer (&task); priv->current_task = g_steal_pointer (&task);
maybe_cancel_on_cancelled (device, cancellable); maybe_cancel_on_cancelled (device, cancellable);

View file

@ -1527,6 +1527,24 @@ test_driver_list_error (void)
g_assert_null (prints); g_assert_null (prints);
} }
static void
test_driver_list_no_storage (void)
{
g_autoptr(FpAutoResetClass) dev_class = auto_reset_device_class ();
g_autoptr(FpAutoCloseDevice) device = NULL;
g_autoptr(GPtrArray) prints = NULL;
g_autoptr(GError) error = NULL;
dev_class->list = NULL;
device = auto_close_fake_device_new ();
g_assert_false (fp_device_has_storage (device));
prints = fp_device_list_prints_sync (device, NULL, &error);
g_assert_null (prints);
g_assert_error (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_NOT_SUPPORTED);
}
static void static void
test_driver_delete (void) test_driver_delete (void)
{ {
@ -2210,6 +2228,7 @@ main (int argc, char *argv[])
g_test_add_func ("/driver/capture/error", test_driver_capture_error); g_test_add_func ("/driver/capture/error", test_driver_capture_error);
g_test_add_func ("/driver/list", test_driver_list); g_test_add_func ("/driver/list", test_driver_list);
g_test_add_func ("/driver/list/error", test_driver_list_error); g_test_add_func ("/driver/list/error", test_driver_list_error);
g_test_add_func ("/driver/list/no_storage", test_driver_list_no_storage);
g_test_add_func ("/driver/delete", test_driver_delete); g_test_add_func ("/driver/delete", test_driver_delete);
g_test_add_func ("/driver/delete/error", test_driver_delete_error); g_test_add_func ("/driver/delete/error", test_driver_delete_error);
g_test_add_func ("/driver/cancel", test_driver_cancel); g_test_add_func ("/driver/cancel", test_driver_cancel);