From 8fd59d7392e488f9deac24ddc3a59e2293b5897b Mon Sep 17 00:00:00 2001
From: Chandler Carruth <chandlerc@gmail.com>
Date: Tue, 4 Aug 2015 03:53:04 +0000
Subject: [PATCH] [UB] Avoid a really broken call to realloc that would later
 result in a bad call to memcpy.

When we only have a buffer from one of the two reparse calls, we can
just return that buffer rather than going through the realloc/memcpy
dance.

Found with UBsan.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@243950 91177308-0d34-0410-b5e6-96231b3b80d8
---
 tools/c-index-test/c-index-test.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index 980f341824f..eeeb832cd87 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -255,6 +255,17 @@ static int parse_remapped_files_with_try(int try_idx,
   if (ret)
     return ret;
 
+  if (num_unsaved_files_no_try_idx == 0) {
+    *unsaved_files = unsaved_files_try_idx;
+    *num_unsaved_files = num_unsaved_files_try_idx;
+    return 0;
+  }
+  if (num_unsaved_files_try_idx == 0) {
+    *unsaved_files = unsaved_files_no_try_idx;
+    *num_unsaved_files = num_unsaved_files_no_try_idx;
+    return 0;
+  }
+
   *num_unsaved_files = num_unsaved_files_no_try_idx + num_unsaved_files_try_idx;
   *unsaved_files
     = (struct CXUnsavedFile *)realloc(unsaved_files_no_try_idx,
-- 
GitLab