Skip to content

Commit df3c57d

Browse files
committed
Update C++ example code to use BinaryNinja::Load to open a file. Fix #5429
1 parent e07d7c1 commit df3c57d

File tree

5 files changed

+25
-95
lines changed

5 files changed

+25
-95
lines changed

examples/bin-info/src/bin-info.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,13 @@ int main(int argc, char* argv[])
4444
SetBundledPluginDirectory(GetBundledPluginDirectory());
4545
InitPlugins();
4646

47-
Ref<BinaryData> bd = BinaryData::CreateFromFilename(new FileMetadata(), argv[1]);
48-
if (!bd)
49-
{
50-
fprintf(stderr, "Could not open input file.\n");
51-
return -1;
52-
}
53-
Ref<BinaryView> bv;
54-
for (auto type : BinaryViewType::GetViewTypes())
55-
{
56-
if (type->IsTypeValidForData(bd) && type->GetName() != "Raw")
57-
{
58-
bv = type->Create(bd);
59-
break;
60-
}
61-
}
62-
47+
Ref<BinaryView> bv = BinaryNinja::Load(fname);
6348
if (!bv || bv->GetTypeName() == "Raw")
6449
{
65-
fprintf(stderr, "Input file does not appear to be an exectuable\n");
50+
fprintf(stderr, "Input file does not appear to be an executable\n");
6651
return -1;
6752
}
6853

69-
bv->UpdateAnalysisAndWait();
70-
7154
cout << "Target: " << fname << endl << endl;
7255
cout << "TYPE: " << bv->GetTypeName() << endl;
7356
cout << "START: 0x" << hex << bv->GetStart() << endl;
@@ -100,6 +83,9 @@ int main(int argc, char* argv[])
10083
break;
10184
}
10285

86+
// Close the file so that the resources can be freed
87+
bv->GetFile()->Close();
88+
10389
// Shutting down is required to allow for clean exit of the core
10490
BNShutdown();
10591

examples/enterprise_test/src/enterprise_test.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,13 @@ int main(int argc, char* argv[])
4343
SetBundledPluginDirectory(GetBundledPluginDirectory());
4444
InitPlugins();
4545

46-
Ref<BinaryData> bd = BinaryData::CreateFromFilename(new FileMetadata(), argv[1]);
47-
if (!bd)
48-
{
49-
fprintf(stderr, "Could not open input file.\n");
50-
return -1;
51-
}
52-
Ref<BinaryView> bv;
53-
for (auto type : BinaryViewType::GetViewTypes())
54-
{
55-
if (type->IsTypeValidForData(bd) && type->GetName() != "Raw")
56-
{
57-
bv = type->Create(bd);
58-
break;
59-
}
60-
}
61-
46+
Ref<BinaryView> bv = BinaryNinja::Load(fname);
6247
if (!bv || bv->GetTypeName() == "Raw")
6348
{
64-
fprintf(stderr, "Input file does not appear to be an exectuable\n");
49+
fprintf(stderr, "Input file does not appear to be an executable\n");
6550
return -1;
6651
}
6752

68-
bv->UpdateAnalysisAndWait();
69-
7053
cout << "Target: " << fname << endl << endl;
7154
cout << "TYPE: " << bv->GetTypeName() << endl;
7255
cout << "START: 0x" << hex << bv->GetStart() << endl;
@@ -99,6 +82,9 @@ int main(int argc, char* argv[])
9982
break;
10083
}
10184

85+
// Close the file so that the resources can be freed
86+
bv->GetFile()->Close();
87+
10288
// Shutting down is required to allow for clean exit of the core
10389
BNShutdown();
10490

examples/llil_parser/src/llil_parser.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -292,30 +292,13 @@ int main(int argc, char* argv[])
292292
SetBundledPluginDirectory(GetBundledPluginDirectory());
293293
InitPlugins();
294294

