From 6b11edb2e2b43c91027babbe0cbac43a468b0f04 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 3 Oct 2013 15:44:53 -0700 Subject: [PATCH] Fix use-after-free bugs UnsavedFile should now actually be storing the data, not just the pointers to it. --- cpp/ycm/ClangCompleter/ClangUtils.cpp | 7 ++----- cpp/ycm/ClangCompleter/ClangUtils.h | 2 ++ cpp/ycm/ClangCompleter/UnsavedFile.h | 8 ++++---- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/cpp/ycm/ClangCompleter/ClangUtils.cpp b/cpp/ycm/ClangCompleter/ClangUtils.cpp index 30ec2767..cf4445c6 100644 --- a/cpp/ycm/ClangCompleter/ClangUtils.cpp +++ b/cpp/ycm/ClangCompleter/ClangUtils.cpp @@ -112,11 +112,8 @@ std::vector< CXUnsavedFile > ToCXUnsavedFiles( std::vector< CXUnsavedFile > clang_unsaved_files( unsaved_files.size() ); for ( uint i = 0; i < unsaved_files.size(); ++i ) { - X_VERIFY( unsaved_files[ i ].filename_ ); - X_VERIFY( unsaved_files[ i ].contents_ ); - X_VERIFY( unsaved_files[ i ].length_ ); - clang_unsaved_files[ i ].Filename = unsaved_files[ i ].filename_; - clang_unsaved_files[ i ].Contents = unsaved_files[ i ].contents_; + clang_unsaved_files[ i ].Filename = unsaved_files[ i ].filename_.c_str(); + clang_unsaved_files[ i ].Contents = unsaved_files[ i ].contents_.c_str(); clang_unsaved_files[ i ].Length = unsaved_files[ i ].length_; } diff --git a/cpp/ycm/ClangCompleter/ClangUtils.h b/cpp/ycm/ClangCompleter/ClangUtils.h index b7dd7a43..088c43b2 100644 --- a/cpp/ycm/ClangCompleter/ClangUtils.h +++ b/cpp/ycm/ClangCompleter/ClangUtils.h @@ -37,6 +37,8 @@ std::string CXStringToString( CXString text ); std::vector< CompletionData > ToCompletionDataVector( CXCodeCompleteResults *results ); +// NOTE: CXUnsavedFiles store pointers to data in UnsavedFiles, so UnsavedFiles +// need to outlive CXUnsavedFiles! std::vector< CXUnsavedFile > ToCXUnsavedFiles( const std::vector< UnsavedFile > &unsaved_files ); diff --git a/cpp/ycm/ClangCompleter/UnsavedFile.h b/cpp/ycm/ClangCompleter/UnsavedFile.h index d35041a3..69bcd2ce 100644 --- a/cpp/ycm/ClangCompleter/UnsavedFile.h +++ b/cpp/ycm/ClangCompleter/UnsavedFile.h @@ -18,13 +18,13 @@ #ifndef UNSAVEDFILE_H_0GIYZQL4 #define UNSAVEDFILE_H_0GIYZQL4 -#include +#include struct UnsavedFile { - UnsavedFile() : filename_( NULL ), contents_( NULL ), length_( 0 ) {} + UnsavedFile() : filename_( "" ), contents_( "" ), length_( 0 ) {} - const char *filename_; - const char *contents_; + std::string filename_; + std::string contents_; unsigned long length_; // We need this to be able to export this struct to Python via Boost.Python's