Skip to content

Commit 4e8aa75

Browse files
committed
Use temporary_filet for automatic resource management
1 parent 63acc5b commit 4e8aa75

File tree

2 files changed

+49
-101
lines changed

2 files changed

+49
-101
lines changed

src/ansi-c/c_preprocess.cpp

Lines changed: 30 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@ Author: Daniel Kroening, [email protected]
1717
#include <cstring>
1818
#include <fstream>
1919

20-
#if defined(__linux__) || \
21-
defined(__FreeBSD_kernel__) || \
22-
defined(__GNU__) || \
23-
defined(__unix__) || \
24-
defined(__CYGWIN__) || \
25-
defined(__MACH__)
26-
#include <unistd.h>
27-
#endif
28-
2920
/// quote a string for bash and CMD
3021
static std::string shell_quote(const std::string &src)
3122
{
@@ -326,11 +317,11 @@ bool c_preprocess_visual_studio(
326317

327318
// use Visual Studio's CL
328319

329-
std::string stderr_file=get_temporary_file("tmp.stderr", "");
330-
std::string command_file_name=get_temporary_file("tmp.cl-cmd", "");
320+
temporary_filet stderr_file("tmp.stderr", "");
321+
temporary_filet command_file_name("tmp.cl-cmd", "");
331322

332323
{
333-
std::ofstream command_file(command_file_name);
324+
std::ofstream command_file(command_file_name());
334325

335326
// This marks the command file as UTF-8, which Visual Studio
336327
// understands.
@@ -380,23 +371,20 @@ bool c_preprocess_visual_studio(
380371
command_file << shell_quote(file) << "\n";
381372
}
382373

383-
std::string tmpi=get_temporary_file("tmp.cl", "");
374+
temporary_filet tmpi("tmp.cl", "");
384375

385-
std::string command="CL @\""+command_file_name+"\"";
386-
command+=" > \""+tmpi+"\"";
387-
command+=" 2> \""+stderr_file+"\"";
376+
std::string command = "CL @\"" + command_file_name() + "\"";
377+
command += " > \"" + tmpi() + "\"";
378+
command += " 2> \"" + stderr_file() + "\"";
388379

389380
// _popen isn't very reliable on WIN32
390381
// that's why we use system()
391382
int result=system(command.c_str());
392383

393-
std::ifstream instream(tmpi);
384+
std::ifstream instream(tmpi());
394385

395386
if(!instream)
396387
{
397-
unlink(tmpi.c_str());
398-
unlink(stderr_file.c_str());
399-
unlink(command_file_name.c_str());
400388
message.error() << "CL Preprocessing failed (open failed)"
401389
<< messaget::eom;
402390
return true;
@@ -405,15 +393,11 @@ bool c_preprocess_visual_studio(
405393
outstream << instream.rdbuf(); // copy
406394

407395
instream.close();
408-
unlink(tmpi.c_str());
409-
unlink(command_file_name.c_str());
410396

411397
// errors/warnings
412-
std::ifstream stderr_stream(stderr_file);
398+
std::ifstream stderr_stream(stderr_file());
413399
error_parse(stderr_stream, result==0, message);
414400

415-
unlink(stderr_file.c_str());
416-
417401
if(result!=0)
418402
{
419403
message.error() << "CL Preprocessing failed" << messaget::eom;
@@ -472,7 +456,7 @@ bool c_preprocess_codewarrior(
472456
// preprocessing
473457
messaget message(message_handler);
474458

475-
std::string stderr_file=get_temporary_file("tmp.stderr", "");
459+
temporary_filet stderr_file("tmp.stderr", "");
476460

477461
std::string command;
478462

@@ -492,37 +476,32 @@ bool c_preprocess_codewarrior(
492476

493477
int result;
494478

495-
std::string tmpi=get_temporary_file("tmp.cl", "");
479+
temporary_filet tmpi("tmp.cl", "");
496480
command+=" \""+file+"\"";
497-
command+=" -o \""+tmpi+"\"";
498-
command+=" 2> \""+stderr_file+"\"";
481+
command += " -o \"" + tmpi() + "\"";
482+
command += " 2> \"" + stderr_file() + "\"";
499483

500484
result=system(command.c_str());
501485

502-
std::ifstream stream_i(tmpi);
486+
std::ifstream stream_i(tmpi());
503487

504488
if(stream_i)
505489
{
506490
postprocess_codewarrior(stream_i, outstream);
507491

508492
stream_i.close();
509-
unlink(tmpi.c_str());
510493
}
511494
else
512495
{
513-
unlink(tmpi.c_str());
514-
unlink(stderr_file.c_str());
515496
message.error() << "Preprocessing failed (fopen failed)"
516497
<< messaget::eom;
517498
return true;
518499
}
519500

520501
// errors/warnings
521-
std::ifstream stderr_stream(stderr_file);
502+
std::ifstream stderr_stream(stderr_file());
522503
error_parse(stderr_stream, result==0, message);
523504

524-
unlink(stderr_file.c_str());
525-
526505
if(result!=0)
527506
{
528507
message.error() << "Preprocessing failed" << messaget::eom;
@@ -546,7 +525,7 @@ bool c_preprocess_gcc_clang(
546525
// preprocessing
547526
messaget message(message_handler);
548527

549-
std::string stderr_file=get_temporary_file("tmp.stderr", "");
528+
temporary_filet stderr_file("tmp.stderr", "");
550529

551530
std::string command;
552531

@@ -646,39 +625,35 @@ bool c_preprocess_gcc_clang(
646625
#endif
647626

648627
#ifdef _WIN32
649-
std::string tmpi=get_temporary_file("tmp.gcc", "");
628+
temporary_filet tmpi("tmp.gcc", "");
650629
command+=" \""+file+"\"";
651-
command+=" -o \""+tmpi+"\"";
652-
command+=" 2> \""+stderr_file+"\"";
630+
command += " -o \"" + tmpi() + "\"";
631+
command += " 2> \"" + stderr_file() + "\"";
653632

654633
// _popen isn't very reliable on WIN32
655634
// that's why we use system() and a temporary file
656635
result=system(command.c_str());
657636

658-
std::ifstream instream(tmpi);
637+
std::ifstream instream(tmpi());
659638

660639
// errors/warnings
661-
std::ifstream stderr_stream(stderr_file);
640+
std::ifstream stderr_stream(stderr_file());
662641
error_parse(stderr_stream, result==0, message);
663642

664-
unlink(stderr_file.c_str());
665-
666643
if(instream)
667644
{
668645
outstream << instream.rdbuf();
669646
instream.close();
670-
unlink(tmpi.c_str());
671647
}
672648
else
673649
{
674-
unlink(tmpi.c_str());
675650
message.error() << "GCC preprocessing failed (open failed)"
676651
<< messaget::eom;
677652
result=1;
678653
}
679654
#else
680655
command+=" \""+file+"\"";
681-
command+=" 2> \""+stderr_file+"\"";
656+
command += " 2> \"" + stderr_file() + "\"";
682657

683658
FILE *stream=popen(command.c_str(), "r");
684659

@@ -698,11 +673,9 @@ bool c_preprocess_gcc_clang(
698673
}
699674

700675
// errors/warnings
701-
std::ifstream stderr_stream(stderr_file);
676+
std::ifstream stderr_stream(stderr_file());
702677
error_parse(stderr_stream, result==0, message);
703678

704-
unlink(stderr_file.c_str());
705-
706679
#endif
707680

708681
if(result!=0)
@@ -727,7 +700,7 @@ bool c_preprocess_arm(
727700
// preprocessing using armcc
728701
messaget message(message_handler);
729702

730-
std::string stderr_file=get_temporary_file("tmp.stderr", "");
703+
temporary_filet stderr_file("tmp.stderr", "");
731704

732705
std::string command;
733706

@@ -765,34 +738,31 @@ bool c_preprocess_arm(
765738
int result;
766739

767740
#ifdef _WIN32
768-
std::string tmpi=get_temporary_file("tmp.cl", "");
741+
temporary_filet tmpi("tmp.cl", "");
769742
command+=" \""+file+"\"";
770-
command+=" > \""+tmpi+"\"";
771-
command+=" 2> \""+stderr_file+"\"";
743+
command += " > \"" + tmpi() + "\"";
744+
command += " 2> \"" + stderr_file() + "\"";
772745

773746
// _popen isn't very reliable on WIN32
774747
// that's why we use system() and a temporary file
775748
result=system(command.c_str());
776749

777-
std::ifstream instream(tmpi);
750+
std::ifstream instream(tmpi());
778751

779752
if(!instream)
780753
{
781754
outstream << instream.rdbuf(); // copy
782755
instream.close();
783-
unlink(tmpi.c_str());
784756
}
785757
else
786758
{
787-
unlink(tmpi.c_str());
788-
unlink(stderr_file.c_str());
789759
message.error() << "ARMCC preprocessing failed (fopen failed)"
790760
<< messaget::eom;
791761
return true;
792762
}
793763
#else
794764
command+=" \""+file+"\"";
795-
command+=" 2> \""+stderr_file+"\"";
765+
command += " 2> \"" + stderr_file() + "\"";
796766

797767
FILE *stream=popen(command.c_str(), "r");
798768

@@ -806,19 +776,16 @@ bool c_preprocess_arm(
806776
}
807777
else
808778
{
809-
unlink(stderr_file.c_str());
810779
message.error() << "ARMCC preprocessing failed (popen failed)"
811780
<< messaget::eom;
812781
return true;
813782
}
814783
#endif
815784

816785
// errors/warnings
817-
std::ifstream stderr_stream(stderr_file);
786+
std::ifstream stderr_stream(stderr_file());
818787
error_parse(stderr_stream, result==0, message);
819788

820-
unlink(stderr_file.c_str());
821-
822789
if(result!=0)
823790
{
824791
message.error() << "ARMCC preprocessing failed" << messaget::eom;

src/goto-programs/read_goto_binary.cpp

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,6 @@ Module: Read Goto Programs
1111

1212
#include "read_goto_binary.h"
1313

14-
#if defined(__linux__) || \
15-
defined(__FreeBSD_kernel__) || \
16-
defined(__GNU__) || \
17-
defined(__unix__) || \
18-
defined(__CYGWIN__) || \
19-
defined(__MACH__)
20-
#include <unistd.h>
21-
#endif
22-
2314
#include <fstream>
2415
#include <unordered_set>
2516

@@ -105,41 +96,31 @@ bool read_goto_binary(
10596
}
10697
else if(is_osx_fat_magic(hdr))
10798
{
108-
std::string tempname;
99+
messaget message(message_handler);
100+
109101
// Mach-O universal binary
110102
// This _may_ have a goto binary as hppa7100LC architecture
111-
try
103+
osx_fat_readert osx_fat_reader(in);
104+
105+
if(osx_fat_reader.has_gb())
112106
{
113-
osx_fat_readert osx_fat_reader(in);
107+
temporary_filet tempname("tmp.goto-binary", ".gb");
108+
osx_fat_reader.extract_gb(filename, tempname());
114109

115-
if(osx_fat_reader.has_gb())
116-
{
117-
tempname=get_temporary_file("tmp.goto-binary", ".gb");
118-
osx_fat_reader.extract_gb(filename, tempname);
119-
120-
std::ifstream temp_in(tempname, std::ios::binary);
121-
if(!temp_in)
122-
messaget(message_handler).error() << "failed to read temp binary"
123-
<< messaget::eom;
124-
const bool read_err=read_bin_goto_object(
125-
temp_in, filename, symbol_table, goto_functions, message_handler);
126-
temp_in.close();
127-
128-
unlink(tempname.c_str());
129-
return read_err;
130-
}
131-
132-
// architecture not found
133-
messaget(message_handler).error() <<
134-
"failed to find goto binary in Mach-O file" << messaget::eom;
135-
}
110+
std::ifstream temp_in(tempname(), std::ios::binary);
111+
if(!temp_in)
112+
message.error() << "failed to read temp binary" << messaget::eom;
136113

137-
catch(const char *s)
138-
{
139-
if(!tempname.empty())
140-
unlink(tempname.c_str());
141-
messaget(message_handler).error() << s << messaget::eom;
114+
const bool read_err = read_bin_goto_object(
115+
temp_in, filename, symbol_table, goto_functions, message_handler);
116+
temp_in.close();
117+
118+
return read_err;
142119
}
120+
121+
// architecture not found
122+
message.error() << "failed to find goto binary in Mach-O file"
123+
<< messaget::eom;
143124
}
144125
else
145126
{

0 commit comments

Comments
 (0)