Skip to content

Commit d9ce1d4

Browse files
committed
Refactor: various: Replace const gchar with const char
...and replace gchar with char in two static buffers, since they don't need to be freed. The GLib documentation says the standard char should be preferred in new code. The two are equivalent (via typedef). We could make an argument for keeping gchar in non-const cases as a reminder to use g_free(). So those replacements can be done in a later commit, if at all. https://docs.gtk.org/glib/types.html#gchar Signed-off-by: Reid Wahl <nrwahl@protonmail.com>
1 parent 46f2b64 commit d9ce1d4

10 files changed

Lines changed: 71 additions & 51 deletions

File tree

lib/common/strings.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,8 +724,8 @@ pcmk__parse_ll_range(const char *text, long long *start, long long *end)
724724
long long local_end = 0;
725725
gchar **split = NULL;
726726
unsigned int length = 0;
727-
const gchar *start_s = NULL;
728-
const gchar *end_s = NULL;
727+
const char *start_s = NULL;
728+
const char *end_s = NULL;
729729

730730
// Do not free
731731
char *remainder = NULL;

lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 the Pacemaker project contributors
2+
* Copyright 2020-2026 the Pacemaker project contributors
33
*
44
* The version control history for this file may have further details.
55
*
@@ -14,8 +14,10 @@
1414
#include <glib.h>
1515
#include <stdint.h>
1616

17+
// @COMPAT Consider replacing with g_strv_equal() when we require GLib 2.60
1718
#define LISTS_EQ(a, b) { \
18-
assert_int_equal(g_strv_length((gchar **) (a)), g_strv_length((gchar **) (b))); \
19+
assert_int_equal(g_strv_length((char **) (a)), \
20+
g_strv_length((char **) (b))); \
1921
for (int i = 0; i < g_strv_length((a)); i++) { \
2022
assert_string_equal((a)[i], (b)[i]); \
2123
} \
@@ -27,9 +29,10 @@ empty_input(void **state) {
2729
}
2830

