r208249 - c-index-test: Don't leak the strings returned by makeClientContainer().

Nico Weber nicolasweber at gmx.de
Wed May 7 14:09:42 PDT 2014


Author: nico
Date: Wed May  7 16:09:42 2014
New Revision: 208249

URL: http://llvm.org/viewvc/llvm-project?rev=208249&view=rev
Log:
c-index-test: Don't leak the strings returned by makeClientContainer().

Modified:
    cfe/trunk/tools/c-index-test/c-index-test.c

Modified: cfe/trunk/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=208249&r1=208248&r2=208249&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Wed May  7 16:09:42 2014
@@ -1824,7 +1824,7 @@ static void print_completion_string(CXCo
 }
 
 static void print_completion_result(CXCompletionResult *completion_result,
-                             FILE *file) {
+                                    FILE *file) {
   CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
   unsigned annotationCount;
   enum CXCursorKind ParentKind;
@@ -2532,6 +2532,11 @@ static void importedASTS_insert(Imported
   p->filenames[p->num_files++] = strdup(file);
 }
 
+typedef struct IndexDataStringList_ {
+  struct IndexDataStringList_ *next;
+  char data[1]; /* Dynamically sized. */
+} IndexDataStringList;
+
 typedef struct {
   const char *check_prefix;
   int first_check_printed;
@@ -2539,8 +2544,19 @@ typedef struct {
   int abort;
   const char *main_filename;
   ImportedASTFilesData *importedASTs;
+  IndexDataStringList *strings;
 } IndexData;
 
+static void free_client_data(IndexData *index_data) {
+  IndexDataStringList *node = index_data->strings;
+  while (node) {
+    IndexDataStringList *next = node->next;
+    free(node);
+    node = next;
+  }
+  index_data->strings = NULL;
+}
+
 static void printCheck(IndexData *data) {
   if (data->check_prefix) {
     if (data->first_check_printed) {
@@ -2601,8 +2617,11 @@ static unsigned digitCount(unsigned val)
   }
 }
 
-static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
+static CXIdxClientContainer makeClientContainer(CXClientData *client_data,
+                                                const CXIdxEntityInfo *info,
                                                 CXIdxLoc loc) {
+  IndexData *index_data;
+  IndexDataStringList *node;
   const char *name;
   char *newStr;
   CXIdxClientFile file;
@@ -2613,10 +2632,18 @@ static CXIdxClientContainer makeClientCo
     name = "<anon-tag>";
 
   clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
-  /* FIXME: free these.*/
-  newStr = (char *)malloc(strlen(name) +
-                          digitCount(line) + digitCount(column) + 3);
+
+  node =
+      (IndexDataStringList *)malloc(sizeof(IndexDataStringList) + strlen(name) +
+                                    digitCount(line) + digitCount(column) + 2);
+  newStr = node->data;
   sprintf(newStr, "%s:%d:%d", name, line, column);
+
+  /* Remember string so it can be freed later. */
+  index_data = (IndexData *)client_data;
+  node->next = index_data->strings;
+  index_data->strings = node;
+
   return (CXIdxClientContainer)newStr;
 }
 
@@ -2934,7 +2961,7 @@ static void index_indexDeclaration(CXCli
   if (info->declAsContainer)
     clang_index_setClientContainer(
         info->declAsContainer,
-        makeClientContainer(info->entityInfo, info->loc));
+        makeClientContainer(client_data, info->entityInfo, info->loc));
 }
 
 static void index_indexEntityReference(CXClientData client_data,
@@ -3005,6 +3032,7 @@ static int index_compile_args(int num_ar
   index_data.abort = 0;
   index_data.main_filename = "";
   index_data.importedASTs = importedASTs;
+  index_data.strings = NULL;
 
   index_opts = getIndexOptions();
   result = clang_indexSourceFile(idxAction, &index_data,
@@ -3017,6 +3045,7 @@ static int index_compile_args(int num_ar
   if (index_data.fail_for_error)
     result = -1;
 
+  free_client_data(&index_data);
   return result;
 }
 
@@ -3039,6 +3068,7 @@ static int index_ast_file(const char *as
   index_data.abort = 0;
   index_data.main_filename = "";
   index_data.importedASTs = importedASTs;
+  index_data.strings = NULL;
 
   index_opts = getIndexOptions();
   result = clang_indexTranslationUnit(idxAction, &index_data,
@@ -3048,6 +3078,7 @@ static int index_ast_file(const char *as
     result = -1;
 
   clang_disposeTranslationUnit(TU);
+  free_client_data(&index_data);
   return result;
 }
 





More information about the cfe-commits mailing list