* configure.in: add -Wdeclaration-after-statement
[platform/upstream/dbus.git] / dbus / dbus-errors.c
index f7b2f74..fb23085 100644 (file)
@@ -1,10 +1,10 @@
 /* -*- mode: C; c-file-style: "gnu" -*- */
 /* dbus-errors.c Error reporting
  *
- * Copyright (C) 2002  Red Hat Inc.
+ * Copyright (C) 2002, 2004  Red Hat Inc.
  * Copyright (C) 2003  CodeFactory AB
  *
- * Licensed under the Academic Free License version 1.2
+ * Licensed under the Academic Free License version 2.1
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -24,6 +24,7 @@
 #include "dbus-errors.h"
 #include "dbus-internals.h"
 #include "dbus-string.h"
+#include "dbus-protocol.h"
 #include <stdarg.h>
 #include <string.h>
 
@@ -39,7 +40,7 @@
  */
 typedef struct
 {
-  const char *name; /**< error name */
+  char *name; /**< error name */
   char *message; /**< error message */
 
   unsigned int const_message : 1; /**< Message is not owned by DBusError */
@@ -91,7 +92,7 @@ message_from_error (const char *error)
   else if (strcmp (error, DBUS_ERROR_DISCONNECTED) == 0)
     return "Disconnected.";
   else if (strcmp (error, DBUS_ERROR_INVALID_ARGS) == 0)
-    return "Invalid argumemts.";
+    return "Invalid arguments.";
   else if (strcmp (error, DBUS_ERROR_NO_REPLY) == 0)
     return "Did not get a reply message.";
   else if (strcmp (error, DBUS_ERROR_FILE_NOT_FOUND) == 0)
@@ -110,7 +111,7 @@ message_from_error (const char *error)
  * Types and functions related to reporting errors.
  *
  *
- * In essence D-BUS error reporting works as follows:
+ * In essence D-Bus error reporting works as follows:
  *
  * @code
  * DBusError error;
@@ -123,7 +124,7 @@ message_from_error (const char *error)
  *   }
  * @endcode
  *
- * There are some rules. An error passed to a D-BUS function must
+ * There are some rules. An error passed to a D-Bus function must
  * always be unset; you can't pass in an error that's already set.  If
  * a function has a return code indicating whether an error occurred,
  * and also a #DBusError parameter, then the error will always be set
@@ -179,7 +180,10 @@ dbus_error_free (DBusError *error)
   real = (DBusRealError *)error;
 
   if (!real->const_message)
-    dbus_free (real->message);
+    {
+      dbus_free (real->name);
+      dbus_free (real->message);
+    }
 
   dbus_error_init (error);
 }
@@ -188,10 +192,8 @@ dbus_error_free (DBusError *error)
  * Assigns an error name and message to a DBusError.  Does nothing if
  * error is #NULL. The message may be NULL, which means a default
  * message will be deduced from the name. If the error name is unknown
- * to D-BUS the default message will be totally useless, though.
+ * to D-Bus the default message will be totally useless, though.
  *
- * @todo should be called dbus_error_set_const() 
- * 
  * @param error the error.
  * @param name the error name (not copied!!!)
  * @param message the error message (not copied!!!)
@@ -217,7 +219,7 @@ dbus_set_error_const (DBusError  *error,
   
   real = (DBusRealError *)error;
   
-  real->name = name;
+  real->name = (char*) name;
   real->message = (char *)message;
   real->const_message = TRUE;
 }
@@ -296,16 +298,14 @@ dbus_error_is_set (const DBusError *error)
  * Does nothing if error is #NULL.
  *
  * The format may be NULL, which means a default message will be
- * deduced from the name. If the error name is unknown to D-BUS the
+ * deduced from the name. If the error name is unknown to D-Bus the
  * default message will be totally useless, though.
  *
  * If no memory can be allocated for the error message, 
  * an out-of-memory error message will be set instead.
  *
- * @todo should be called dbus_error_set()
- *
  * @param error the error.
- * @param name the error name (not copied!!!)
+ * @param name the error name
  * @param format printf-style format string.
  */
 void
@@ -358,16 +358,21 @@ dbus_set_error (DBusError  *error,
       _dbus_string_free (&str);
       goto nomem;
     }
+  _dbus_string_free (&str);
   
-  real->name = name;
+  real->name = _dbus_strdup (name);
+  if (real->name == NULL)
+    {
+      dbus_free (real->message);
+      real->message = NULL;
+      goto nomem;
+    }
   real->const_message = FALSE;
 
-  _dbus_string_free (&str);
-
   return;
   
  nomem:
-  dbus_set_error_const (error, DBUS_ERROR_NO_MEMORY, NULL);      
+  _DBUS_SET_OOM (error);
 }
 
 /** @} */ /* End public API */