2931
static void
30-
no_specials(void **state) {
32+
no_specials(void **state)
33+
{
3134
const char *argv[] = { "crm_mon", "-a", "-b", "-c", "-d", "-1", NULL };
32-
const gchar *expected[] = { "crm_mon", "-a", "-b", "-c", "-d", "-1", NULL };
35+
const char *expected[] = { "crm_mon", "-a", "-b", "-c", "-d", "-1", NULL };
3336

3437
gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
3538
LISTS_EQ(processed, expected);
@@ -41,109 +44,124 @@ no_specials(void **state) {
4144
}
4245

4346
static void
44-
single_dash(void **state) {
47+
single_dash(void **state)
48+
{
4549
const char *argv[] = { "crm_mon", "-", NULL };
46-
const gchar *expected[] = { "crm_mon", "-", NULL };
50+
const char *expected[] = { "crm_mon", "-", NULL };
4751

4852
gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
4953
LISTS_EQ(processed, expected);
5054
g_strfreev(processed);
5155
}
5256

5357
static void
54-
double_dash(void **state) {
58+
double_dash(void **state)
59+
{
5560
const char *argv[] = { "crm_mon", "-a", "--", "-bc", NULL };
56-
const gchar *expected[] = { "crm_mon", "-a", "--", "-bc", NULL };
61+
const char *expected[] = { "crm_mon", "-a", "--", "-bc", NULL };
5762

5863
gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
5964
LISTS_EQ(processed, expected);
6065
g_strfreev(processed);
6166
}
6267

6368
static void
64-
special_args(void **state) {
69+
special_args(void **state)
70+
{
6571
const char *argv[] = { "crm_mon", "-aX", "-Fval", NULL };
66-
const gchar *expected[] = { "crm_mon", "-a", "X", "-F", "val", NULL };
72+
const char *expected[] = { "crm_mon", "-a", "X", "-F", "val", NULL };
6773

6874
gchar **processed = pcmk__cmdline_preproc((char **) argv, "aF");
6975
LISTS_EQ(processed, expected);
7076
g_strfreev(processed);
7177
}
7278

7379
static void
74-
special_arg_at_end(void **state) {
80+
special_arg_at_end(void **state)
81+
{
7582
const char *argv[] = { "crm_mon", "-a", NULL };
76-
const gchar *expected[] = { "crm_mon", "-a", NULL };
83+
const char *expected[] = { "crm_mon", "-a", NULL };
7784

7885
gchar **processed = pcmk__cmdline_preproc((char **) argv, "a");
7986
LISTS_EQ(processed, expected);
8087
g_strfreev(processed);
8188
}
8289

8390
static void
84-
long_arg(void **state) {
91+
long_arg(void **state)
92+
{
8593
const char *argv[] = { "crm_mon", "--blah=foo", NULL };
86-
const gchar *expected[] = { "crm_mon", "--blah=foo", NULL };
94+
const char *expected[] = { "crm_mon", "--blah=foo", NULL };
8795

8896
gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
8997
LISTS_EQ(processed, expected);
9098
g_strfreev(processed);
9199
}
92100

93101
static void
94-
negative_score(void **state) {
102+
negative_score(void **state)
103+
{
95104
const char *argv[] = { "crm_mon", "-v", "-1000", NULL };
96-
const gchar *expected[] = { "crm_mon", "-v", "-1000", NULL };
105+
const char *expected[] = { "crm_mon", "-v", "-1000", NULL };
97106

98107
gchar **processed = pcmk__cmdline_preproc((char **) argv, "v");
99108
LISTS_EQ(processed, expected);
100109
g_strfreev(processed);
101110
}
102111

103112
static void
104-
negative_score_2(void **state) {
113+
negative_score_2(void **state)
114+
{
105115
const char *argv[] = { "crm_mon", "-1i3", NULL };
106-
const gchar *expected[] = { "crm_mon", "-1", "-i", "-3", NULL };
116+
const char *expected[] = { "crm_mon", "-1", "-i", "-3", NULL };
107117

108118
gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
109119
LISTS_EQ(processed, expected);
110120
g_strfreev(processed);
111121
}
112122

113123
static void
114-
negative_score_3(void **state) {
124+
negative_score_3(void **state)
125+
{
115126
const char *argv[] = { "crm_attribute", "-p", "-v", "-INFINITY", NULL };
116-
const gchar *expected[] = { "crm_attribute", "-p", "-v", "-INFINITY", NULL };
127+
const char *expected[] = { "crm_attribute", "-p", "-v", "-INFINITY", NULL };
117128

118129
gchar **processed = pcmk__cmdline_preproc((char **) argv, "pv");
119130
LISTS_EQ(processed, expected);
120131
g_strfreev(processed);
121132
}
122133

123134
static void
124-
string_arg_with_dash(void **state) {
125-
const char *argv[] = { "crm_mon", "-n", "crm_mon_options", "-v", "--opt1 --opt2", NULL };
126-
const gchar *expected[] = { "crm_mon", "-n", "crm_mon_options", "-v", "--opt1 --opt2", NULL };
135+
string_arg_with_dash(void **state)
136+
{
137+
const char *argv[] = { "crm_mon", "-n", "crm_mon_options", "-v",
138+
"--opt1 --opt2", NULL };
139+
const char *expected[] = { "crm_mon", "-n", "crm_mon_options", "-v",
140+
"--opt1 --opt2", NULL };
127141

128142
gchar **processed = pcmk__cmdline_preproc((char **) argv, "v");
129143
LISTS_EQ(processed, expected);
130144
g_strfreev(processed);
131145
}
132146

133147
static void
134-
string_arg_with_dash_2(void **state) {
135-
const char *argv[] = { "crm_mon", "-n", "crm_mon_options", "-v", "-1i3", NULL };
136-
const gchar *expected[] = { "crm_mon", "-n", "crm_mon_options", "-v", "-1i3", NULL };
148+
string_arg_with_dash_2(void **state)
149+
{
150+
const char *argv[] = { "crm_mon", "-n", "crm_mon_options", "-v", "-1i3",
151+
NULL };
152+
const char *expected[] = { "crm_mon", "-n", "crm_mon_options", "-v", "-1i3",
153+
NULL };
137154

138155
gchar **processed = pcmk__cmdline_preproc((char **) argv, "v");
139156
LISTS_EQ(processed, expected);
140157
g_strfreev(processed);
141158
}
142159

143160
static void
144-
string_arg_with_dash_3(void **state) {
161+
string_arg_with_dash_3(void **state)
162+
{
145163
const char *argv[] = { "crm_mon", "-abc", "-1i3", NULL };
146-
const gchar *expected[] = { "crm_mon", "-a", "-b", "-c", "-1i3", NULL };
164+
const char *expected[] = { "crm_mon", "-a", "-b", "-c", "-1i3", NULL };
147165

148166
gchar **processed = pcmk__cmdline_preproc((char **) argv, "c");
149167
LISTS_EQ(processed, expected);

lib/common/tests/xml/pcmk__xml_is_name_char_test.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024-2025 the Pacemaker project contributors
2+
* Copyright 2024-2026 the Pacemaker project contributors
33
*
44
* The version control history for this file may have further details.
55
*
@@ -11,7 +11,7 @@
1111

1212
#include <stdbool.h>
1313

14-
#include <glib.h> // gchar, g_ascii_isalnum(), etc.
14+
#include <glib.h> // g_*
1515

1616
#include <crm/common/unittest_internal.h>
1717

@@ -28,7 +28,7 @@
2828
static void
2929
assert_name_char(int c, bool reference)
3030
{
31-
gchar utf8_buf[6] = { 0, };
31+
char utf8_buf[6] = { 0, };
3232
int len = 4;
3333
int ref_len = g_unichar_to_utf8(c, utf8_buf);
3434
bool result = pcmk__xml_is_name_char(utf8_buf, &len);

lib/common/tests/xml/pcmk__xml_is_name_start_char_test.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024-2025 the Pacemaker project contributors
2+
* Copyright 2024-2026 the Pacemaker project contributors
33
*
44
* The version control history for this file may have further details.
55
*
@@ -11,8 +11,7 @@
1111

1212
#include <stdbool.h>
1313

14-
#include <glib.h> // gchar, g_ascii_isalpha(), etc.
15-
#include <libxml/xmlstring.h> // xmlGetUTF8Char()
14+
#include <glib.h> // g_*
1615

1716
#include <crm/common/unittest_internal.h>
1817

@@ -29,7 +28,7 @@
2928
static void
3029
assert_name_start_char(int c, bool reference)
3130
{
32-
gchar utf8_buf[6] = { 0, };
31+
char utf8_buf[6] = { 0, };
3332
int len = 4;
3433
int ref_len = g_unichar_to_utf8(c, utf8_buf);
3534
bool result = pcmk__xml_is_name_start_char(utf8_buf, &len);

lib/common/xpath.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2004-2025 the Pacemaker project contributors
2+
* Copyright 2004-2026 the Pacemaker project contributors
33
*
44
* The version control history for this file may have further details.
55
*
@@ -298,7 +298,7 @@ pcmk__element_xpath(const xmlNode *xml)
298298
if (parent == NULL) {
299299
g_string_append_c(xpath, '/');
300300
} else if (parent->parent == NULL) {
301-
g_string_append(xpath, (const gchar *) xml->name);
301+
g_string_append(xpath, (const char *) xml->name);
302302
} else {
303303
pcmk__g_strcat(xpath, "/", (const char *) xml->name, NULL);
304304
}

lib/pacemaker/pcmk_fence.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ handle_level(stonith_t *st, const char *target, int fence_level, GList *devices,
4747
const char *pattern = NULL;
4848

4949
gchar **name_value = NULL;
50-
const gchar *name = NULL;
51-
const gchar *value = NULL;
50+
const char *name = NULL;
51+
const char *value = NULL;
5252
int rc = pcmk_rc_ok;
5353

5454
if (target == NULL) {

lib/pengine/pe_health.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ add_node_health_value(void *key, void *value, void *user_data)
9090
return;
9191
}
9292

93-
if (g_str_has_prefix((const gchar *) key, "#health")) {
93+
if (g_str_has_prefix((const char *) key, "#health")) {
9494
struct health_sum *health_sum = user_data;
9595
int score = 0;
9696
int rc = pcmk_parse_score((const char *) value, &score, 0);

lib/services/dbus.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ async_query_result_cb(DBusPendingCall *pending, void *user_data)
703703
*/
704704
char *
705705
pcmk_dbus_get_property(DBusConnection *connection, const char *target,
706-
const char *obj, const gchar * iface, const char *name,
706+
const char *obj, const char *iface, const char *name,
707707
property_callback_func callback, void *userdata,
708708
DBusPendingCall **pending, int timeout)
709709
{

lib/services/pcmk-dbus.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2024 the Pacemaker project contributors
2+
* Copyright 2014-2026 the Pacemaker project contributors
33
*
44
* The version control history for this file may have further details.
55
*
@@ -40,10 +40,14 @@ G_GNUC_INTERNAL
4040
bool pcmk_dbus_type_check(DBusMessage *msg, DBusMessageIter *field, int expected, const char *function, int line);
4141

4242
G_GNUC_INTERNAL
43-
char *pcmk_dbus_get_property(
44-
DBusConnection *connection, const char *target, const char *obj, const gchar * iface, const char *name,
45-
void (*callback)(const char *name, const char *value, void *userdata), void *userdata,
46-
DBusPendingCall **pending, int timeout);
43+
char *pcmk_dbus_get_property(DBusConnection *connection, const char *target,
44+
const char *obj, const char *iface,
45+
const char *name,
46+
void (*callback)(const char *name,
47+
const char *value,
48+
void *userdata),
49+
void *userdata, DBusPendingCall **pending,
50+
int timeout);
4751

4852
G_GNUC_INTERNAL
4953
bool pcmk_dbus_find_error(const DBusPendingCall *pending, DBusMessage *reply,

tools/cibadmin.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,7 @@ cibadmin_post_default(pcmk__output_t *out, cib_t *cib_conn, int call_options,
359359

360360
static void
361361
cibadmin_output_xml(pcmk__output_t *out, xmlNode *xml, int call_options,
362-
const gchar *acl_user, crm_exit_t *exit_code,
363-
GError **error)
362+
const char *acl_user, crm_exit_t *exit_code, GError **error)
364363
{
365364
if ((options.acl_render_mode != pcmk__acl_render_none)
366365
&& (*exit_code == CRM_EX_OK)
@@ -406,7 +405,7 @@ cibadmin_output_xml(pcmk__output_t *out, xmlNode *xml, int call_options,
406405
static crm_exit_t
407406
cibadmin_handle_command(pcmk__output_t *out,
408407
const cibadmin_cmd_info_t *cmd_info, int call_options,
409-
const gchar *acl_user, xmlNode *input, GError **error)
408+
const char *acl_user, xmlNode *input, GError **error)
410409
{
411410
int rc = pcmk_rc_ok;
412411
crm_exit_t exit_code = CRM_EX_OK;

0 commit comments

Comments
 (0)