2018-03-10 11:22:10 +01:00
|
|
|
|
|
|
|
// Test library configuration for gtk.cfg
|
|
|
|
//
|
|
|
|
// Usage:
|
|
|
|
// $ cppcheck --check-library --enable=information --inconclusive --error-exitcode=1 --suppress=missingIncludeSystem --inline-suppr --library=gtk test/cfg/gtk.cpp
|
|
|
|
// =>
|
|
|
|
// 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
|
|
|
|
if G_UNLIKELY(argInt == 1) {
|
|
|
|
} else if (G_UNLIKELY(argInt == 2)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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"));
|
2019-01-28 13:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void g_malloc_test()
|
|
|
|
{
|
2019-07-15 14:51:58 +02:00
|
|
|
// cppcheck-suppress ignoredReturnValue
|
2019-01-28 13:47:46 +01:00
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_malloc(8);
|
|
|
|
|
|
|
|
gpointer gpt = g_malloc(1);
|
2019-07-15 14:51:58 +02:00
|
|
|
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_malloc0_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_malloc0(8);
|
|
|
|
|
|
|
|
gpointer gpt = g_malloc0(1);
|
|
|
|
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_malloc_n_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_malloc_n(8, 1);
|
|
|
|
|
|
|
|
gpointer gpt = g_malloc_n(1, 2);
|
|
|
|
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_malloc0_n_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_malloc0_n(8, 1);
|
|
|
|
|
|
|
|
gpointer gpt = g_malloc0_n(1, 2);
|
|
|
|
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_try_malloc_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_try_malloc(8);
|
|
|
|
|
|
|
|
gpointer gpt = g_try_malloc(1);
|
|
|
|
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_try_malloc0_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_try_malloc0(8);
|
|
|
|
|
|
|
|
gpointer gpt = g_try_malloc0(1);
|
|
|
|
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_try_malloc_n_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_try_malloc_n(8, 1);
|
|
|
|
|
|
|
|
gpointer gpt = g_try_malloc_n(1, 2);
|
|
|
|
|
|
|
|
printf("%p", gpt);
|
|
|
|
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_try_malloc0_n_test()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_try_malloc0_n(8, 1);
|
|
|
|
|
|
|
|
gpointer gpt = g_try_malloc0_n(1, 2);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
struct a * pNew2 = g_new(struct a, 2);
|
|
|
|
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);
|
|
|
|
|
|
|
|
struct a * pNew2 = g_new0(struct a, 2);
|
|
|
|
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);
|
|
|
|
|
|
|
|
struct a * pNew2 = g_try_new(struct a, 2);
|
|
|
|
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);
|
|
|
|
|
|
|
|
struct a * pNew2 = g_try_new0(struct a, 2);
|
|
|
|
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 ignoredReturnValue
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
g_error_new(1, -2, "a %d", 1);
|
|
|
|
|
|
|
|
GError * pNew2 = g_error_new(1, -2, "a %d", 1);
|
|
|
|
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;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
g_once_init_enter(&init_val2);
|
|
|
|
|
|
|
|
gsize * init_val3 = NULL;
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
if (g_once_init_enter(init_val3)) {
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
g_once_init_leave(init_val3, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
gsize * init_val4;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
if (g_once_init_enter(init_val4)) {
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
g_once_init_leave(init_val4, 1);
|
|
|
|
}
|
|
|
|
}
|
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");
|
|
|
|
}
|