• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GLib Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GLib at ftp://ftp.gtk.org/pub/gtk/.
23  */
24 
25 /**
26  * SECTION:error_reporting
27  * @Title: Error Reporting
28  * @Short_description: a system for reporting errors
29  *
30  * GLib provides a standard method of reporting errors from a called
31  * function to the calling code. (This is the same problem solved by
32  * exceptions in other languages.) It's important to understand that
33  * this method is both a data type (the #GError struct) and a [set of
34  * rules][gerror-rules]. If you use #GError incorrectly, then your code will not
35  * properly interoperate with other code that uses #GError, and users
36  * of your API will probably get confused. In most cases, [using #GError is
37  * preferred over numeric error codes][gerror-comparison], but there are
38  * situations where numeric error codes are useful for performance.
39  *
40  * First and foremost: #GError should only be used to report recoverable
41  * runtime errors, never to report programming errors. If the programmer
42  * has screwed up, then you should use g_warning(), g_return_if_fail(),
43  * g_assert(), g_error(), or some similar facility. (Incidentally,
44  * remember that the g_error() function should only be used for
45  * programming errors, it should not be used to print any error
46  * reportable via #GError.)
47  *
48  * Examples of recoverable runtime errors are "file not found" or
49  * "failed to parse input." Examples of programming errors are "NULL
50  * passed to strcmp()" or "attempted to free the same pointer twice."
51  * These two kinds of errors are fundamentally different: runtime errors
52  * should be handled or reported to the user, programming errors should
53  * be eliminated by fixing the bug in the program. This is why most
54  * functions in GLib and GTK+ do not use the #GError facility.
55  *
56  * Functions that can fail take a return location for a #GError as their
57  * last argument. On error, a new #GError instance will be allocated and
58  * returned to the caller via this argument. For example:
59  * |[<!-- language="C" -->
60  * gboolean g_file_get_contents (const gchar  *filename,
61  *                               gchar       **contents,
62  *                               gsize        *length,
63  *                               GError      **error);
64  * ]|
65  * If you pass a non-%NULL value for the `error` argument, it should
66  * point to a location where an error can be placed. For example:
67  * |[<!-- language="C" -->
68  * gchar *contents;
69  * GError *err = NULL;
70  *
71  * g_file_get_contents ("foo.txt", &contents, NULL, &err);
72  * g_assert ((contents == NULL && err != NULL) || (contents != NULL && err == NULL));
73  * if (err != NULL)
74  *   {
75  *     // Report error to user, and free error
76  *     g_assert (contents == NULL);
77  *     fprintf (stderr, "Unable to read file: %s\n", err->message);
78  *     g_error_free (err);
79  *   }
80  * else
81  *   {
82  *     // Use file contents
83  *     g_assert (contents != NULL);
84  *   }
85  * ]|
86  * Note that `err != NULL` in this example is a reliable indicator
87  * of whether g_file_get_contents() failed. Additionally,
88  * g_file_get_contents() returns a boolean which
89  * indicates whether it was successful.
90  *
91  * Because g_file_get_contents() returns %FALSE on failure, if you
92  * are only interested in whether it failed and don't need to display
93  * an error message, you can pass %NULL for the @error argument:
94  * |[<!-- language="C" -->
95  * if (g_file_get_contents ("foo.txt", &contents, NULL, NULL)) // ignore errors
96  *   // no error occurred
97  *   ;
98  * else
99  *   // error
100  *   ;
101  * ]|
102  *
103  * The #GError object contains three fields: @domain indicates the module
104  * the error-reporting function is located in, @code indicates the specific
105  * error that occurred, and @message is a user-readable error message with
106  * as many details as possible. Several functions are provided to deal
107  * with an error received from a called function: g_error_matches()
108  * returns %TRUE if the error matches a given domain and code,
109  * g_propagate_error() copies an error into an error location (so the
110  * calling function will receive it), and g_clear_error() clears an
111  * error location by freeing the error and resetting the location to
112  * %NULL. To display an error to the user, simply display the @message,
113  * perhaps along with additional context known only to the calling
114  * function (the file being opened, or whatever - though in the
115  * g_file_get_contents() case, the @message already contains a filename).
116  *
117  * Note, however, that many error messages are too technical to display to the
118  * user in an application, so prefer to use g_error_matches() to categorize errors
119  * from called functions, and build an appropriate error message for the context
120  * within your application. Error messages from a #GError are more appropriate
121  * to be printed in system logs or on the command line. They are typically
122  * translated.
123  *
124  * When implementing a function that can report errors, the basic
125  * tool is g_set_error(). Typically, if a fatal error occurs you
126  * want to g_set_error(), then return immediately. g_set_error()
127  * does nothing if the error location passed to it is %NULL.
128  * Here's an example:
129  * |[<!-- language="C" -->
130  * gint
131  * foo_open_file (GError **error)
132  * {
133  *   gint fd;
134  *   int saved_errno;
135  *
136  *   g_return_val_if_fail (error == NULL || *error == NULL, -1);
137  *
138  *   fd = open ("file.txt", O_RDONLY);
139  *   saved_errno = errno;
140  *
141  *   if (fd < 0)
142  *     {
143  *       g_set_error (error,
144  *                    FOO_ERROR,                 // error domain
145  *                    FOO_ERROR_BLAH,            // error code
146  *                    "Failed to open file: %s", // error message format string
147  *                    g_strerror (saved_errno));
148  *       return -1;
149  *     }
150  *   else
151  *     return fd;
152  * }
153  * ]|
154  *
155  * Things are somewhat more complicated if you yourself call another
156  * function that can report a #GError. If the sub-function indicates
157  * fatal errors in some way other than reporting a #GError, such as
158  * by returning %TRUE on success, you can simply do the following:
159  * |[<!-- language="C" -->
160  * gboolean
161  * my_function_that_can_fail (GError **err)
162  * {
163  *   g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
164  *
165  *   if (!sub_function_that_can_fail (err))
166  *     {
167  *       // assert that error was set by the sub-function
168  *       g_assert (err == NULL || *err != NULL);
169  *       return FALSE;
170  *     }
171  *
172  *   // otherwise continue, no error occurred
173  *   g_assert (err == NULL || *err == NULL);
174  * }
175  * ]|
176  *
177  * If the sub-function does not indicate errors other than by
178  * reporting a #GError (or if its return value does not reliably indicate
179  * errors) you need to create a temporary #GError
180  * since the passed-in one may be %NULL. g_propagate_error() is
181  * intended for use in this case.
182  * |[<!-- language="C" -->
183  * gboolean
184  * my_function_that_can_fail (GError **err)
185  * {
186  *   GError *tmp_error;
187  *
188  *   g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
189  *
190  *   tmp_error = NULL;
191  *   sub_function_that_can_fail (&tmp_error);
192  *
193  *   if (tmp_error != NULL)
194  *     {
195  *       // store tmp_error in err, if err != NULL,
196  *       // otherwise call g_error_free() on tmp_error
197  *       g_propagate_error (err, tmp_error);
198  *       return FALSE;
199  *     }
200  *
201  *   // otherwise continue, no error occurred
202  * }
203  * ]|
204  *
205  * Error pileups are always a bug. For example, this code is incorrect:
206  * |[<!-- language="C" -->
207  * gboolean
208  * my_function_that_can_fail (GError **err)
209  * {
210  *   GError *tmp_error;
211  *
212  *   g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
213  *
214  *   tmp_error = NULL;
215  *   sub_function_that_can_fail (&tmp_error);
216  *   other_function_that_can_fail (&tmp_error);
217  *
218  *   if (tmp_error != NULL)
219  *     {
220  *       g_propagate_error (err, tmp_error);
221  *       return FALSE;
222  *     }
223  * }
224  * ]|
225  * @tmp_error should be checked immediately after sub_function_that_can_fail(),
226  * and either cleared or propagated upward. The rule is: after each error,
227  * you must either handle the error, or return it to the calling function.
228  *
229  * Note that passing %NULL for the error location is the equivalent
230  * of handling an error by always doing nothing about it. So the
231  * following code is fine, assuming errors in sub_function_that_can_fail()
232  * are not fatal to my_function_that_can_fail():
233  * |[<!-- language="C" -->
234  * gboolean
235  * my_function_that_can_fail (GError **err)
236  * {
237  *   GError *tmp_error;
238  *
239  *   g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
240  *
241  *   sub_function_that_can_fail (NULL); // ignore errors
242  *
243  *   tmp_error = NULL;
244  *   other_function_that_can_fail (&tmp_error);
245  *
246  *   if (tmp_error != NULL)
247  *     {
248  *       g_propagate_error (err, tmp_error);
249  *       return FALSE;
250  *     }
251  * }
252  * ]|
253  *
254  * Note that passing %NULL for the error location ignores errors;
255  * it's equivalent to
256  * `try { sub_function_that_can_fail (); } catch (...) {}`
257  * in C++. It does not mean to leave errors unhandled; it means
258  * to handle them by doing nothing.
259  *
260  * Error domains and codes are conventionally named as follows:
261  *
262  * - The error domain is called <NAMESPACE>_<MODULE>_ERROR,
263  *   for example %G_SPAWN_ERROR or %G_THREAD_ERROR:
264  *   |[<!-- language="C" -->
265  *   #define G_SPAWN_ERROR g_spawn_error_quark ()
266  *
267  *   GQuark
268  *   g_spawn_error_quark (void)
269  *   {
270  *       return g_quark_from_static_string ("g-spawn-error-quark");
271  *   }
272  *   ]|
273  *
274  * - The quark function for the error domain is called
275  *   <namespace>_<module>_error_quark,
276  *   for example g_spawn_error_quark() or g_thread_error_quark().
277  *
278  * - The error codes are in an enumeration called
279  *   <Namespace><Module>Error;
280  *   for example, #GThreadError or #GSpawnError.
281  *
282  * - Members of the error code enumeration are called
283  *   <NAMESPACE>_<MODULE>_ERROR_<CODE>,
284  *   for example %G_SPAWN_ERROR_FORK or %G_THREAD_ERROR_AGAIN.
285  *
286  * - If there's a "generic" or "unknown" error code for unrecoverable
287  *   errors it doesn't make sense to distinguish with specific codes,
288  *   it should be called <NAMESPACE>_<MODULE>_ERROR_FAILED,
289  *   for example %G_SPAWN_ERROR_FAILED. In the case of error code
290  *   enumerations that may be extended in future releases, you should
291  *   generally not handle this error code explicitly, but should
292  *   instead treat any unrecognized error code as equivalent to
293  *   FAILED.
294  *
295  * ## Comparison of #GError and traditional error handling # {#gerror-comparison}
296  *
297  * #GError has several advantages over traditional numeric error codes:
298  * importantly, tools like
299  * [gobject-introspection](https://developer.gnome.org/gi/stable/) understand
300  * #GErrors and convert them to exceptions in bindings; the message includes
301  * more information than just a code; and use of a domain helps prevent
302  * misinterpretation of error codes.
303  *
304  * #GError has disadvantages though: it requires a memory allocation, and
305  * formatting the error message string has a performance overhead. This makes it
306  * unsuitable for use in retry loops where errors are a common case, rather than
307  * being unusual. For example, using %G_IO_ERROR_WOULD_BLOCK means hitting these
308  * overheads in the normal control flow. String formatting overhead can be
309  * eliminated by using g_set_error_literal() in some cases.
310  *
311  * These performance issues can be compounded if a function wraps the #GErrors
312  * returned by the functions it calls: this multiplies the number of allocations
313  * and string formatting operations. This can be partially mitigated by using
314  * g_prefix_error().
315  *
316  * ## Rules for use of #GError # {#gerror-rules}
317  *
318  * Summary of rules for use of #GError:
319  *
320  * - Do not report programming errors via #GError.
321  *
322  * - The last argument of a function that returns an error should
323  *   be a location where a #GError can be placed (i.e. "#GError** error").
324  *   If #GError is used with varargs, the #GError** should be the last
325  *   argument before the "...".
326  *
327  * - The caller may pass %NULL for the #GError** if they are not interested
328  *   in details of the exact error that occurred.
329  *
330  * - If %NULL is passed for the #GError** argument, then errors should
331  *   not be returned to the caller, but your function should still
332  *   abort and return if an error occurs. That is, control flow should
333  *   not be affected by whether the caller wants to get a #GError.
334  *
335  * - If a #GError is reported, then your function by definition had a
336  *   fatal failure and did not complete whatever it was supposed to do.
337  *   If the failure was not fatal, then you handled it and you should not
338  *   report it. If it was fatal, then you must report it and discontinue
339  *   whatever you were doing immediately.
340  *
341  * - If a #GError is reported, out parameters are not guaranteed to
342  *   be set to any defined value.
343  *
344  * - A #GError* must be initialized to %NULL before passing its address
345  *   to a function that can report errors.
346  *
347  * - "Piling up" errors is always a bug. That is, if you assign a
348  *   new #GError to a #GError* that is non-%NULL, thus overwriting
349  *   the previous error, it indicates that you should have aborted
350  *   the operation instead of continuing. If you were able to continue,
351  *   you should have cleared the previous error with g_clear_error().
352  *   g_set_error() will complain if you pile up errors.
353  *
354  * - By convention, if you return a boolean value indicating success
355  *   then %TRUE means success and %FALSE means failure. Avoid creating
356  *   functions which have a boolean return value and a GError parameter,
357  *   but where the boolean does something other than signal whether the
358  *   GError is set.  Among other problems, it requires C callers to allocate
359  *   a temporary error.  Instead, provide a "gboolean *" out parameter.
360  *   There are functions in GLib itself such as g_key_file_has_key() that
361  *   are deprecated because of this. If %FALSE is returned, the error must
362  *   be set to a non-%NULL value.  One exception to this is that in situations
363  *   that are already considered to be undefined behaviour (such as when a
364  *   g_return_val_if_fail() check fails), the error need not be set.
365  *   Instead of checking separately whether the error is set, callers
366  *   should ensure that they do not provoke undefined behaviour, then
367  *   assume that the error will be set on failure.
368  *
369  * - A %NULL return value is also frequently used to mean that an error
370  *   occurred. You should make clear in your documentation whether %NULL
371  *   is a valid return value in non-error cases; if %NULL is a valid value,
372  *   then users must check whether an error was returned to see if the
373  *   function succeeded.
374  *
375  * - When implementing a function that can report errors, you may want
376  *   to add a check at the top of your function that the error return
377  *   location is either %NULL or contains a %NULL error (e.g.
378  *   `g_return_if_fail (error == NULL || *error == NULL);`).
379  */
380 
381 #include "config.h"
382 
383 #include "gerror.h"
384 
385 #include "gslice.h"
386 #include "gstrfuncs.h"
387 #include "gtestutils.h"
388 
389 /**
390  * g_error_new_valist:
391  * @domain: error domain
392  * @code: error code
393  * @format: printf()-style format for error message
394  * @args: #va_list of parameters for the message format
395  *
396  * Creates a new #GError with the given @domain and @code,
397  * and a message formatted with @format.
398  *
399  * Returns: a new #GError
400  *
401  * Since: 2.22
402  */
403 GError*
g_error_new_valist(GQuark domain,gint code,const gchar * format,va_list args)404 g_error_new_valist (GQuark       domain,
405                     gint         code,
406                     const gchar *format,
407                     va_list      args)
408 {
409   GError *error;
410 
411   /* Historically, GError allowed this (although it was never meant to work),
412    * and it has significant use in the wild, which g_return_val_if_fail
413    * would break. It should maybe g_return_val_if_fail in GLib 4.
414    * (GNOME#660371, GNOME#560482)
415    */
416   g_warn_if_fail (domain != 0);
417   g_warn_if_fail (format != NULL);
418 
419   error = g_slice_new (GError);
420 
421   error->domain = domain;
422   error->code = code;
423   error->message = g_strdup_vprintf (format, args);
424 
425   return error;
426 }
427 
428 /**
429  * g_error_new:
430  * @domain: error domain
431  * @code: error code
432  * @format: printf()-style format for error message
433  * @...: parameters for message format
434  *
435  * Creates a new #GError with the given @domain and @code,
436  * and a message formatted with @format.
437  *
438  * Returns: a new #GError
439  */
440 GError*
g_error_new(GQuark domain,gint code,const gchar * format,...)441 g_error_new (GQuark       domain,
442              gint         code,
443              const gchar *format,
444              ...)
445 {
446   GError* error;
447   va_list args;
448 
449   g_return_val_if_fail (format != NULL, NULL);
450   g_return_val_if_fail (domain != 0, NULL);
451 
452   va_start (args, format);
453   error = g_error_new_valist (domain, code, format, args);
454   va_end (args);
455 
456   return error;
457 }
458 
459 /**
460  * g_error_new_literal:
461  * @domain: error domain
462  * @code: error code
463  * @message: error message
464  *
465  * Creates a new #GError; unlike g_error_new(), @message is
466  * not a printf()-style format string. Use this function if
467  * @message contains text you don't have control over,
468  * that could include printf() escape sequences.
469  *
470  * Returns: a new #GError
471  **/
472 GError*
g_error_new_literal(GQuark domain,gint code,const gchar * message)473 g_error_new_literal (GQuark         domain,
474                      gint           code,
475                      const gchar   *message)
476 {
477   GError* err;
478 
479   g_return_val_if_fail (message != NULL, NULL);
480   g_return_val_if_fail (domain != 0, NULL);
481 
482   err = g_slice_new (GError);
483 
484   err->domain = domain;
485   err->code = code;
486   err->message = g_strdup (message);
487 
488   return err;
489 }
490 
491 /**
492  * g_error_free:
493  * @error: a #GError
494  *
495  * Frees a #GError and associated resources.
496  */
497 void
g_error_free(GError * error)498 g_error_free (GError *error)
499 {
500   g_return_if_fail (error != NULL);
501 
502   g_free (error->message);
503 
504   g_slice_free (GError, error);
505 }
506 
507 /**
508  * g_error_copy:
509  * @error: a #GError
510  *
511  * Makes a copy of @error.
512  *
513  * Returns: a new #GError
514  */
515 GError*
g_error_copy(const GError * error)516 g_error_copy (const GError *error)
517 {
518   GError *copy;
519 
520   g_return_val_if_fail (error != NULL, NULL);
521   /* See g_error_new_valist for why these don't return */
522   g_warn_if_fail (error->domain != 0);
523   g_warn_if_fail (error->message != NULL);
524 
525   copy = g_slice_new (GError);
526 
527   *copy = *error;
528 
529   copy->message = g_strdup (error->message);
530 
531   return copy;
532 }
533 
534 /**
535  * g_error_matches:
536  * @error: (nullable): a #GError
537  * @domain: an error domain
538  * @code: an error code
539  *
540  * Returns %TRUE if @error matches @domain and @code, %FALSE
541  * otherwise. In particular, when @error is %NULL, %FALSE will
542  * be returned.
543  *
544  * If @domain contains a `FAILED` (or otherwise generic) error code,
545  * you should generally not check for it explicitly, but should
546  * instead treat any not-explicitly-recognized error code as being
547  * equivalent to the `FAILED` code. This way, if the domain is
548  * extended in the future to provide a more specific error code for
549  * a certain case, your code will still work.
550  *
551  * Returns: whether @error has @domain and @code
552  */
553 gboolean
g_error_matches(const GError * error,GQuark domain,gint code)554 g_error_matches (const GError *error,
555                  GQuark        domain,
556                  gint          code)
557 {
558   return error &&
559     error->domain == domain &&
560     error->code == code;
561 }
562 
563 #define ERROR_OVERWRITTEN_WARNING "GError set over the top of a previous GError or uninitialized memory.\n" \
564                "This indicates a bug in someone's code. You must ensure an error is NULL before it's set.\n" \
565                "The overwriting error message was: %s"
566 
567 /**
568  * g_set_error:
569  * @err: (out callee-allocates) (optional): a return location for a #GError
570  * @domain: error domain
571  * @code: error code
572  * @format: printf()-style format
573  * @...: args for @format
574  *
575  * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err
576  * must be %NULL. A new #GError is created and assigned to *@err.
577  */
578 void
g_set_error(GError ** err,GQuark domain,gint code,const gchar * format,...)579 g_set_error (GError      **err,
580              GQuark        domain,
581              gint          code,
582              const gchar  *format,
583              ...)
584 {
585   GError *new;
586 
587   va_list args;
588 
589   if (err == NULL)
590     return;
591 
592   va_start (args, format);
593   new = g_error_new_valist (domain, code, format, args);
594   va_end (args);
595 
596   if (*err == NULL)
597     *err = new;
598   else
599     {
600       g_warning (ERROR_OVERWRITTEN_WARNING, new->message);
601       g_error_free (new);
602     }
603 }
604 
605 /**
606  * g_set_error_literal:
607  * @err: (out callee-allocates) (optional): a return location for a #GError
608  * @domain: error domain
609  * @code: error code
610  * @message: error message
611  *
612  * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err
613  * must be %NULL. A new #GError is created and assigned to *@err.
614  * Unlike g_set_error(), @message is not a printf()-style format string.
615  * Use this function if @message contains text you don't have control over,
616  * that could include printf() escape sequences.
617  *
618  * Since: 2.18
619  */
620 void
g_set_error_literal(GError ** err,GQuark domain,gint code,const gchar * message)621 g_set_error_literal (GError      **err,
622                      GQuark        domain,
623                      gint          code,
624                      const gchar  *message)
625 {
626   if (err == NULL)
627     return;
628 
629   if (*err == NULL)
630     *err = g_error_new_literal (domain, code, message);
631   else
632     g_warning (ERROR_OVERWRITTEN_WARNING, message);
633 }
634 
635 /**
636  * g_propagate_error:
637  * @dest: (out callee-allocates) (optional) (nullable): error return location
638  * @src: (transfer full): error to move into the return location
639  *
640  * If @dest is %NULL, free @src; otherwise, moves @src into *@dest.
641  * The error variable @dest points to must be %NULL.
642  *
643  * @src must be non-%NULL.
644  *
645  * Note that @src is no longer valid after this call. If you want
646  * to keep using the same GError*, you need to set it to %NULL
647  * after calling this function on it.
648  */
649 void
g_propagate_error(GError ** dest,GError * src)650 g_propagate_error (GError **dest,
651 		   GError  *src)
652 {
653   g_return_if_fail (src != NULL);
654 
655   if (dest == NULL)
656     {
657       if (src)
658         g_error_free (src);
659       return;
660     }
661   else
662     {
663       if (*dest != NULL)
664         {
665           g_warning (ERROR_OVERWRITTEN_WARNING, src->message);
666           g_error_free (src);
667         }
668       else
669         *dest = src;
670     }
671 }
672 
673 /**
674  * g_clear_error:
675  * @err: a #GError return location
676  *
677  * If @err or *@err is %NULL, does nothing. Otherwise,
678  * calls g_error_free() on *@err and sets *@err to %NULL.
679  */
680 void
g_clear_error(GError ** err)681 g_clear_error (GError **err)
682 {
683   if (err && *err)
684     {
685       g_error_free (*err);
686       *err = NULL;
687     }
688 }
689 
690 G_GNUC_PRINTF(2, 0)
691 static void
g_error_add_prefix(gchar ** string,const gchar * format,va_list ap)692 g_error_add_prefix (gchar       **string,
693                     const gchar  *format,
694                     va_list       ap)
695 {
696   gchar *oldstring;
697   gchar *prefix;
698 
699   prefix = g_strdup_vprintf (format, ap);
700   oldstring = *string;
701   *string = g_strconcat (prefix, oldstring, NULL);
702   g_free (oldstring);
703   g_free (prefix);
704 }
705 
706 /**
707  * g_prefix_error:
708  * @err: (inout) (optional) (nullable): a return location for a #GError
709  * @format: printf()-style format string
710  * @...: arguments to @format
711  *
712  * Formats a string according to @format and prefix it to an existing
713  * error message. If @err is %NULL (ie: no error variable) then do
714  * nothing.
715  *
716  * If *@err is %NULL (ie: an error variable is present but there is no
717  * error condition) then also do nothing.
718  *
719  * Since: 2.16
720  */
721 void
g_prefix_error(GError ** err,const gchar * format,...)722 g_prefix_error (GError      **err,
723                 const gchar  *format,
724                 ...)
725 {
726   if (err && *err)
727     {
728       va_list ap;
729 
730       va_start (ap, format);
731       g_error_add_prefix (&(*err)->message, format, ap);
732       va_end (ap);
733     }
734 }
735 
736 /**
737  * g_propagate_prefixed_error:
738  * @dest: error return location
739  * @src: error to move into the return location
740  * @format: printf()-style format string
741  * @...: arguments to @format
742  *
743  * If @dest is %NULL, free @src; otherwise, moves @src into *@dest.
744  * *@dest must be %NULL. After the move, add a prefix as with
745  * g_prefix_error().
746  *
747  * Since: 2.16
748  **/
749 void
g_propagate_prefixed_error(GError ** dest,GError * src,const gchar * format,...)750 g_propagate_prefixed_error (GError      **dest,
751                             GError       *src,
752                             const gchar  *format,
753                             ...)
754 {
755   g_propagate_error (dest, src);
756 
757   if (dest && *dest)
758     {
759       va_list ap;
760 
761       va_start (ap, format);
762       g_error_add_prefix (&(*dest)->message, format, ap);
763       va_end (ap);
764     }
765 }
766