Glib (and gtk too) boolean sizing

Hi everyone,

is there any flags to indicate possible improper use of booleans in code?
(like with G_OPTION_ARG_NONE and boolean options)

What kind of “improper use” are you thinking of? Care to give an example?

Sure, for example

#include <gtk/gtk.h>

int main(int argc, char **argv) {
  bool a = 0, b = 0; // '-a -b' can result in '1 0'
// int a = 0, b = 0; // '-a -b' results in '1 1'
  const GOptionEntry options[] = {
    { .long_name = "a", .short_name = 'a', .arg = G_OPTION_ARG_NONE, .arg_data = &a },
    { .long_name = "b", .short_name = 'b', .arg = G_OPTION_ARG_NONE, .arg_data = &b },
    {},
  };
  GOptionGroup *og = g_option_group_new("test", "test", "options", NULL, NULL);
  if (!og) { g_warning("%s failed", "g_option_group_new()"); return -1; }
  g_option_group_add_entries(og, options);
  GOptionContext *oc = g_option_context_new(NULL);
  if (!oc) { g_warning("%s failed", "g_option_context_new()"); return -2; }
  g_option_context_set_main_group(oc, og);
  gint ac = argc;
  gchar **av = argv;
  GError *error = NULL;
  if (!g_option_context_parse(oc, &ac, &av, &error)) { g_warning("%s", error->message); return -3; }
  g_print("a=%d b=%d\n", a, b);
  return 0;
}

If one misread booleans as ‘bool’ on GLib.OptionArg page

G_OPTION_ARG_NONE
No extra argument. This is useful for simple flags or booleans.

p.s.
On another page GLib.OptionArgFunc it’s noted more precisely

the required type of the location depends on the arg type: - G_OPTION_ARG_NONE: %gboolean

GLib predates C99 being standardised, so it has its own boolean type called gboolean, which is an alias to int. Whenever you read “boolean” in the GLib documentation, it’s always implied to be gboolean.

The C99 bool type has no defined size—it has to be big enough to store 1, so toolchains are allowed to make it as big or as small as they wish; this means taking its address when the caller expects an int may end up overspilling.

You can use true and false constants where GLib expects a gboolean; but you can’t do the other way around, and use a bool where GLib passes a gboolean.

2 Likes

Yes, it is connected with sizing. Just because there is no warnings made by compilers or c analyzers (‘-Wall’ or “gcc -fanalyzer”) to spot it: thougnt if some internal glib warnings like 'int' type expected but the argument has type 'bool (int8)' exist to be used as a warning?

No, it’s really not possible to deal with this from GLib: all that GLib sees is a pointer.

Okay, thanks for the answer. As I can see in ‘glib/gtypes.h’ only gboolean could be misread as ‘bool’, other types go just with adding g- prefix

typedef char   gchar;
typedef short  gshort;
typedef long   glong;
typedef int    gint;
typedef gint   gboolean;

Be especially careful when passing booleans to any function that receives untyped pointers as arguments. For example, g_object_get (my_object, "bool-property", &bool, NULL); is a great way to corrupt the stack if bool is a C99 bool rather than a gboolean.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.