Line data Source code
1 : #include "wibmod/WIB1/BUException/ExceptionBase.hh"
2 :
3 : // Error string for bad allocation of description
4 : static const char descriptionError[] = "Description allocation failed.\n";
5 :
6 0 : BUException::exBase::exBase() throw()
7 0 : : descriptionSize(255)
8 0 : , stackSize(1024)
9 0 : , PID(-1)
10 : {
11 : // Allocate memory (throw if failed)
12 0 : stackBuffer = (char*)malloc(stackSize + 1);
13 0 : descriptionBuffer = (char*)malloc(descriptionSize + 1);
14 :
15 0 : descriptionUsed = 0;
16 0 : stackUsed = 0;
17 :
18 : // Null terminate the strings to empty
19 : // also make the char after the nominal end of string null terminated
20 0 : if (descriptionBuffer != NULL) {
21 0 : descriptionBuffer[0] = '\0';
22 0 : descriptionBuffer[descriptionSize] = '\0';
23 : } else {
24 0 : descriptionSize = 0;
25 : }
26 0 : if (stackBuffer != NULL) {
27 0 : stackBuffer[0] = '\0';
28 0 : stackBuffer[stackSize] = '\0';
29 : } else {
30 0 : stackSize = 0;
31 : }
32 :
33 0 : GenerateStackTrace();
34 0 : }
35 :
36 0 : BUException::exBase::~exBase() throw()
37 : {
38 0 : if (stackBuffer != NULL)
39 0 : free(stackBuffer);
40 0 : if (descriptionBuffer != NULL)
41 0 : free(descriptionBuffer);
42 0 : }
43 :
44 : void
45 0 : BUException::exBase::Copy(const exBase& rh) throw()
46 : {
47 : // Copy description buffer
48 0 : this->descriptionUsed = 0;
49 0 : this->descriptionSize = rh.descriptionSize;
50 : // Check if the allocation had worked for rh
51 0 : if (rh.descriptionBuffer == NULL) {
52 : // It didn't work, make sure our copy is also NULL
53 0 : this->descriptionBuffer = NULL;
54 : } else {
55 : // rh has a valid description, build ours
56 0 : this->descriptionBuffer = (char*)malloc(descriptionSize + 1);
57 0 : if (this->descriptionBuffer != NULL) {
58 0 : memcpy(this->descriptionBuffer, rh.descriptionBuffer, descriptionSize);
59 0 : this->descriptionUsed = rh.descriptionUsed;
60 : }
61 : }
62 :
63 : // Copy stack trace buffer
64 0 : this->stackUsed = 0;
65 0 : this->stackSize = rh.stackSize;
66 : // Check if the allocation worked for rh
67 0 : if (rh.stackBuffer == NULL) {
68 : // It didn't work, make sure our copy is also NULL
69 0 : this->stackBuffer = NULL;
70 : } else { // rh was valid, so construct ours
71 0 : this->stackBuffer = (char*)malloc(stackSize + 1);
72 0 : if (this->stackBuffer != NULL) {
73 0 : memcpy(this->stackBuffer, rh.stackBuffer, stackSize);
74 0 : this->stackUsed = rh.stackUsed;
75 : }
76 : }
77 :
78 : // Copy PID
79 0 : this->PID = rh.PID;
80 0 : }
81 :
82 : void
83 0 : BUException::exBase::Append(const char* buffer) throw()
84 : {
85 : // Compute the appended buffer's size
86 0 : size_t appendedSize = strlen(buffer);
87 :
88 : // Make sure we stay in the bounds of our buffer
89 0 : if (appendedSize > (descriptionSize - descriptionUsed)) {
90 : appendedSize = (descriptionSize - descriptionUsed);
91 : }
92 :
93 : // Append the new string (n keeps us safe from buffer overflow)
94 0 : strncpy(descriptionBuffer + descriptionUsed, buffer, appendedSize);
95 :
96 : // update the description size
97 0 : descriptionUsed += appendedSize;
98 :
99 : // enforce the null terminator
100 : //(the buffer is one larger than the size so we always have space for a NULL)
101 : // This is untrue if malloc failed initially, so we watch out for that.
102 0 : if (descriptionBuffer != NULL)
103 0 : descriptionBuffer[descriptionUsed] = '\0';
104 0 : }
105 :
106 : const char*
107 0 : BUException::exBase::Description() const throw()
108 : {
109 0 : if (descriptionBuffer == NULL) // If there was a bad alloc, return an error string
110 0 : return descriptionError;
111 : return descriptionBuffer;
112 : }
|