Coverage report for dbus/dbus-errors.c.gcov

        -:    0:Source:dbus-errors.c
        -:    0:Graph:.libs/dbus-errors.gcno
        -:    0:Data:.libs/dbus-errors.gcda
        -:    0:Runs:11825
        -:    0:Programs:5
        -:    1:/* -*- mode: C; c-file-style: "gnu" -*- */
        -:    2:/* dbus-errors.c Error reporting
        -:    3: *
        -:    4: * Copyright (C) 2002, 2004  Red Hat Inc.
        -:    5: * Copyright (C) 2003  CodeFactory AB
        -:    6: *
        -:    7: * Licensed under the Academic Free License version 2.1
        -:    8: * 
        -:    9: * This program is free software; you can redistribute it and/or modify
        -:   10: * it under the terms of the GNU General Public License as published by
        -:   11: * the Free Software Foundation; either version 2 of the License, or
        -:   12: * (at your option) any later version.
        -:   13: *
        -:   14: * This program is distributed in the hope that it will be useful,
        -:   15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
        -:   16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        -:   17: * GNU General Public License for more details.
        -:   18: * 
        -:   19: * You should have received a copy of the GNU General Public License
        -:   20: * along with this program; if not, write to the Free Software
        -:   21: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
        -:   22: *
        -:   23: */
        -:   24:#include "dbus-errors.h"
        -:   25:#include "dbus-internals.h"
        -:   26:#include "dbus-string.h"
        -:   27:#include "dbus-protocol.h"
        -:   28:#include <stdarg.h>
        -:   29:#include <string.h>
        -:   30:
        -:   31:/**
        -:   32: * @defgroup DBusErrorInternals Error reporting internals
        -:   33: * @ingroup  DBusInternals
        -:   34: * @brief Error reporting internals
        -:   35: * @{
        -:   36: */
        -:   37: 
        -:   38:/**
        -:   39: * Internals of DBusError
        -:   40: */
        -:   41:typedef struct
        -:   42:{
        -:   43:  const char *name; /**< error name */
        -:   44:  char *message; /**< error message */
        -:   45:
        -:   46:  unsigned int const_message : 1; /**< Message is not owned by DBusError */
        -:   47:
        -:   48:  unsigned int dummy2 : 1; /**< placeholder */
        -:   49:  unsigned int dummy3 : 1; /**< placeholder */
        -:   50:  unsigned int dummy4 : 1; /**< placeholder */
        -:   51:  unsigned int dummy5 : 1; /**< placeholder */
        -:   52:
        -:   53:  void *padding1; /**< placeholder */
        -:   54:  
        -:   55:} DBusRealError;
        -:   56:
        -:   57:/**
        -:   58: * Returns a longer message describing an error name.
        -:   59: * If the error name is unknown, returns the name
        -:   60: * itself.
        -:   61: *
        -:   62: * @param error the error to describe
        -:   63: * @returns a constant string describing the error.
        -:   64: */
        -:   65:static const char*
        -:   66:message_from_error (const char *error)
