Don't use glib format modifiers with sscanf or printf
authorNirbheek Chauhan <nirbheek@centricular.com>
Tue, 25 Feb 2020 13:43:59 +0000 (19:13 +0530)
committerNirbheek Chauhan <nirbheek@centricular.com>
Tue, 25 Feb 2020 15:52:28 +0000 (21:22 +0530)
We do not have a way to know the format modifiers to use with string
functions provided by the system. `G_GUINT64_FORMAT` and other string
modifiers only work for glib string formatting functions. We cannot
use them for string functions provided by the stdlib. See:
https://developer.gnome.org/glib/stable/glib-Basic-Types.html#glib-Basic-Types.description

F.ex.:

```
 ../tools/gst-stats.c:921:11: error: too many arguments for format [-Werror=format-extra-args]
   printf ("Number of Buffers passed: %" G_GUINT64_FORMAT "\n", num_buffers);
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../tools/gst-stats.c:922:11: error: unknown conversion type character 'l' in format [-Werror=format=]
   printf ("Number of Events sent: %" G_GUINT64_FORMAT "\n", num_events);
           ^~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /builds/nirbheek/cerbero/cerbero-build/dist/windows_x86_64/include/glib-2.0/glib/gtypes.h:32,
                 from /builds/nirbheek/cerbero/cerbero-build/dist/windows_x86_64/include/glib-2.0/glib/galloca.h:32,
                 from /builds/nirbheek/cerbero/cerbero-build/dist/windows_x86_64/include/glib-2.0/glib.h:30,
                 from ../gst/gst.h:27,
                 from ../tools/tools.h:28,
                 from ../tools/gst-stats.c:30:
/builds/nirbheek/cerbero/cerbero-build/dist/windows_x86_64/lib/glib-2.0/include/glibconfig.h:69:28: note: format string is defined here
 #define G_GUINT64_FORMAT "llu"
                            ^
```

and

```
../tests/misc/netclock-replay.c: In function 'main':
../tests/misc/netclock-replay.c:98:23: error: unknown conversion type character 'l' in format [-Werror=format=]
     if (sscanf (line, "%" G_GUINT64_FORMAT " %" G_GUINT64_FORMAT " %"
                       ^~~
In file included from /builds/nirbheek/cerbero/cerbero-build/dist/windows_x86/include/glib-2.0/glib/gtypes.h:32,
                 from /builds/nirbheek/cerbero/cerbero-build/dist/windows_x86/include/glib-2.0/glib/galloca.h:32,
                 from /builds/nirbheek/cerbero/cerbero-build/dist/windows_x86/include/glib-2.0/glib.h:30,
                 from ../tests/misc/../../libs/gst/net/gstntppacket.c:38,
                 from ../tests/misc/netclock-replay.c:31:
/builds/nirbheek/cerbero/cerbero-build/dist/windows_x86/lib/glib-2.0/include/glibconfig.h:69:28: note: format string is defined here
 #define G_GUINT64_FORMAT "llu"
                            ^
```

This is needed for upgrading glib inside Cerbero which builds with
`-Werror` on Windows:
https://gitlab.freedesktop.org/gstreamer/cerbero/merge_requests/419

tests/misc/netclock-replay.c
tools/gst-stats.c

index b7b741b..e405902 100644 (file)
@@ -48,6 +48,41 @@ static GOptionEntry entries[] = {
   {NULL,}
 };
 
