Added checks and debug messages for failed malloc.

git-svn-id: http://yate.null.ro/svn/yate/trunk@215 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
paulc 2005-01-17 23:38:12 +00:00
parent 26f40169e3
commit 93e54b3442
2 changed files with 76 additions and 28 deletions

View File

@ -119,11 +119,15 @@ DataBlock& DataBlock::assign(void *value, unsigned int len, bool copyData)
if (len) {
if (copyData) {
void *data = ::malloc(len);
if (value)
::memcpy(data,value,len);
if (data) {
if (value)
::memcpy(data,value,len);
else
::memset(data,0,len);
m_data = data;
}
else
::memset(data,0,len);
m_data = data;
Debug("DataBlock",DebugFail,"malloc(%d) returned NULL!",len);
}
else
m_data = value;
@ -173,9 +177,13 @@ void DataBlock::append(const DataBlock &value)
if (value.length()) {
unsigned int len = m_length+value.length();
void *data = ::malloc(len);
::memcpy(data,m_data,m_length);
::memcpy(m_length+(char*)data,value.data(),value.length());
assign(data,len,false);
if (data) {
::memcpy(data,m_data,m_length);
::memcpy(m_length+(char*)data,value.data(),value.length());
assign(data,len,false);
}
else
Debug("DataBlock",DebugFail,"malloc(%d) returned NULL!",len);
}
}
else
@ -188,9 +196,13 @@ void DataBlock::append(const String &value)
if (value.length()) {
unsigned int len = m_length+value.length();
void *data = ::malloc(len);
::memcpy(data,m_data,m_length);
::memcpy(m_length+(char*)data,value.safe(),value.length());
assign(data,len,false);
if (data) {
::memcpy(data,m_data,m_length);
::memcpy(m_length+(char*)data,value.safe(),value.length());
assign(data,len,false);
}
else
Debug("DataBlock",DebugFail,"malloc(%d) returned NULL!",len);
}
}
else
@ -204,9 +216,13 @@ void DataBlock::insert(const DataBlock &value)
if (vl) {
unsigned int len = m_length+vl;
void *data = ::malloc(len);
::memcpy(data,value.data(),vl);
::memcpy(vl+(char*)data,m_data,m_length);
assign(data,len,false);
if (data) {
::memcpy(data,value.data(),vl);
::memcpy(vl+(char*)data,m_data,m_length);
assign(data,len,false);
}
else
Debug("DataBlock",DebugFail,"malloc(%d) returned NULL!",len);
}
}
else

View File

@ -165,6 +165,8 @@ String::String(const String &value)
DDebug(DebugAll,"String::String(%p) [%p]",&value,this);
if (!value.null()) {
m_string = ::strdup(value.c_str());
if (!m_string)
Debug("String",DebugFail,"strdup() returned NULL!");
changed();
}
}
@ -175,8 +177,12 @@ String::String(char value, unsigned int repeat)
DDebug(DebugAll,"String::String('%c',%d) [%p]",value,repeat,this);
if (value && repeat) {
m_string = (char *) ::malloc(repeat+1);
::memset(m_string,value,repeat);
m_string[repeat] = 0;
if (m_string) {
::memset(m_string,value,repeat);
m_string[repeat] = 0;
}
else
Debug("String",DebugFail,"malloc(%d) returned NULL!",repeat+1);
changed();
}
}
@ -188,6 +194,8 @@ String::String(int value)
char buf[64];
::sprintf(buf,"%d",value);
m_string = ::strdup(buf);
if (!m_string)
Debug("String",DebugFail,"strdup() returned NULL!");
changed();
}
@ -198,6 +206,8 @@ String::String(unsigned int value)
char buf[64];
::sprintf(buf,"%u",value);
m_string = ::strdup(buf);
if (!m_string)
Debug("String",DebugFail,"strdup() returned NULL!");
changed();
}
@ -206,6 +216,8 @@ String::String(bool value)
{
DDebug(DebugAll,"String::String(%u) [%p]",value,this);
m_string = ::strdup(value ? "true" : "false");
if (!m_string)
Debug("String",DebugFail,"strdup() returned NULL!");
changed();
}
@ -215,6 +227,8 @@ String::String(const String *value)
DDebug(DebugAll,"String::String(%p) [%p]",&value,this);
if (value && !value->null()) {
m_string = ::strdup(value->c_str());
if (!m_string)
Debug("String",DebugFail,"strdup() returned NULL!");
changed();
}
}
@ -243,13 +257,17 @@ String& String::assign(const char *value, int len)
len = vlen;
if (value != m_string || len != (int)m_length) {
char *data = (char *) ::malloc(len+1);
::memcpy(data,value,len);
data[len] = 0;
char *odata = m_string;
m_string = data;
changed();
if (odata)
::free(odata);
if (data) {
::memcpy(data,value,len);
data[len] = 0;
char *odata = m_string;
m_string = data;
changed();
if (odata)
::free(odata);
}
else
Debug("String",DebugFail,"malloc(%d) returned NULL!",len+1);
}
}
else
@ -375,6 +393,8 @@ String& String::operator=(const char *value)
if (value != c_str()) {
char *tmp = m_string;
m_string = value ? ::strdup(value) : 0;
if (value && *value && !m_string)
Debug("String",DebugFail,"strdup() returned NULL!");
changed();
if (tmp)
::free(tmp);
@ -386,15 +406,23 @@ String& String::operator+=(const char *value)
{
if (value && *value) {
if (m_string) {
int len = ::strlen(value)+length();
char *tmp1 = m_string;
char *tmp2 = (char *) ::malloc(::strlen(value)+length()+1);
::strcpy(tmp2,m_string);
::strcat(tmp2,value);
m_string = tmp2;
::free(tmp1);
char *tmp2 = (char *) ::malloc(len+1);
if (tmp2) {
::strcpy(tmp2,m_string);
::strcat(tmp2,value);
m_string = tmp2;
::free(tmp1);
}
else
Debug("String",DebugFail,"malloc(%d) returned NULL!",len+1);
}
else
else {
m_string = ::strdup(value);
if (!m_string)
Debug("String",DebugFail,"strdup() returned NULL!");
}
changed();
}
return *this;
@ -819,6 +847,10 @@ bool Regexp::compile()
DDebug(DebugInfo,"Regexp::compile()");
if (c_str() && !m_regexp) {
regex_t *data = (regex_t *) ::malloc(sizeof(regex_t));
if (!data) {
Debug("Regexp",DebugFail,"malloc(%d) returned NULL!",sizeof(regex_t));
return false;
}
if (::regcomp(data,c_str(),0)) {
Debug(DebugWarn,"Regexp::compile() \"%s\" failed",c_str());
::regfree(data);