[ros-diffs] [arty] 39218: Fix misuse of std::string in assuming that it's always null terminated, and that the c_str result is writable.

arty at svn.reactos.org arty at svn.reactos.org
Sat Jan 31 09:43:11 CET 2009


Author: arty
Date: Sat Jan 31 02:43:10 2009
New Revision: 39218

URL: http://svn.reactos.org/svn/reactos?rev=39218&view=rev
Log:
Fix misuse of std::string in assuming that it's always null terminated, and that
the c_str result is writable.  

Modified:
    trunk/reactos/tools/xml.cpp
    trunk/reactos/tools/xml.h

Modified: trunk/reactos/tools/xml.cpp
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/tools/xml.cpp?rev=39218&r1=39217&r2=39218&view=diff
==============================================================================
--- trunk/reactos/tools/xml.cpp [iso-8859-1] (original)
+++ trunk/reactos/tools/xml.cpp [iso-8859-1] Sat Jan 31 02:43:10 2009
@@ -58,6 +58,35 @@
 
 string working_directory;
 
+std::vector<char> vectorize(const std::string &str)
+{
+    std::vector<char> result( str.size() + 1 );
+    strcpy( &result[0], str.c_str() );
+    return result;
+}
+
+void vectappend(std::vector<char> &strvec, const char *str)
+{
+    if (*str) { strvec[strlen(&strvec[0])] = *str; str++; }
+    while (*str)
+    {
+    	strvec.push_back(*str);
+	str++;
+    }
+    strvec.push_back(0);
+}
+
+void vectappend(std::vector<char> &strvec, const std::string &str)
+{
+    vectappend(strvec, str.c_str());
+}
+
+void vectappend(std::vector<char> &strvec, char ch)
+{
+    strvec[strlen(&strvec[0])] = ch;
+    strvec.push_back(0);
+}
+
 XMLException::XMLException (
 	const std::string& location,
 	const char* format, ... )
@@ -158,7 +187,7 @@
 		return file;
 	}
 	vector<string> pathtmp ( path );
-	string tmp ( file );
+	vector<char> tmp = vectorize( file );
 	const char* prev = strtok ( &tmp[0], "/\\" );
 	const char* p = strtok ( NULL, "/\\" );
 	while ( p )
@@ -184,18 +213,18 @@
 		pathtmp.push_back ( prev );
 
 	// reuse tmp variable to return recombined path
-	tmp.resize(0);
+	tmp = vectorize("");
 	for ( size_t i = 0; i < pathtmp.size(); i++ )
 	{
 		// this squirreliness is b/c win32 has drive letters and *nix doesn't...
 #ifdef WIN32
-		if ( i ) tmp += "/";
+		if ( i ) vectappend(tmp, "/");
 #else
-		tmp += "/";
+		vectappend(tmp, "/");
 #endif
-		tmp += pathtmp[i];
-	}
-	return tmp;
+		vectappend(tmp, pathtmp[i]);
+	}
+	return &tmp[0];
 }
 
 string

Modified: trunk/reactos/tools/xml.h
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/tools/xml.h?rev=39218&r1=39217&r2=39218&view=diff
==============================================================================
--- trunk/reactos/tools/xml.h [iso-8859-1] (original)
+++ trunk/reactos/tools/xml.h [iso-8859-1] Sat Jan 31 02:43:10 2009
@@ -236,4 +236,9 @@
 XMLElement*
 XMLLoadFile ( const std::string& filename );
 
+std::vector<char> vectorize(const std::string &str);
+void vectappend(std::vector<char> &strvec, char ch);
+void vectappend(std::vector<char> &strvec, const char *charstr);
+void vectappend(std::vector<char> &strvec, const std::string &str);
+
 #endif // XML_H



More information about the Ros-diffs mailing list