Ability to use ordered argument gaps with GLib's printf variants
man 3 printf
indicates:
There may be no gaps in the numbers of arguments specified using '$'; for example, if arguments 1 and 3 are specified, argument 2 must also be specified somewhere in the format string.
Except that actually testing a standalone printf with ordered argument gaps, the printf available in my Debian machine works fine with gaps ordered arguments, unlike the GLib's variants, which crash with
*** invalid %N$ use detected ***
Here is a test code:
#include <stdio.h>
#include <libintl.h>
#include <glib.h>
int
main (int argc,
char *argv[])
{
const char *ntext;
gchar *text;
gchar fmt_str[256];
int i = 1;
ntext = ngettext ("Hello our world: %2$s\n",
"Hello world %d: %s\n",
i);
/* This works! */
printf (ntext, i, "earth");
/* This works too! */
snprintf (fmt_str, sizeof (fmt_str),
ntext, i, "earth");
printf (fmt_str);
/* This crashes! */
g_strdup_printf (ntext, i, "earth");
return 0;
}
This code crashes only on the g_strdup_printf
, not the printf
and snprintf
using the same argument gap (first argument is unused):
$ ./a.out
Hello our world: earth
Hello our world: earth
*** invalid %N$ use detected ***
Aborted
$ echo $?
134
It would be nice if g_strdup_printf
and other GLib variants were able to handle argument gaps as it's typically something which might happen especially with localization and plural forms.
Note that actually g_strdup_printf
handles these fine in some cases, but we couldn't figure out why (probably some build options set up by the build system), since this is a bug which appeared in GIMP where we had such printf with argument gap: gimp#9362 (closed)
For some people (e.g. me), it worked fine within GIMP code, but others experienced the crash. On the other hand, I was able to always experience the crash when building the small standalone code above.
For now, I just inversed the argument orders so that it always works. But we would appreciate printf variants robust to argument gaps as a general rule. :-)