2018-03-10 11:22:10 +01:00
|
|
|
|
|
|
|
// Test library configuration for gtk.cfg
|
|
|
|
//
|
|
|
|
// Usage:
|
2023-03-09 20:04:20 +01:00
|
|
|
// $ cppcheck --check-library --library=gtk --enable=style,information --inconclusive --error-exitcode=1 --disable=missingInclude --inline-suppr --library=gtk test/cfg/gtk.cpp
|
2018-03-10 11:22:10 +01:00
|
|
|
// =>
|
|
|
|
// No warnings about bad library configuration, unmatched suppressions, etc. exitcode=0
|
|
|
|
//
|
|
|
|
|
2020-02-27 13:16:43 +01:00
|
|
|
#include <stdlib.h>
|
2018-03-10 11:22:10 +01:00
|
|
|
#include <gtk/gtk.h>
|
2019-02-18 16:48:46 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2018-03-10 11:22:10 +01:00
|
|
|
|
2020-02-26 10:33:23 +01:00
|
|
|
void validCode(int argInt, GHashTableIter * hash_table_iter, GHashTable * hash_table)
|
2018-03-10 11:22:10 +01:00
|
|
|
{
|
2020-02-26 10:33:23 +01:00
|
|
|
g_assert_cmpint(4 + 1, >=, 5);
|
|
|
|
g_assert_cmpstr("test", ==, "test");
|
|
|
|
|
2018-03-10 11:22:10 +01:00
|
|
|
// if G_UNLIKELY is not defined this results in a syntax error
|
2021-08-07 20:51:18 +02:00
|
|
|
if G_UNLIKELY(argInt == 1) {} else if (G_UNLIKELY(argInt == 2)) {}
|
2018-03-10 11:22:10 +01:00
|
|
|
|
2021-08-07 20:51:18 +02:00
|
|
|
if G_LIKELY(argInt == 0) {} else if (G_LIKELY(argInt == -1)) {}
|
2019-01-25 13:07:48 +01:00
|
|
|
|
|
|
|
printf("%s", _("test"));
|
|
|
|
printf("%s", Q_("a|test"));
|
|
|
|
printf("%s", N_("test"));
|
2019-01-28 13:47:46 +01:00
|
|
|
|
|
|
|
gpointer gpt = g_malloc(4);
|
|
|
|
printf("%p", gpt);
|
|
|
|
g_free(gpt);
|
2019-02-13 15:35:46 +01:00
|
|
|
g_assert(gpt);
|
|
|
|
if (!gpt) {
|
|
|
|
// cppcheck-suppress checkLibraryNoReturn
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
2019-02-15 08:44:21 +01:00
|
|
|
gpointer p = GINT_TO_POINTER(1);
|
|
|
|
int i = GPOINTER_TO_INT(p);
|
|
|
|
// cppcheck-suppress knownConditionTrueFalse
|
|
|
|
if (i == 1) {}
|
2019-02-18 16:48:46 +01:00
|
|
|
|
|
|
|
g_print("test");
|
|
|
|
g_print("%d", 1);
|
|
|
|
g_printerr("err");
|
2019-04-01 15:33:27 +02:00
|
|
|
|
|
|
|
GString * pGStr1 = g_string_new("test");
|
|
|
|
g_string_append(pGStr1, "a");
|
|
|
|
g_string_free(pGStr1, TRUE);
|
2019-04-01 16:15:32 +02:00
|
|
|
|
|
|
|
gchar * pGchar1 = g_strconcat("a", "b", NULL);
|
|
|
|
printf("%s", pGchar1);
|
|
|
|
g_free(pGchar1);
|
2019-06-25 19:19:10 +02:00
|
|
|
|
2023-05-21 14:01:14 +02:00
|
|
|
// cppcheck-suppress unusedAllocatedMemory
|
2019-06-25 19:19:10 +02:00
|
|
|
GError * pGerror = g_error_new(1, -2, "a %d", 1);
|
|
|
|
g_error_free(pGerror);
|
2019-08-20 15:00:30 +02:00
|
|
|
|
|
|
|
static gsize init_val = 0;
|
|
|
|
if (g_once_init_enter(&init_val)) {
|
|
|
|
gsize result_val = 1;
|
|
|
|
g_once_init_leave(&init_val, result_val);
|
|
|
|
}
|
2020-02-26 10:33:23 +01:00
|
|
|
|
|
|
|
g_hash_table_iter_replace(hash_table_iter, g_strdup("test"));
|
|
|
|
g_hash_table_insert(hash_table, g_strdup("key"), g_strdup("value"));
|
|
|
|
g_hash_table_replace(hash_table, g_strdup("key"), g_strdup("value"));
|
2021-05-17 05:35:16 +02:00
|
|
|
|
|
|
|
// NULL is handled graciously
|
|
|
|
char* str = g_strdup(NULL);
|
2021-05-17 05:59:28 +02:00
|
|
|
if (g_strcmp0(str, NULL) || g_strcmp0(NULL, str))
|
2021-05-17 05:35:16 +02:00
|
|
|
printf("%s", str);
|
|
|
|
g_free(str);
|
2019-01-28 13:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void g_malloc_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_malloc(8);
|
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
gconstpointer gpt = g_malloc(1);
|
2019-07-15 14:51:58 +02:00
|
|
|
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_malloc0_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_malloc0(8);
|
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
gconstpointer gpt = g_malloc0(1);
|
2019-07-15 14:51:58 +02:00
|
|
|
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_malloc_n_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_malloc_n(8, 1);
|
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
gconstpointer gpt = g_malloc_n(1, 2);
|
2019-07-15 14:51:58 +02:00
|
|
|
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_malloc0_n_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_malloc0_n(8, 1);
|
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
gconstpointer gpt = g_malloc0_n(1, 2);
|
2019-07-15 14:51:58 +02:00
|
|
|
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_try_malloc_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_try_malloc(8);
|
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
gconstpointer gpt = g_try_malloc(1);
|
2019-07-15 14:51:58 +02:00
|
|
|
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_try_malloc0_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_try_malloc0(8);
|
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
gconstpointer gpt = g_try_malloc0(1);
|
2019-07-15 14:51:58 +02:00
|
|
|
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_try_malloc_n_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_try_malloc_n(8, 1);
|
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
gconstpointer gpt = g_try_malloc_n(1, 2);
|
2019-07-15 14:51:58 +02:00
|
|
|
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_try_malloc0_n_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_try_malloc0_n(8, 1);
|
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
gconstpointer gpt = g_try_malloc0_n(1, 2);
|
2019-07-15 14:51:58 +02:00
|
|
|
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_realloc_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
2019-07-22 10:37:36 +02:00
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
2019-07-15 14:51:58 +02:00
|
|
|
g_realloc(NULL, 1);
|
|
|
|
|
|
|
|
gpointer gpt = g_malloc(1);
|
|
|
|
gpt = g_realloc(gpt, 2); // No memleakOnRealloc since g_realloc aborts if it fails
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_realloc_n_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
2019-07-22 10:37:36 +02:00
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
2019-07-15 14:51:58 +02:00
|
|
|
g_realloc_n(NULL, 1, 2);
|
|
|
|
|
|
|
|
gpointer gpt = g_malloc_n(1, 2);
|
|
|
|
gpt = g_realloc_n(gpt, 2, 3); // No memleakOnRealloc since g_realloc_n aborts if it fails
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_try_realloc_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
2019-07-22 10:37:36 +02:00
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
2019-07-15 14:51:58 +02:00
|
|
|
g_try_realloc(NULL, 1);
|
|
|
|
|
|
|
|
gpointer gpt = g_try_malloc(1);
|
|
|
|
// cppcheck-suppress memleakOnRealloc
|
|
|
|
gpt = g_try_realloc(gpt, 2);
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_try_realloc_n_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
2019-07-22 10:37:36 +02:00
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
2019-07-15 14:51:58 +02:00
|
|
|
g_try_realloc_n(NULL, 1, 2);
|
|
|
|
|
|
|
|
gpointer gpt = g_try_malloc_n(1, 2);
|
2019-07-22 10:37:36 +02:00
|
|
|
// cppcheck-suppress memleakOnRealloc
|
2019-07-15 14:51:58 +02:00
|
|
|
gpt = g_try_realloc_n(gpt, 2, 3);
|
2019-01-28 13:47:46 +01:00
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
2018-03-10 11:22:10 +01:00
|
|
|
}
|
2019-02-13 15:35:46 +01:00
|
|
|
|
|
|
|
void g_assert_test()
|
|
|
|
{
|
|
|
|
int a;
|
|
|
|
// cppcheck-suppress checkLibraryNoReturn
|
|
|
|
// cppcheck-suppress assignmentInAssert
|
|
|
|
g_assert(a = 5);
|
|
|
|
}
|
2019-02-18 16:48:46 +01:00
|
|
|
|
|
|
|
void g_print_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress invalidPrintfArgType_uint
|
|
|
|
g_print("%u", -1);
|
|
|
|
// cppcheck-suppress invalidPrintfArgType_uint
|
|
|
|
g_printerr("%x", "a");
|
|
|
|
}
|
2019-02-28 15:29:37 +01:00
|
|
|
|
|
|
|
void g_alloca_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress allocaCalled
|
|
|
|
char * pBuf1 = g_alloca(5);
|
|
|
|
pBuf1[0] = '\0';
|
|
|
|
}
|
2019-03-26 10:45:06 +01:00
|
|
|
|
|
|
|
void g_new_test()
|
|
|
|
{
|
|
|
|
struct a {
|
|
|
|
int b;
|
|
|
|
};
|
|
|
|
// valid
|
|
|
|
struct a * pNew1 = g_new(struct a, 5);
|
|
|
|
printf("%p", pNew1);
|
|
|
|
g_free(pNew1);
|
|
|
|
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_new(struct a, 1);
|
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
const struct a * pNew2 = g_new(struct a, 2);
|
2019-03-26 10:45:06 +01:00
|
|
|
printf("%p", pNew2);
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
2019-07-03 08:39:44 +02:00
|
|
|
void g_new_if_test()
|
|
|
|
{
|
|
|
|
struct a {
|
|
|
|
int b;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct a * pNew3;
|
|
|
|
if (pNew3 = g_new(struct a, 6)) {
|
|
|
|
printf("%p", pNew3);
|
|
|
|
}
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
2019-07-15 14:51:58 +02:00
|
|
|
void g_new0_test()
|
|
|
|
{
|
|
|
|
struct a {
|
|
|
|
int b;
|
|
|
|
};
|
|
|
|
// valid
|
|
|
|
struct a * pNew1 = g_new0(struct a, 5);
|
|
|
|
printf("%p", pNew1);
|
|
|
|
g_free(pNew1);
|
|
|
|
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_new0(struct a, 1);
|
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
const struct a * pNew2 = g_new0(struct a, 2);
|
2019-07-15 14:51:58 +02:00
|
|
|
printf("%p", pNew2);
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_try_new_test()
|
|
|
|
{
|
|
|
|
struct a {
|
|
|
|
int b;
|
|
|
|
};
|
|
|
|
// valid
|
|
|
|
struct a * pNew1 = g_try_new(struct a, 5);
|
|
|
|
printf("%p", pNew1);
|
|
|
|
g_free(pNew1);
|
|
|
|
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_try_new(struct a, 1);
|
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
const struct a * pNew2 = g_try_new(struct a, 2);
|
2019-07-15 14:51:58 +02:00
|
|
|
printf("%p", pNew2);
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
2019-03-26 10:45:06 +01:00
|
|
|
void g_try_new0_test()
|
|
|
|
{
|
|
|
|
struct a {
|
|
|
|
int b;
|
|
|
|
};
|
|
|
|
// valid
|
|
|
|
struct a * pNew1 = g_try_new0(struct a, 5);
|
|
|
|
printf("%p", pNew1);
|
|
|
|
g_free(pNew1);
|
|
|
|
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_try_new0(struct a, 1);
|
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
const struct a * pNew2 = g_try_new0(struct a, 2);
|
2019-03-26 10:45:06 +01:00
|
|
|
printf("%p", pNew2);
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
2019-06-25 19:19:10 +02:00
|
|
|
|
2019-07-15 14:51:58 +02:00
|
|
|
void g_renew_test()
|
|
|
|
{
|
|
|
|
struct a {
|
|
|
|
int b;
|
|
|
|
};
|
2019-07-22 10:37:36 +02:00
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
2019-07-15 14:51:58 +02:00
|
|
|
g_renew(struct a, NULL, 1);
|
|
|
|
|
|
|
|
struct a * pNew = g_new(struct a, 1);
|
|
|
|
pNew = g_renew(struct a, pNew, 2); // No memleakOnRealloc since g_renew aborts if it fails
|
|
|
|
printf("%p", pNew);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_try_renew_test()
|
|
|
|
{
|
|
|
|
struct a {
|
|
|
|
int b;
|
|
|
|
};
|
2019-07-22 10:37:36 +02:00
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
2019-07-15 14:51:58 +02:00
|
|
|
g_try_renew(struct a, NULL, 1);
|
|
|
|
|
|
|
|
struct a * pNew = g_try_new(struct a, 1);
|
2019-07-22 10:37:36 +02:00
|
|
|
// cppcheck-suppress memleakOnRealloc
|
2019-07-15 14:51:58 +02:00
|
|
|
pNew = g_try_renew(struct a, pNew, 2);
|
|
|
|
printf("%p", pNew);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
2019-06-25 19:19:10 +02:00
|
|
|
void g_error_new_test()
|
|
|
|
{
|
|
|
|
// valid
|
|
|
|
GError * pNew1 = g_error_new(1, -2, "a %d", 1);
|
|
|
|
printf("%p", pNew1);
|
|
|
|
g_error_free(pNew1);
|
|
|
|
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_error_new(1, -2, "a %d", 1);
|
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
const GError * pNew2 = g_error_new(1, -2, "a %d", 1);
|
2019-06-25 19:19:10 +02:00
|
|
|
printf("%p", pNew2);
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
2019-08-20 15:00:30 +02:00
|
|
|
|
|
|
|
void g_once_init_enter_leave_test()
|
|
|
|
{
|
|
|
|
static gsize init_val;
|
|
|
|
if (g_once_init_enter(&init_val)) {
|
|
|
|
gsize result_val = 0;
|
|
|
|
// cppcheck-suppress invalidFunctionArg
|
|
|
|
g_once_init_leave(&init_val, result_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
gsize init_val2;
|
2021-05-22 07:39:27 +02:00
|
|
|
// cppcheck-suppress uninitvar
|
2019-08-20 15:00:30 +02:00
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
g_once_init_enter(&init_val2);
|
|
|
|
|
|
|
|
gsize * init_val3 = NULL;
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
if (g_once_init_enter(init_val3)) {
|
2021-08-21 18:52:11 +02:00
|
|
|
gsize* init_val31 = NULL;
|
2019-08-20 15:00:30 +02:00
|
|
|
// cppcheck-suppress nullPointer
|
2021-08-21 18:52:11 +02:00
|
|
|
g_once_init_leave(init_val31, 1);
|
2019-08-20 15:00:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gsize * init_val4;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
if (g_once_init_enter(init_val4)) {
|
2021-10-03 22:01:13 +02:00
|
|
|
gsize * init_val5;
|
2019-08-20 15:00:30 +02:00
|
|
|
// cppcheck-suppress uninitvar
|
2021-10-03 22:01:13 +02:00
|
|
|
g_once_init_leave(init_val5, 1);
|
2019-08-20 15:00:30 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-08 10:01:17 +02:00
|
|
|
|
|
|
|
void g_strchug_g_strchomp_test(gchar * str1)
|
|
|
|
{
|
|
|
|
g_strchug(str1);
|
|
|
|
g_strchomp(str1);
|
|
|
|
g_strchug(g_strchomp(str1));
|
|
|
|
gchar * str2;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
g_strchug(str2);
|
|
|
|
gchar * str3;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
g_strchomp(str3);
|
|
|
|
}
|
2020-02-27 13:16:43 +01:00
|
|
|
|
|
|
|
void g_abort_test()
|
|
|
|
{
|
|
|
|
g_abort();
|
|
|
|
//cppcheck-suppress unreachableCode
|
|
|
|
printf("Never reached");
|
|
|
|
}
|