function message_from_error called 1542 returned 100% blocks executed 12%
     1542:   67:{
     1542:   68:  if (strcmp (error, DBUS_ERROR_FAILED) == 0)
call    0 returned 100%
branch  1 taken 0% (fallthrough)
branch  2 taken 100%
    #####:   69:    return "Unknown error";
     1542:   70:  else if (strcmp (error, DBUS_ERROR_NO_MEMORY) == 0)
call    0 returned 100%
branch  1 taken 100% (fallthrough)
branch  2 taken 0%
     1542:   71:    return "Not enough memory available";
    #####:   72:  else if (strcmp (error, DBUS_ERROR_IO_ERROR) == 0)
call    0 never executed
branch  1 never executed
branch  2 never executed
    #####:   73:    return "Error reading or writing data";
    #####:   74:  else if (strcmp (error, DBUS_ERROR_BAD_ADDRESS) == 0)
call    0 never executed
branch  1 never executed
branch  2 never executed
    #####:   75:    return "Could not parse address";
    #####:   76:  else if (strcmp (error, DBUS_ERROR_NOT_SUPPORTED) == 0)
call    0 never executed
branch  1 never executed
branch  2 never executed
    #####:   77:    return "Feature not supported";
    #####:   78:  else if (strcmp (error, DBUS_ERROR_LIMITS_EXCEEDED) == 0)
call    0 never executed
branch  1 never executed
branch  2 never executed
    #####:   79:    return "Resource limits exceeded";
    #####:   80:  else if (strcmp (error, DBUS_ERROR_ACCESS_DENIED) == 0)
call    0 never executed
branch  1 never executed
branch  2 never executed
    #####:   81:    return "Permission denied";
    #####:   82:  else if (strcmp (error, DBUS_ERROR_AUTH_FAILED) == 0)
call    0 never executed
branch  1 never executed
branch  2 never executed
    #####:   83:    return "Could not authenticate to server";
    #####:   84:  else if (strcmp (error, DBUS_ERROR_NO_SERVER) == 0)
call    0 never executed
branch  1 never executed
branch  2 never executed
    #####:   85:    return "No server available at address";
    #####:   86:  else if (strcmp (error, DBUS_ERROR_TIMEOUT) == 0)
call    0 never executed
branch  1 never executed
branch  2 never executed
    #####:   87:    return "Connection timed out";
    #####:   88:  else if (strcmp (error, DBUS_ERROR_NO_NETWORK) == 0)
call    0 never executed
branch  1 never executed
branch  2 never executed
    #####:   89:    return "Network unavailable";
    #####:   90:  else if (strcmp (error, DBUS_ERROR_ADDRESS_IN_USE) == 0)
call    0 never executed
branch  1 never executed
branch  2 never executed
    #####:   91:    return "Address already in use";
    #####:   92:  else if (strcmp (error, DBUS_ERROR_DISCONNECTED) == 0)
call    0 never executed
branch  1 never executed
branch  2 never executed
    #####:   93:    return "Disconnected.";
    #####:   94:  else if (strcmp (error, DBUS_ERROR_INVALID_ARGS) == 0)
call    0 never executed
branch  1 never executed
branch  2 never executed
    #####:   95:    return "Invalid arguments.";
    #####:   96:  else if (strcmp (error, DBUS_ERROR_NO_REPLY) == 0)
call    0 never executed
branch  1 never executed
branch  2 never executed
    #####:   97:    return "Did not get a reply message.";
    #####:   98:  else if (strcmp (error, DBUS_ERROR_FILE_NOT_FOUND) == 0)
call    0 never executed
branch  1 never executed
branch  2 never executed
    #####:   99:    return "File doesn't exist.";
        -:  100:  else
    #####:  101:    return error;
        -:  102:}
        -:  103:
        -:  104:/** @} */ /* End of internals */
        -:  105:
        -:  106:/**
        -:  107: * @defgroup DBusErrors Error reporting
        -:  108: * @ingroup  DBus
        -:  109: * @brief Error reporting
        -:  110: *
        -:  111: * Types and functions related to reporting errors.
        -:  112: *
        -:  113: *
        -:  114: * In essence D-BUS error reporting works as follows:
        -:  115: *
        -:  116: * @code
        -:  117: * DBusError error;
        -:  118: * dbus_error_init (&error);
        -:  119: * dbus_some_function (arg1, arg2, &error);
        -:  120: * if (dbus_error_is_set (&error))
        -:  121: *   {
        -:  122: *     fprintf (stderr, "an error occurred: %s\n", error.message);
        -:  123: *     dbus_error_free (&error);
        -:  124: *   }
        -:  125: * @endcode
        -:  126: *
        -:  127: * There are some rules. An error passed to a D-BUS function must
        -:  128: * always be unset; you can't pass in an error that's already set.  If
        -:  129: * a function has a return code indicating whether an error occurred,
        -:  130: * and also a #DBusError parameter, then the error will always be set
        -:  131: * if and only if the return code indicates an error occurred. i.e.
        -:  132: * the return code and the error are never going to disagree.
        -:  133: *
        -:  134: * An error only needs to be freed if it's been set, not if
        -:  135: * it's merely been initialized.
        -:  136: *
        -:  137: * You can check the specific error that occurred using
        -:  138: * dbus_error_has_name().
        -:  139: * 
        -:  140: * @{
        -:  141: */
        -:  142:
        -:  143:/**
        -:  144: * Initializes a DBusError structure. Does not allocate
        -:  145: * any memory; the error only needs to be freed
        -:  146: * if it is set at some point. 
        -:  147: *
        -:  148: * @param error the DBusError.
        -:  149: */
        -:  150:void
        -:  151:dbus_error_init (DBusError *error)
