error.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Copyright information is at end of file */
  2. #define _XOPEN_SOURCE 600 /* Make sure strdup() is in <string.h> */
  3. #include "xmlrpc_config.h"
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdarg.h>
  8. #include "xmlrpc-c/util_int.h"
  9. #include "xmlrpc-c/string_int.h"
  10. #include "xmlrpc-c/util.h"
  11. void
  12. xmlrpc_assertion_failed(const char * const fileName,
  13. int const lineNumber) {
  14. fprintf(stderr, "%s:%d: assertion failed\n", fileName, lineNumber);
  15. abort();
  16. }
  17. static const char * const default_fault_string =
  18. "Not enough memory for error message";
  19. void xmlrpc_env_init (xmlrpc_env* env)
  20. {
  21. XMLRPC_ASSERT(env != NULL);
  22. env->fault_occurred = 0;
  23. env->fault_code = 0;
  24. env->fault_string = NULL;
  25. }
  26. void
  27. xmlrpc_env_clean(xmlrpc_env * const envP) {
  28. XMLRPC_ASSERT(envP != NULL);
  29. XMLRPC_ASSERT(envP->fault_string != XMLRPC_BAD_POINTER);
  30. /* env->fault_string may be one of three things:
  31. ** 1) a NULL pointer
  32. ** 2) a pointer to the default_fault_string
  33. ** 3) a pointer to a malloc'd fault string
  34. ** If we have case (3), we'll need to free it. */
  35. if (envP->fault_string && envP->fault_string != default_fault_string)
  36. free(envP->fault_string);
  37. envP->fault_string = XMLRPC_BAD_POINTER;
  38. }
  39. void
  40. xmlrpc_env_set_fault(xmlrpc_env * const envP,
  41. int const faultCode,
  42. const char * const faultDescription) {
  43. char * buffer;
  44. XMLRPC_ASSERT(envP != NULL);
  45. XMLRPC_ASSERT(faultDescription != NULL);
  46. /* Clean up any leftover pointers. */
  47. xmlrpc_env_clean(envP);
  48. envP->fault_occurred = 1;
  49. envP->fault_code = faultCode;
  50. /* Try to copy the fault string. If this fails, use a default. */
  51. buffer = strdup(faultDescription);
  52. if (buffer == NULL)
  53. envP->fault_string = (char *)default_fault_string;
  54. else {
  55. xmlrpc_force_to_utf8(buffer);
  56. xmlrpc_force_to_xml_chars(buffer);
  57. envP->fault_string = buffer;
  58. }
  59. }
  60. void
  61. xmlrpc_set_fault_formatted_v(xmlrpc_env * const envP,
  62. int const code,
  63. const char * const format,
  64. va_list args) {
  65. const char * faultDescription;
  66. xmlrpc_vasprintf(&faultDescription, format, args);
  67. xmlrpc_env_set_fault(envP, code, faultDescription);
  68. xmlrpc_strfree(faultDescription);
  69. }
  70. void
  71. xmlrpc_env_set_fault_formatted(xmlrpc_env * const envP,
  72. int const code,
  73. const char * const format,
  74. ...) {
  75. va_list args;
  76. XMLRPC_ASSERT(envP != NULL);
  77. XMLRPC_ASSERT(format != NULL);
  78. /* Print our error message to the buffer. */
  79. va_start(args, format);
  80. xmlrpc_set_fault_formatted_v(envP, code, format, args);
  81. va_end(args);
  82. }
  83. void
  84. xmlrpc_faultf(xmlrpc_env * const envP,
  85. const char * const format,
  86. ...) {
  87. va_list args;
  88. XMLRPC_ASSERT(envP != NULL);
  89. XMLRPC_ASSERT(format != NULL);
  90. /* Print our error message to the buffer. */
  91. va_start(args, format);
  92. xmlrpc_set_fault_formatted_v(envP, XMLRPC_INTERNAL_ERROR, format, args);
  93. va_end(args);
  94. }
  95. /* Copyright (C) 2001 by First Peer, Inc. All rights reserved.
  96. **
  97. ** Redistribution and use in source and binary forms, with or without
  98. ** modification, are permitted provided that the following conditions
  99. ** are met:
  100. ** 1. Redistributions of source code must retain the above copyright
  101. ** notice, this list of conditions and the following disclaimer.
  102. ** 2. Redistributions in binary form must reproduce the above copyright
  103. ** notice, this list of conditions and the following disclaimer in the
  104. ** documentation and/or other materials provided with the distribution.
  105. ** 3. The name of the author may not be used to endorse or promote products
  106. ** derived from this software without specific prior written permission.
  107. **
  108. ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  109. ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  110. ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  111. ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  112. ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  113. ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  114. ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  115. ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  116. ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  117. ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  118. ** SUCH DAMAGE. */