295-
Ref<BinaryData> bd = BinaryData::CreateFromFilename(new FileMetadata(), argv[1]);
296-
if (!bd)
297-
{
298-
fprintf(stderr, "Could not open input file.\n");
299-
return -1;
300-
}
301-
Ref<BinaryView> bv;
302-
for (auto type : BinaryViewType::GetViewTypes())
303-
{
304-
if (type->IsTypeValidForData(bd) && type->GetName() != "Raw")
305-
{
306-
bv = type->Create(bd);
307-
break;
308-
}
309-
}
310-
295+
Ref<BinaryView> bv = BinaryNinja::Load(argv[1]);
311296
if (!bv || bv->GetTypeName() == "Raw")
312297
{
313-
fprintf(stderr, "Input file does not appear to be an exectuable\n");
298+
fprintf(stderr, "Input file does not appear to be an executable\n");
314299
return -1;
315300
}
316301

317-
bv->UpdateAnalysisAndWait();
318-
319302
// Go through all functions in the binary
320303
for (auto& func : bv->GetAnalysisFunctionList())
321304
{
@@ -398,6 +381,9 @@ int main(int argc, char* argv[])
398381
printf("\n");
399382
}
400383

384+
// Close the file so that the resources can be freed
385+
bv->GetFile()->Close();
386+
401387
// Shutting down is required to allow for clean exit of the core
402388
BNShutdown();
403389

examples/mlil_parser/src/mlil_parser.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -243,30 +243,13 @@ int main(int argc, char* argv[])
243243
SetBundledPluginDirectory(GetBundledPluginDirectory());
244244
InitPlugins();
245245

246-
Ref<BinaryData> bd = BinaryData::CreateFromFilename(new FileMetadata(), argv[1]);
247-
if (!bd)
248-
{
249-
fprintf(stderr, "Could not open input file.\n");
250-
return -1;
251-
}
252-
Ref<BinaryView> bv;
253-
for (auto type : BinaryViewType::GetViewTypes())
254-
{
255-
if (type->IsTypeValidForData(bd) && type->GetName() != "Raw")
256-
{
257-
bv = type->Create(bd);
258-
break;
259-
}
260-
}
261-
246+
Ref<BinaryView> bv = BinaryNinja::Load(argv[1]);
262247
if (!bv || bv->GetTypeName() == "Raw")
263248
{
264-
fprintf(stderr, "Input file does not appear to be an exectuable\n");
249+
fprintf(stderr, "Input file does not appear to be an executable\n");
265250
return -1;
266251
}
267252

268-
bv->UpdateAnalysisAndWait();
269-
270253
// Go through all functions in the binary
271254
for (auto& func : bv->GetAnalysisFunctionList())
272255
{
@@ -342,6 +325,9 @@ int main(int argc, char* argv[])
342325
printf("\n");
343326
}
344327

328+
// Close the file so that the resources can be freed
329+
bv->GetFile()->Close();
330+
345331
// Shutting down is required to allow for clean exit of the core
346332
BNShutdown();
347333

examples/print_syscalls/src/arm-syscall.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,13 @@ int main(int argc, char* argv[])
4444
SetBundledPluginDirectory(GetBundledPluginDirectory());
4545
InitPlugins();
4646

47-
Ref<BinaryData> bd = BinaryData::CreateFromFilename(new FileMetadata(), argv[1]);
48-
if (!bd)
49-
{
50-
fprintf(stderr, "Could not open input file.\n");
51-
return -1;
52-
}
53-
Ref<BinaryView> bv;
54-
for (auto type : BinaryViewType::GetViewTypes())
55-
{
56-
if (type->IsTypeValidForData(bd) && type->GetName() != "Raw")
57-
{
58-
bv = type->Create(bd);
59-
break;
60-
}
61-
}
62-
47+
Ref<BinaryView> bv = BinaryNinja::Load(fname);
6348
if (!bv || bv->GetTypeName() == "Raw")
6449
{
65-
fprintf(stderr, "Input file does not appear to be an exectuable\n");
50+
fprintf(stderr, "Input file does not appear to be an executable\n");
6651
return -1;
6752
}
6853

69-
bv->UpdateAnalysisAndWait();
70-
7154
auto arch = bv->GetDefaultArchitecture();
7255
auto platform = bv->GetDefaultPlatform();
7356

@@ -97,6 +80,9 @@ int main(int argc, char* argv[])
9780
}
9881
}
9982

83+
// Close the file so that the resources can be freed
84+
bv->GetFile()->Close();
85+
10086
// Shutting down is required to allow for clean exit of the core
10187
BNShutdown();
10288

0 commit comments

Comments
 (0)