function dbus_error_init called 320694 returned 100% blocks executed 71%
   320694:  152:{
        -:  153:  DBusRealError *real;
        -:  154:
   320694:  155:  _dbus_return_if_fail (error != NULL);
call    0 returned 100%
branch  1 taken 0% (fallthrough)
branch  2 taken 100%
call    3 never executed
call    4 never executed
        -:  156:
   320694:  157:  _dbus_assert (sizeof (DBusError) == sizeof (DBusRealError));
call    0 returned 100%
        -:  158:
   320694:  159:  real = (DBusRealError *)error;
        -:  160:  
   320694:  161:  real->name = NULL;  
   320694:  162:  real->message = NULL;
        -:  163:
   320694:  164:  real->const_message = TRUE;
        -:  165:}
        -:  166:
        -:  167:/**
        -:  168: * Frees an error that's been set (or just initialized),
        -:  169: * then reinitializes the error as in dbus_error_init().
        -:  170: *
        -:  171: * @param error memory where the error is stored.
        -:  172: */
        -:  173:void
        -:  174:dbus_error_free (DBusError *error)
function dbus_error_free called 100739 returned 100% blocks executed 78%
   100739:  175:{
        -:  176:  DBusRealError *real;
        -:  177:
   100739:  178:  _dbus_return_if_fail (error != NULL);
call    0 returned 100%
branch  1 taken 0% (fallthrough)
branch  2 taken 100%
call    3 never executed
call    4 never executed
        -:  179:  
   100739:  180:  real = (DBusRealError *)error;
        -:  181:
   100739:  182:  if (!real->const_message)
branch  0 taken 27% (fallthrough)
branch  1 taken 73%
        -:  183:    {
    27407:  184:      dbus_free (real->name);
call    0 returned 100%
    27407:  185:      dbus_free (real->message);
call    0 returned 100%
        -:  186:    }
        -:  187:
   100739:  188:  dbus_error_init (error);
call    0 returned 100%
        -:  189:}
        -:  190:
        -:  191:/**
        -:  192: * Assigns an error name and message to a DBusError.  Does nothing if
        -:  193: * error is #NULL. The message may be NULL, which means a default
        -:  194: * message will be deduced from the name. If the error name is unknown
        -:  195: * to D-BUS the default message will be totally useless, though.
        -:  196: *
        -:  197: * @todo should be called dbus_error_set_const() 
        -:  198: * 
        -:  199: * @param error the error.
        -:  200: * @param name the error name (not copied!!!)
        -:  201: * @param message the error message (not copied!!!)
        -:  202: */
        -:  203:void
        -:  204:dbus_set_error_const (DBusError  *error,
        -:  205:		      const char *name,
        -:  206:		      const char *message)
function dbus_set_error_const called 9112 returned 100% blocks executed 74%
     9112:  207:{
        -:  208:  DBusRealError *real;
        -:  209:
     9112:  210:  _dbus_return_if_error_is_set (error);
call    0 returned 100%
branch  1 taken 100% (fallthrough)
branch  2 taken 0%
call    3 returned 100%
branch  4 taken 0% (fallthrough)
branch  5 taken 100%
call    6 never executed
call    7 never executed
     9112:  211:  _dbus_return_if_fail (name != NULL);
call    0 returned 100%
branch  1 taken 0% (fallthrough)
branch  2 taken 100%
call    3 never executed
call    4 never executed
        -:  212:  
     9112:  213:  if (error == NULL)
branch  0 taken 0% (fallthrough)
branch  1 taken 100%
    #####:  214:    return;
        -:  215:
     9112:  216:  _dbus_assert (error->name == NULL);
call    0 returned 100%
     9112:  217:  _dbus_assert (error->message == NULL);
call    0 returned 100%
        -:  218:
     9112:  219:  if (message == NULL)
branch  0 taken 1% (fallthrough)
branch  1 taken 99%
       95:  220:    message = message_from_error (name);
call    0 returned 100%
        -:  221:  
     9112:  222:  real = (DBusRealError *)error;
        -:  223:  
     9112:  224:  real->name = name;
     9112:  225:  real->message = (char *)message;
     9112:  226:  real->const_message = TRUE;
        -:  227:}
        -:  228:
        -:  229:/**
        -:  230: * Moves an error src into dest, freeing src and
        -:  231: * overwriting dest. Both src and dest must be initialized.
        -:  232: * src is reinitialized to an empty error. dest may not
        -:  233: * contain an existing error. If the destination is
        -:  234: * #NULL, just frees and reinits the source error.
        -:  235: * 
        -:  236: * @param src the source error
        -:  237: * @param dest the destination error or #NULL
        -:  238: */
        -:  239:void
        -:  240:dbus_move_error (DBusError *src,
        -:  241:                 DBusError *dest)
