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