From 843585e00de143f2176cf1ad299f0554bd5fea1c Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Tue, 23 Feb 2021 15:30:12 -0800 Subject: [PATCH] cursor: handle NULL readers gracefully Fixes #55 --- .../sysprof-capture-cursor.c | 22 +++++++++++-------- src/tests/test-capture-cursor.c | 11 ++++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/libsysprof-capture/sysprof-capture-cursor.c b/src/libsysprof-capture/sysprof-capture-cursor.c index cf11bcd5..6d4c83a5 100644 --- a/src/libsysprof-capture/sysprof-capture-cursor.c +++ b/src/libsysprof-capture/sysprof-capture-cursor.c @@ -151,9 +151,11 @@ sysprof_capture_cursor_foreach (SysprofCaptureCursor *self, void *user_data) { assert (self != NULL); - assert (self->reader != NULL); assert (callback != NULL); + if (self->reader == NULL) + return; + for (;;) { const SysprofCaptureFrame *frame; @@ -260,9 +262,9 @@ void sysprof_capture_cursor_reset (SysprofCaptureCursor *self) { assert (self != NULL); - assert (self->reader != NULL); - sysprof_capture_reader_reset (self->reader); + if (self->reader != NULL) + sysprof_capture_reader_reset (self->reader); } void @@ -305,18 +307,20 @@ sysprof_capture_cursor_add_condition (SysprofCaptureCursor *self, * sysprof_capture_cursor_new: * @self: a #SysprofCaptureCursor * - * Returns: (transfer full): a new cursor for @reader + * Returns: (transfer full) (nullable): a new cursor for @reader */ SysprofCaptureCursor * sysprof_capture_cursor_new (SysprofCaptureReader *reader) { SysprofCaptureCursor *self; - assert (reader != NULL); - self = sysprof_capture_cursor_init (); - self->reader = sysprof_capture_reader_copy (reader); - sysprof_capture_reader_reset (self->reader); + + if (reader != NULL) + { + self->reader = sysprof_capture_reader_copy (reader); + sysprof_capture_reader_reset (self->reader); + } return self; } @@ -326,7 +330,7 @@ sysprof_capture_cursor_new (SysprofCaptureReader *reader) * * Gets the underlying reader that is used by the cursor. * - * Returns: (transfer none): An #SysprofCaptureReader + * Returns: (transfer none) (nullable): An #SysprofCaptureReader */ SysprofCaptureReader * sysprof_capture_cursor_get_reader (SysprofCaptureCursor *self) diff --git a/src/tests/test-capture-cursor.c b/src/tests/test-capture-cursor.c index 9dc2e6c6..1549d712 100644 --- a/src/tests/test-capture-cursor.c +++ b/src/tests/test-capture-cursor.c @@ -69,6 +69,16 @@ test_cursor_basic (void) g_unlink ("capture-cursor-file"); } +static void +test_cursor_null (void) +{ + SysprofCaptureCursor *cursor = sysprof_capture_cursor_new (NULL); + gint count = 0; + sysprof_capture_cursor_foreach (cursor, increment, &count); + g_assert_cmpint (count, ==, 0); + g_clear_pointer (&cursor, sysprof_capture_cursor_unref); +} + int main (int argc, char *argv[]) @@ -76,5 +86,6 @@ main (int argc, sysprof_clock_init (); g_test_init (&argc, &argv, NULL); g_test_add_func ("/SysprofCaptureCursor/basic", test_cursor_basic); + g_test_add_func ("/SysprofCaptureCursor/null", test_cursor_null); return g_test_run (); }