function dbus_move_error called 1530 returned 100% blocks executed 73%
     1530:  242:{
     1530:  243:  _dbus_return_if_error_is_set (dest);
call    0 returned 100%
branch  1 taken 100% (fallthrough)
branch  2 taken 0%
call    3 returned 100%
branch  4 taken 0% (fallthrough)
branch  5 taken 100%
call    6 never executed
call    7 never executed
        -:  244:
     1530:  245:  if (dest)
branch  0 taken 100% (fallthrough)
branch  1 taken 0%
        -:  246:    {
     1530:  247:      dbus_error_free (dest);
call    0 returned 100%
     1530:  248:      *dest = *src;
     1530:  249:      dbus_error_init (src);
call    0 returned 100%
        -:  250:    }
        -:  251:  else
    #####:  252:    dbus_error_free (src);
call    0 never executed
        -:  253:}
        -:  254:
        -:  255:/**
        -:  256: * Checks whether the error is set and has the given
        -:  257: * name.
        -:  258: * @param error the error
        -:  259: * @param name the name
        -:  260: * @returns #TRUE if the given named error occurred
        -:  261: */
        -:  262:dbus_bool_t
        -:  263:dbus_error_has_name (const DBusError *error,
        -:  264:                     const char      *name)
function dbus_error_has_name called 47782 returned 100% blocks executed 71%
    47782:  265:{
    47782:  266:  _dbus_return_val_if_fail (error != NULL, FALSE);
call    0 returned 100%
branch  1 taken 0% (fallthrough)
branch  2 taken 100%
call    3 never executed
call    4 never executed
    47782:  267:  _dbus_return_val_if_fail (name != NULL, FALSE);
call    0 returned 100%
branch  1 taken 0% (fallthrough)
branch  2 taken 100%
call    3 never executed
call    4 never executed
        -:  268:
    47782:  269:  _dbus_assert ((error->name != NULL && error->message != NULL) ||
branch  0 taken 68% (fallthrough)
branch  1 taken 32%
branch  2 taken 0% (fallthrough)
branch  3 taken 100%
branch  4 taken 100% (fallthrough)
branch  5 taken 0%
branch  6 taken 100% (fallthrough)
branch  7 taken 0%
call    8 returned 100%
        -:  270:                (error->name == NULL && error->message == NULL));
        -:  271:  
    47782:  272:  if (error->name != NULL)
branch  0 taken 68% (fallthrough)
branch  1 taken 32%
        -:  273:    {
        -:  274:      DBusString str1, str2;
    32562:  275:      _dbus_string_init_const (&str1, error->name);
call    0 returned 100%
    32562:  276:      _dbus_string_init_const (&str2, name);
call    0 returned 100%
    32562:  277:      return _dbus_string_equal (&str1, &str2);
call    0 returned 100%
        -:  278:    }
        -:  279:  else
    15220:  280:    return FALSE;
        -:  281:}
        -:  282:
        -:  283:/**
        -:  284: * Checks whether an error occurred (the error is set).
        -:  285: *
        -:  286: * @param error the error object
        -:  287: * @returns #TRUE if an error occurred
        -:  288: */
        -:  289:dbus_bool_t
        -:  290:dbus_error_is_set (const DBusError *error)
function dbus_error_is_set called 783718 returned 100% blocks executed 71%
   783718:  291:{
   783718:  292:  _dbus_return_val_if_fail (error != NULL, FALSE);  
call    0 returned 100%
branch  1 taken 0% (fallthrough)
branch  2 taken 100%
call    3 never executed
call    4 never executed
   783718:  293:  _dbus_assert ((error->name != NULL && error->message != NULL) ||
branch  0 taken 8% (fallthrough)
branch  1 taken 92%
branch  2 taken 0% (fallthrough)
branch  3 taken 100%
branch  4 taken 100% (fallthrough)
branch  5 taken 0%
branch  6 taken 100% (fallthrough)
branch  7 taken 0%
call    8 returned 100%
        -:  294:                (error->name == NULL && error->message == NULL));
   783718:  295:  return error->name != NULL;
        -:  296:}
        -:  297:
        -:  298:/**
        -:  299: * Assigns an error name and message to a DBusError.
        -:  300: * Does nothing if error is #NULL.
        -:  301: *
        -:  302: * The format may be NULL, which means a default message will be
        -:  303: * deduced from the name. If the error name is unknown to D-BUS the
        -:  304: * default message will be totally useless, though.
        -:  305: *
        -:  306: * If no memory can be allocated for the error message, 
        -:  307: * an out-of-memory error message will be set instead.
        -:  308: *
        -:  309: * @todo should be called dbus_error_set()
        -:  310: *
        -:  311: * @param error the error.
        -:  312: * @param name the error name
        -:  313: * @param format printf-style format string.
        -:  314: */
        -:  315:void
        -:  316:dbus_set_error (DBusError  *error,
        -:  317:		const char *name,
        -:  318:		const char *format,
        -:  319:		...)
function dbus_set_error called 67356 returned 100% blocks executed 86%
    67356:  320:{
        -:  321:  DBusRealError *real;
        -:  322:  DBusString str;
        -:  323:  va_list args;
        -:  324:  
    67356:  325:  if (error == NULL)
branch  0 taken 57% (fallthrough)
branch  1 taken 43%
    38124:  326:    return;
        -:  327:
        -:  328:  /* it's a bug to pile up errors */
    29232:  329:  _dbus_return_if_error_is_set (error);
call    0 returned 100%
branch  1 taken 100% (fallthrough)
branch  2 taken 0%
call    3 returned 100%
branch  4 taken 0% (fallthrough)
branch  5 taken 100%
call    6 never executed
call    7 never executed
    29232:  330:  _dbus_return_if_fail (name != NULL);
call    0 returned 100%
branch  1 taken 0% (fallthrough)
branch  2 taken 100%
call    3 never executed
call    4 never executed
        -:  331:  
    29232:  332:  _dbus_assert (error->name == NULL);
call    0 returned 100%
    29232:  333:  _dbus_assert (error->message == NULL);
call    0 returned 100%
        -:  334:
    29232:  335:  if (!_dbus_string_init (&str))
call    0 returned 100%
branch  1 taken 95% (fallthrough)
branch  2 taken 5%
     1593:  336:    goto nomem;
        -:  337:  
    27639:  338:  if (format == NULL)
branch  0 taken 5% (fallthrough)
branch  1 taken 95%
        -:  339:    {
     1447:  340:      if (!_dbus_string_append (&str,
call    0 returned 100%
call    1 returned 100%
branch  2 taken 0% (fallthrough)
branch  3 taken 100%
        -:  341:                                message_from_error (name)))
        -:  342:        {
    #####:  343:          _dbus_string_free (&str);
call    0 never executed
    #####:  344:          goto nomem;
        -:  345:        }
        -:  346:    }
        -:  347:  else
        -:  348:    {
    26192:  349:      va_start (args, format);
call    0 returned 100%
    26192:  350:      if (!_dbus_string_append_printf_valist (&str, format, args))
call    0 returned 100%
branch  1 taken 1% (fallthrough)
branch  2 taken 99%
        -:  351:        {
       78:  352:          _dbus_string_free (&str);
call    0 returned 100%
       78:  353:          goto nomem;
        -:  354:        }
    26114:  355:      va_end (args);
call    0 returned 100%
        -:  356:    }
        -:  357:
    27561:  358:  real = (DBusRealError *)error;
        -:  359:
    27561:  360:  if (!_dbus_string_steal_data (&str, &real->message))
call    0 returned 100%
branch  1 taken 1% (fallthrough)
branch  2 taken 99%
        -:  361:    {
       76:  362:      _dbus_string_free (&str);
call    0 returned 100%
       76:  363:      goto nomem;
        -:  364:    }
    27485:  365:  _dbus_string_free (&str);
call    0 returned 100%
        -:  366:  
    27485:  367:  real->name = _dbus_strdup (name);
call    0 returned 100%
    27485:  368:  if (real->name == NULL)
branch  0 taken 1% (fallthrough)
branch  1 taken 99%
        -:  369:    {
       78:  370:      dbus_free (real->message);
call    0 returned 100%
       78:  371:      real->message = NULL;
       78:  372:      goto nomem;
        -:  373:    }
    27407:  374:  real->const_message = FALSE;
        -:  375:
    27407:  376:  return;
        -:  377:  
     1825:  378: nomem:
     1825:  379:  _DBUS_SET_OOM (error);
call    0 returned 100%
        -:  380:}
        -:  381:
        -:  382:/** @} */ /* End public API */