+static gboolean
+parse_time_values (const gchar * line, GstClockTime * local_1,
+    GstClockTime * remote_1, GstClockTime * remote_2, GstClockTime * local_2)
+{
+  gchar **split;
+  gboolean ret = FALSE;
+
+  if (!line)
+    return FALSE;
+
+  split = g_strsplit (line, " ", -1);
+
+  if (g_strv_length (split) != 4)
+    goto out;
+
+  if (!g_ascii_string_to_unsigned (split[0], 10, 0, G_MAXUINT64, local_1, NULL))
+    goto out;
+
+  if (!g_ascii_string_to_unsigned (split[1], 10, 0, G_MAXUINT64, remote_1,
+          NULL))
+    goto out;
+
+  if (!g_ascii_string_to_unsigned (split[2], 10, 0, G_MAXUINT64, remote_2,
+          NULL))
+    goto out;
+
+  if (!g_ascii_string_to_unsigned (split[3], 10, 0, G_MAXUINT64, local_2, NULL))
+    goto out;
+
+  ret = TRUE;
+out:
+  g_strfreev (split);
+  return ret;
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -92,12 +127,10 @@ main (int argc, char *argv[])
 
   while ((status = g_io_channel_read_line (channel, &line, NULL, NULL,
               &error)) == G_IO_STATUS_NORMAL) {
-    GstClockTime local_1, local_2, remote_1, remote_2;
+    GstClockTime local_1, remote_1, remote_2, local_2;
     GstMessage *message;
 
-    if (sscanf (line, "%" G_GUINT64_FORMAT " %" G_GUINT64_FORMAT " %"
-            G_GUINT64_FORMAT " %" G_GUINT64_FORMAT, &local_1, &remote_1,
-            &remote_2, &local_2) != 4) {
+    if (!parse_time_values (line, &local_1, &remote_1, &remote_2, &local_2)) {
       g_print ("Failed to get local/remote time values from: %s\n", line);
       goto done;
     }
index ee14f32..2f2f61d 100644 (file)
@@ -693,22 +693,22 @@ print_element_stats (gpointer value, gpointer user_data)
 
     printf ("  %-45s:", fullname);
     if (stats->recv_buffers)
-      printf (" buffers in/out %7u", stats->recv_buffers);
+      g_print (" buffers in/out %7u", stats->recv_buffers);
     else
-      printf (" buffers in/out %7s", "-");
+      g_print (" buffers in/out %7s", "-");
     if (stats->sent_buffers)
-      printf ("/%7u", stats->sent_buffers);
+      g_print ("/%7u", stats->sent_buffers);
     else
-      printf ("/%7s", "-");
+      g_print ("/%7s", "-");
     if (stats->recv_bytes)
-      printf (" bytes in/out %12" G_GUINT64_FORMAT, stats->recv_bytes);
+      g_print (" bytes in/out %12" G_GUINT64_FORMAT, stats->recv_bytes);
     else
-      printf (" bytes in/out %12s", "-");
+      g_print (" bytes in/out %12s", "-");
     if (stats->sent_bytes)
-      printf ("/%12" G_GUINT64_FORMAT, stats->sent_bytes);
+      g_print ("/%12" G_GUINT64_FORMAT, stats->sent_bytes);
     else
       printf ("/%12s", "-");
-    printf (" first activity %" GST_TIME_FORMAT ", "
+    g_print (" first activity %" GST_TIME_FORMAT ", "
         " ev/msg/qry sent %5u/%5u/%5u\n", GST_TIME_ARGS (stats->first_ts),
         stats->num_events, stats->num_messages, stats->num_queries);
   }
@@ -912,21 +912,21 @@ print_stats (void)
   guint num_threads = g_hash_table_size (threads);
 
   /* print overall stats */
-  puts ("\nOverall Statistics:");
-  printf ("Number of Threads: %u\n", num_threads);
-  printf ("Number of Elements: %u\n", num_elements - num_bins);
-  printf ("Number of Bins: %u\n", num_bins);
-  printf ("Number of Pads: %u\n", num_pads - num_ghostpads);
-  printf ("Number of GhostPads: %u\n", num_ghostpads);
-  printf ("Number of Buffers passed: %" G_GUINT64_FORMAT "\n", num_buffers);
-  printf ("Number of Events sent: %" G_GUINT64_FORMAT "\n", num_events);
-  printf ("Number of Message sent: %" G_GUINT64_FORMAT "\n", num_messages);
-  printf ("Number of Queries sent: %" G_GUINT64_FORMAT "\n", num_queries);
-  printf ("Time: %" GST_TIME_FORMAT "\n", GST_TIME_ARGS (last_ts));
+  g_print ("\nOverall Statistics:\n");
+  g_print ("Number of Threads: %u\n", num_threads);
+  g_print ("Number of Elements: %u\n", num_elements - num_bins);
+  g_print ("Number of Bins: %u\n", num_bins);
+  g_print ("Number of Pads: %u\n", num_pads - num_ghostpads);
+  g_print ("Number of GhostPads: %u\n", num_ghostpads);
+  g_print ("Number of Buffers passed: %" G_GUINT64_FORMAT "\n", num_buffers);
+  g_print ("Number of Events sent: %" G_GUINT64_FORMAT "\n", num_events);
+  g_print ("Number of Message sent: %" G_GUINT64_FORMAT "\n", num_messages);
+  g_print ("Number of Queries sent: %" G_GUINT64_FORMAT "\n", num_queries);
+  g_print ("Time: %" GST_TIME_FORMAT "\n", GST_TIME_ARGS (last_ts));
   if (have_cpuload) {
-    printf ("Avg CPU load: %4.1f %%\n", (gfloat) total_cpuload / 10.0);
+    g_print ("Avg CPU load: %4.1f %%\n", (gfloat) total_cpuload / 10.0);
   }
-  puts ("");
+  g_print ("\n");
 
   /* thread stats */
   if (num_threads) {