Skip to content

Commit

Permalink
libdnf: switch from g_error_free() to g_clear_error() in tests
Browse files Browse the repository at this point in the history
Use g_clear_error() so the pointer is also reset to NULL; this avoids
potential wrong non-null checks and double frees.

Card ID: CCT-368
  • Loading branch information
ptoscano committed Feb 22, 2024
1 parent 7bf9c26 commit 1eaefef
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/plugins/libdnf/test-productdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void testReadMissingFile(dbFixture *fixture, gconstpointer ignored) {
GError *err = NULL;
readProductDb(db, &err);
g_assert_nonnull(err);
g_error_free(err);
g_clear_error(&err);
}

void testReadFile(dbFixture *fixture, gconstpointer ignored) {
Expand Down Expand Up @@ -151,7 +151,7 @@ void testReadFile(dbFixture *fixture, gconstpointer ignored) {
g_object_unref(ioStream);
g_object_unref(testJsonFile);
if(err != NULL) {
g_error_free(err);
g_clear_error(&err);
}
}

Expand All @@ -178,7 +178,7 @@ void testReadCorruptedFile(dbFixture *fixture, gconstpointer ignored) {
readProductDb(db, &err);

g_assert_nonnull(err);
g_error_free(err);
g_clear_error(&err);
g_assert_cmpint(g_hash_table_size(db->repoMap), ==, 0);
}

Expand All @@ -204,23 +204,23 @@ void testReadFileWrongData(dbFixture *fixture, gconstpointer ignored) {
g_output_stream_write_all(outStream, testJson01, strlen(testJson01), NULL, NULL, &err);
readProductDb(db, &err);
g_assert_nonnull(err);
g_error_free(err);
g_clear_error(&err);

// Key is not string, but it is integer
gchar *testJson02 = "{69: ['rhel']}\n";
outStream = g_io_stream_get_output_stream((GIOStream*) ioStream);
g_output_stream_write_all(outStream, testJson02, strlen(testJson02), NULL, NULL, &err);
readProductDb(db, &err);
g_assert_nonnull(err);
g_error_free(err);
g_clear_error(&err);

// Value in array is not string, but it is integer
gchar *testJson03 = "{'69': [100]}\n";
outStream = g_io_stream_get_output_stream((GIOStream*) ioStream);
g_output_stream_write_all(outStream, testJson03, strlen(testJson03), NULL, NULL, &err);
readProductDb(db, &err);
g_assert_nonnull(err);
g_error_free(err);
g_clear_error(&err);

g_io_stream_close((GIOStream*) ioStream, NULL, &err);
}
Expand Down

0 comments on commit 1eaefef

Please sign in to comment.