Bug 555311 – format not a string literal and no format arguments
[platform/upstream/glib.git] / glib / gmarkup.c
index 3e073f8..ccc7626 100644 (file)
@@ -242,13 +242,32 @@ static void set_error (GMarkupParseContext *context,
                       ...) G_GNUC_PRINTF (4, 5);
 
 static void
+set_error_literal (GMarkupParseContext *context,
+                   GError             **error,
+                   GMarkupError         code,
+                   const gchar         *message)
+{
+  GError *tmp_error;
+
+  tmp_error = g_error_new_literal (G_MARKUP_ERROR, code, message);
+
+  g_prefix_error (&tmp_error,
+                  _("Error on line %d char %d: "),
+                  context->line_number,
+                  context->char_number);
+
+  mark_error (context, tmp_error);
+
+  g_propagate_error (error, tmp_error);
+}
+
+static void
 set_error (GMarkupParseContext *context,
            GError             **error,
            GMarkupError         code,
            const gchar         *format,
            ...)
 {
-  GError *tmp_error;
   gchar *s;
   gchar *s_valid;
   va_list args;
@@ -257,22 +276,13 @@ set_error (GMarkupParseContext *context,
   s = g_strdup_vprintf (format, args);
   va_end (args);
 
-  /* Make sure that the GError message is valid UTF-8 even if it is 
+  /* Make sure that the GError message is valid UTF-8 even if it is
    * complaining about invalid UTF-8 in the markup: */
   s_valid = _g_utf8_make_valid (s);
-  tmp_error = g_error_new_literal (G_MARKUP_ERROR, code, s_valid);
+  set_error_literal (context, error, code, s);
 
   g_free (s);
   g_free (s_valid);
-
-  g_prefix_error (&tmp_error,
-                  _("Error on line %d char %d: "),
-                  context->line_number,
-                  context->char_number);
-
-  mark_error (context, tmp_error);
-
-  g_propagate_error (error, tmp_error);
 }
 
 static void
@@ -1047,10 +1057,10 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
               /* The leftover char portion is too big to be
                * a UTF-8 character
                */
-              set_error (context,
-                         error,
-                         G_MARKUP_ERROR_BAD_UTF8,
-                         _("Invalid UTF-8 encoded text - overlong sequence"));
+              set_error_literal (context,
+                                 error,
+                                 G_MARKUP_ERROR_BAD_UTF8,
+                                 _("Invalid UTF-8 encoded text - overlong sequence"));
             }
           
           goto finished;
@@ -1075,10 +1085,10 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
    */
   if ((*context->current_text & 0xc0) == 0x80) /* not a char start */
     {
-      set_error (context,
-                 error,
-                 G_MARKUP_ERROR_BAD_UTF8,
-                 _("Invalid UTF-8 encoded text - not a start char"));
+      set_error_literal (context,
+                         error,
+                         G_MARKUP_ERROR_BAD_UTF8,
+                         _("Invalid UTF-8 encoded text - not a start char"));
       goto finished;
     }
 
@@ -1096,6 +1106,8 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
     {
       gint newlines = 0;
       const gchar *p, *q;
+      gchar *current_text_dup;
+
       q = p = context->current_text;
       while (p != first_invalid)
         {
@@ -1111,12 +1123,13 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
       context->line_number += newlines;
       context->char_number += g_utf8_strlen (q, first_invalid - q);
 
+      current_text_dup = g_strndup (context->current_text, context->current_text_len);
       set_error (context,
                  error,
                  G_MARKUP_ERROR_BAD_UTF8,
                  _("Invalid UTF-8 encoded text - not valid '%s'"),
-                 g_strndup (context->current_text,
-                            context->current_text_len));
+                 current_text_dup);
+      g_free (current_text_dup);
       goto finished;
     }
 
@@ -1149,10 +1162,10 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                 }
               else
                 {
-                  set_error (context,
-                             error,
-                             G_MARKUP_ERROR_PARSE,
-                             _("Document must begin with an element (e.g. <book>)"));
+                  set_error_literal (context,
+                                     error,
+                                     G_MARKUP_ERROR_PARSE,
+                                     _("Document must begin with an element (e.g. <book>)"));
                 }
             }
           break;
@@ -1841,8 +1854,8 @@ g_markup_parse_context_end_parse (GMarkupParseContext *context,
 
   if (context->document_empty)
     {
-      set_error (context, error, G_MARKUP_ERROR_EMPTY,
-                 _("Document was empty or contained only whitespace"));
+      set_error_literal (context, error, G_MARKUP_ERROR_EMPTY,
+                         _("Document was empty or contained only whitespace"));
       return FALSE;
     }
   
@@ -1855,8 +1868,8 @@ g_markup_parse_context_end_parse (GMarkupParseContext *context,
       break;
 
     case STATE_AFTER_OPEN_ANGLE:
-      set_error (context, error, G_MARKUP_ERROR_PARSE,
-                 _("Document ended unexpectedly just after an open angle bracket '<'"));
+      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
+                         _("Document ended unexpectedly just after an open angle bracket '<'"));
       break;
 
     case STATE_AFTER_CLOSE_ANGLE:
@@ -1877,33 +1890,33 @@ g_markup_parse_context_end_parse (GMarkupParseContext *context,
       break;
 
     case STATE_INSIDE_OPEN_TAG_NAME:
-      set_error (context, error, G_MARKUP_ERROR_PARSE,
-                 _("Document ended unexpectedly inside an element name"));
+      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
+                         _("Document ended unexpectedly inside an element name"));
       break;
 
     case STATE_INSIDE_ATTRIBUTE_NAME:
     case STATE_AFTER_ATTRIBUTE_NAME:
-      set_error (context, error, G_MARKUP_ERROR_PARSE,
-                 _("Document ended unexpectedly inside an attribute name"));
+      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
+                         _("Document ended unexpectedly inside an attribute name"));
       break;
 
     case STATE_BETWEEN_ATTRIBUTES:
-      set_error (context, error, G_MARKUP_ERROR_PARSE,
-                 _("Document ended unexpectedly inside an element-opening "
-                   "tag."));
+      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
+                         _("Document ended unexpectedly inside an element-opening "
+                           "tag."));
       break;
 
     case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
-      set_error (context, error, G_MARKUP_ERROR_PARSE,
-                 _("Document ended unexpectedly after the equals sign "
-                   "following an attribute name; no attribute value"));
+      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
+                         _("Document ended unexpectedly after the equals sign "
+                           "following an attribute name; no attribute value"));
       break;
 
     case STATE_INSIDE_ATTRIBUTE_VALUE_SQ:
     case STATE_INSIDE_ATTRIBUTE_VALUE_DQ:
-      set_error (context, error, G_MARKUP_ERROR_PARSE,
-                 _("Document ended unexpectedly while inside an attribute "
-                   "value"));
+      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
+                         _("Document ended unexpectedly while inside an attribute "
+                           "value"));
       break;
 
     case STATE_INSIDE_TEXT:
@@ -1923,9 +1936,9 @@ g_markup_parse_context_end_parse (GMarkupParseContext *context,
       break;
 
     case STATE_INSIDE_PASSTHROUGH:
-      set_error (context, error, G_MARKUP_ERROR_PARSE,
-                 _("Document ended unexpectedly inside a comment or "
-                   "processing instruction"));
+      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
+                         _("Document ended unexpectedly inside a comment or "
+                           "processing instruction"));
       break;
 
     case STATE_ERROR: