Skip to content

Commit 36f98e1

Browse files
[ScanDependencies] Fix a memory leak in dependency graph
Fix a memory leak from #75134. (cherry picked from commit ed93685)
1 parent c29791c commit 36f98e1

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

lib/DependencyScan/DependencyScanJSON.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ static void
206206
writeMacroDependencies(llvm::raw_ostream &out,
207207
const swiftscan_macro_dependency_set_t *macro_deps,
208208
unsigned indentLevel, bool trailingComma) {
209-
if (macro_deps->count == 0)
209+
if (!macro_deps)
210210
return;
211211

212212
out.indent(indentLevel * 2);

lib/DependencyScan/DependencyScanningTool.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,6 @@ static swiftscan_dependency_graph_t generateHollowDiagnosticOutput(
217217
c_string_utils::create_clone("swiftTextual:unknown");
218218

219219
// Hollow info details
220-
swiftscan_module_details_s *hollowDetails = new swiftscan_module_details_s;
221-
hollowDetails->kind = SWIFTSCAN_DEPENDENCY_INFO_SWIFT_TEXTUAL;
222220
hollowDetails->swift_textual_details = {c_string_utils::create_null(),
223221
c_string_utils::create_empty_set(),
224222
c_string_utils::create_null(),
@@ -232,7 +230,8 @@ static swiftscan_dependency_graph_t generateHollowDiagnosticOutput(
232230
false,
233231
c_string_utils::create_null(),
234232
c_string_utils::create_null(),
235-
c_string_utils::create_null()};
233+
c_string_utils::create_null(),
234+
nullptr};
236235
hollowMainModuleInfo->details = hollowDetails;
237236

238237
// Populate the diagnostic info

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -591,12 +591,10 @@ static void bridgeDependencyIDs(const ArrayRef<ModuleDependencyID> dependencies,
591591

592592
static swiftscan_macro_dependency_set_t *createMacroDependencySet(
593593
const std::map<std::string, MacroPluginDependency> &macroDeps) {
594+
if (macroDeps.empty())
595+
return nullptr;
596+
594597
swiftscan_macro_dependency_set_t *set = new swiftscan_macro_dependency_set_t;
595-
if (macroDeps.empty()) {
596-
set->count = 0;
597-
set->macro_dependencies = nullptr;
598-
return set;
599-
}
600598
set->count = macroDeps.size();
601599
set->macro_dependencies = new swiftscan_macro_dependency_t[set->count];
602600
unsigned SI = 0;

tools/libSwiftScan/libSwiftScan.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DependencyScanningTool, swiftscan_scanner_t)
3131
//=== Private Cleanup Functions -------------------------------------------===//
3232
void swiftscan_macro_dependency_dispose(
3333
swiftscan_macro_dependency_set_t *macro) {
34+
if (!macro)
35+
return;
36+
3437
for (unsigned i = 0; i < macro->count; ++i) {
3538
swiftscan_string_dispose(macro->macro_dependencies[i]->moduleName);
3639
swiftscan_string_dispose(macro->macro_dependencies[i]->libraryPath);
3740
swiftscan_string_dispose(macro->macro_dependencies[i]->executablePath);
3841
delete macro->macro_dependencies[i];
3942
}
43+
delete[] macro->macro_dependencies;
4044
delete macro;
4145
}
4246

0 commit comments

Comments
 (0)