Commit bcd9dee2f5cddfbbc176138346967f41206bd7ad

Fix buffer overrun in string operator +
  
1616
1717#include "string.h"
1818
19#include "iostream.h"
20using namespace std;
21
1922namespace std
2023{
2124 // Make an empty string
114114 return !(* this == s);
115115 }
116116
117 string string::operator + (const char * s)
117 string operator + (const char * lhs, string& rhs)
118118 {
119 char * temp;
120 strncpy(temp, m_data, m_length + 1);
119 // Allocate space to copy the strings
120 char * temp = new char[strlen(lhs) + rhs.length() + 1];
121121
122 string str;
123 str = strcat(temp, s);
122 // Copy the first string
123 strcpy(temp, lhs);
124124
125 // Copy the second string and make a new string from it
126 string str(strcat(temp, rhs.c_string()));
127
128 // Free the temp string's memory
129 delete [] temp;
130
125131 return str;
126132 }
127133
128 // TODO: Fix this
129 /*string string::operator + (string s)
134 string operator + (string& lhs, const char * rhs)
130135 {
131 char * temp;
132 strcpy(temp, m_data);
136 // Allocate space to copy the strings
137 char * temp = new char[lhs.length() + strlen(rhs) + 1];
133138
134 string str;
135 str = strcat(temp, s.c_string());
136 cout << " d";
139 // Copy the first string
140 strcpy(temp, lhs.c_string());
141
142 // Copy the second string and make a new string from it
143 string str(strcat(temp, rhs));
144
145 // Free the temp string's memory
146 delete [] temp;
147
137148 return str;
138 }*/
149 }
150
151 string operator + (string& lhs, string& rhs)
152 {
153 // Allocate space to copy the strings
154 char * temp = new char[lhs.length() + rhs.length() + 1];
155
156 // Copy the first string
157 strcpy(temp, lhs.c_string());
158
159 string str(strcat(temp, rhs.c_string()));
160
161 // Free the temporary string's memory
162 delete [] temp;
163
164 return str;
165 }
139166
140167 // Return a C string of the string
141168 const char * string::c_string()
  
5555 bool operator != (const char * s);
5656
5757 // Addition operator for adding two strings together
58 string operator + (const char * s);
59 //string operator + (string s);
58 friend string operator + (const char * lhs, string& rhs);
59 friend string operator + (string& lhs, const char * rhs);
60 friend string operator + (string& lhs, string& rhs);
6061
6162 // Return the length of this string
6263 size_t length() const;