OpenWrt Forum Archive

Topic: gdate.c:2497:7: error

The content of this topic has been archived on 28 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

my attempt to compile failed with:

gdate.c: In function 'g_date_strftime':
gdate.c:2497:7: error: format not a string literal, format string not checked [-Werror=format-nonliteral]
       tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
       ^~~~~~

there is a 12 months old ticket here:
https://dev.openwrt.org/ticket/22193

and there is an 11 months old patch here:
https://dev.openwrt.org/browser/trunk/t … ?rev=49342

Also it was reported , that error comes with gcc-6.1 or gcc-6.3 and with gcc-5.3 worked .

Not clear if it is fixed? still not working ? does the patch work? if it does, how can I apply it ?

Please advise !
Thanks!

found this patch:
----------------------------------------------------------------------------------------------------------------------------
diff --git a/glib/gdate.c b/glib/gdate.c
index 4aece02..cdc735c 100644
--- a/glib/gdate.c
+++ b/glib/gdate.c
@@ -2494,7 +2494,10 @@ g_date_strftime (gchar       *s,
        * recognize whether strftime actually failed or just returned "".
        */
       tmpbuf[0] = '\1';
+      #pragma GCC diagnostic push
+      #pragma GCC diagnostic ignored "-Wformat-nonliteral"
       tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
+      #pragma GCC diagnostic pop

       if (tmplen == 0 && tmpbuf[0] != '\0')
         {
-------------------------------------------------------------------------------------------------------------------------------------

HOW DO I APPLY  PATCH?
_____________________________________________________________________________________

Here is pkg-config-0.29.1/glib/glib/gdate.c       and the part which I think it is related to issue:
---------------------------------------------------------------------------------------------------------------------------------------

/**
* g_date_strftime:
* @s: destination buffer
* @slen: buffer size
* @format: format string
* @date: valid #GDate
*
* Generates a printed representation of the date, in a
* <link linkend="setlocale">locale</link>-specific way.
* Works just like the platform's C library strftime() function,
* but only accepts date-related formats; time-related formats
* give undefined results. Date must be valid. Unlike strftime()
* (which uses the locale encoding), works on a UTF-8 format
* string and stores a UTF-8 result.
*
* This function does not provide any conversion specifiers in
* addition to those implemented by the platform's C library.
* For example, don't expect that using g_date_strftime() would
* make the \%F provided by the C99 strftime() work on Windows
* where the C library only complies to C89.
*
* Returns: number of characters written to the buffer, or 0 the buffer was too small
*/
gsize     
g_date_strftime (gchar       *s,
                 gsize        slen,
                 const gchar *format,
                 const GDate *d)
{
  struct tm tm;
#ifndef G_OS_WIN32
  gsize locale_format_len = 0;
  gchar *locale_format;
  gsize tmplen;
  gchar *tmpbuf;
  gsize tmpbufsize;
  gsize convlen = 0;
  gchar *convbuf;
  GError *error = NULL;
  gsize retval;
#endif

  g_return_val_if_fail (g_date_valid (d), 0);
  g_return_val_if_fail (slen > 0, 0);
  g_return_val_if_fail (format != NULL, 0);
  g_return_val_if_fail (s != NULL, 0);

  g_date_to_struct_tm (d, &tm);

#ifdef G_OS_WIN32
  if (!g_utf8_validate (format, -1, NULL))
    {
      s[0] = '\0';
      return 0;
    }
  return win32_strftime_helper (d, format, &tm, s, slen);
#else

  locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);

  if (error)
    {
      g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
      g_error_free (error);

      s[0] = '\0';
      return 0;
    }

  tmpbufsize = MAX (128, locale_format_len * 2);
  while (TRUE)
    {
      tmpbuf = g_malloc (tmpbufsize);

      /* Set the first byte to something other than '\0', to be able to
       * recognize whether strftime actually failed or just returned "".
       */
      tmpbuf[0] = '\1';
      tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);

      if (tmplen == 0 && tmpbuf[0] != '\0')
        {
          g_free (tmpbuf);
          tmpbufsize *= 2;

          if (tmpbufsize > 65536)
            {
              g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up\n");
              g_free (locale_format);

              s[0] = '\0';
              return 0;
            }
        }
      else
        break;
    }
  g_free (locale_format);

  convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
  g_free (tmpbuf);

  if (error)
    {
      g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
      g_error_free (error);

      s[0] = '\0';
      return 0;
    }

  if (slen <= convlen)
    {
      /* Ensure only whole characters are copied into the buffer.
       */
      gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
      g_assert (end != NULL);
      convlen = end - convbuf;

      /* Return 0 because the buffer isn't large enough.
       */
      retval = 0;
    }
  else
    retval = convlen;

  memcpy (s, convbuf, convlen);
  s[convlen] = '\0';
  g_free (convbuf);

  return retval;
#endif
}
-----------------------------------------------------------------------------------------------------------------------------------------------
Based on the PATCH, what do I need to change to make it work without the ERROR ?

Thanks!

found a very simple solution
since the patch contains only few lines:

https://dev.openwrt.org/browser/trunk/t … ?rev=49342

I manually edited /build_dir/host/pkg-conf-0.29.1/glib/glib/gdate.c file using "geany"

patch shows in column a previous line number, and in column b new line number
So I just copied and pasted as shown in patch, renamed original as "gdate.c-old" and saved file as "gdate.c"

Compilation no longer stops and  complaining about gdate !

The discussion might have continued from here.