From c327dc15508c21d4e221928f57080025befe3dac Mon Sep 17 00:00:00 2001 From: arjen Date: Sat, 29 Mar 2003 07:18:54 +0000 Subject: [PATCH] String constructor and assignment from char * are more robust fro NULL pointers. --- src/string.cpp | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/string.cpp b/src/string.cpp index 1bcded9..c1cb1b8 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -5,7 +5,7 @@ *********************** ** FILE NAME : string.cpp ** SYSTEM NAME : Andromeda X-Windows Encapsulation -** VERSION NUMBER : $Revision: 1.3 $ +** VERSION NUMBER : $Revision: 1.4 $ ** ** DESCRIPTION : String class implementation. ** @@ -23,7 +23,10 @@ /***************************** $Log: string.cpp,v $ - Revision 1.3 2002-11-03 13:18:57 arjen + Revision 1.4 2003-03-29 07:18:54 arjen + String constructor and assignment from char * are more robust fro NULL pointers. + + Revision 1.3 2002/11/03 13:18:57 arjen New functions - String::escape() and String::unescape() Revision 1.2 2002/09/28 06:42:11 arjen @@ -34,7 +37,7 @@ *****************************/ -static const char RCSID[] = "$Id: string.cpp,v 1.3 2002-11-03 13:18:57 arjen Exp $"; +static const char RCSID[] = "$Id: string.cpp,v 1.4 2003-03-29 07:18:54 arjen Exp $"; #include #include @@ -61,9 +64,16 @@ String::String(char c) // Create a String from a char String::String(const char *s) // Create a String from a char * { p = new srep; - p->s = new char[strlen(s)+1]; - strcpy(p->s, s); + + p->s = 0; p->n = 1; + + if (s != 0) + { + p->s = new char[strlen(s)+1]; + strcpy(p->s, s); + p->n = 1; + } } String::String(const String& x) // Create a String from another String @@ -122,9 +132,12 @@ String& String::operator=(const char *s) else if (p->n == 1) delete p->s; - p->s = new char[strlen(s)+1]; - strcpy(p->s, s); - p->n = 1; + if (s != 0) + { + p->s = new char[strlen(s)+1]; + strcpy(p->s, s); + p->n = 1; + } return *this; } -- 2.11.0