GNOME Bugzilla – Bug 547199
Constness of args in 'NULL terminated string array' functions
Last modified: 2017-09-11 21:46:01 UTC
Is there any reason why g_strv_length() is declared as: guint g_strv_length (gchar **str_array) instead of guint g_strv_length (const gchar * const * str_array) as one would expect? (Same question for g_strdupv() and g_strjoinv().) The implementation of the a.m. functions doesn't seem to require write access to pointers and data in str_array, so we should tell this to both the developer and the compiler. Other information:
Created attachment 157626 [details] [review] Bug 547199 — Constness of args in 'NULL terminated string array' functions Make g_strv_length(), g_strdupv() and g_strjoinv() const-correct. Closes: bgo#547199
Created attachment 157627 [details] [review] Bug 547199 — Constness of args in 'NULL terminated string array' functions Make g_strv_length(), g_strdupv() and g_strjoinv() const-correct and fix various internal uses of them in GLib. Closes: bgo#547199
Created attachment 157628 [details] [review] Bug 547199 — Constness of args in 'NULL terminated string array' functions Make g_strv_length(), g_strdupv() and g_strjoinv() const-correct and fix various internal uses of them in GLib. Closes: bgo#547199
I'm getting gcc warnings like the one below with the patch applied, but I can't work out why it doesn't want to promote "char**" to "const char * const *" implicitly. gthemedicon.c: In function ‘IA__g_themed_icon_append_name’: gthemedicon.c:384: warning: passing argument 1 of ‘g_strv_length’ from incompatible pointer type ../glib/gstrfuncs.h:243: note: expected ‘const gchar * const*’ but argument is of type ‘char **’
AFAIK this is a notorious C problem: implicit casts work on the top level only (i.e. without recursion). A shortcoming of the C standard, if you put it that way. It leads to (char * *) implicitly being casted to (char * const *), but never to (const char * const *) as we would need. I suppose using an explicit cast is the only way out here.
(In reply to Philip Withnall from comment #4) > I'm getting gcc warnings like the one below with the patch applied, but I > can't work out why it doesn't want to promote "char**" to "const char * > const *" implicitly. > > gthemedicon.c: In function ‘IA__g_themed_icon_append_name’: > gthemedicon.c:384: warning: passing argument 1 of ‘g_strv_length’ from > incompatible pointer type > ../glib/gstrfuncs.h:243: note: expected ‘const gchar * const*’ but argument > is of type ‘char **’ ⇒ And for this reason, we can’t change this API, as it would cause warnings in loads of existing code.
Review of attachment 157628 [details] [review]: --