Skip to content

Commit d17c990

Browse files
author
Daniel Kroening
authored
Merge pull request diffblue#2321 from tautschnig/vs-temporary-filet
Use temporary_filet for automatic resource management
2 parents 43d008a + bcf8e61 commit d17c990

File tree

2 files changed

+49
-101
lines changed

2 files changed

+49
-101
lines changed

src/ansi-c/c_preprocess.cpp

+30-63
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ Author: Daniel Kroening, [email protected]
1616

1717
#include <fstream>
1818

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

326317
// use Visual Studio's CL
327318

328-
std::string stderr_file=get_temporary_file("tmp.stderr", "");
329-
std::string command_file_name=get_temporary_file("tmp.cl-cmd", "");
319+
temporary_filet stderr_file("tmp.stderr", "");
320+
temporary_filet command_file_name("tmp.cl-cmd", "");
330321

331322
{
332-
std::ofstream command_file(command_file_name);
323+
std::ofstream command_file(command_file_name());
333324

334325
// This marks the command file as UTF-8, which Visual Studio
335326
// understands.
@@ -385,23 +376,20 @@ bool c_preprocess_visual_studio(
385376
command_file << shell_quote(file) << "\n";
386377
}
387378

388-
std::string tmpi=get_temporary_file("tmp.cl", "");
379+
temporary_filet tmpi("tmp.cl", "");
389380

390-
std::string command="CL @\""+command_file_name+"\"";
391-
command+=" > \""+tmpi+"\"";
392-
command+=" 2> \""+stderr_file+"\"";
381+
std::string command = "CL @\"" + command_file_name() + "\"";
382+
command += " > \"" + tmpi() + "\"";
383+
command += " 2> \"" + stderr_file() + "\"";
393384

394385
// _popen isn't very reliable on WIN32
395386
// that's why we use system()
396387
int result=system(command.c_str());
397388

398-
std::ifstream instream(tmpi);
389+
std::ifstream instream(tmpi());
399390

400391
if(!instream)
401392
{
402-
unlink(tmpi.c_str());
403-
unlink(stderr_file.c_str());
404-
unlink(command_file_name.c_str());
405393
message.error() << "CL Preprocessing failed (open failed)"
406394
<< messaget::eom;
407395
return true;
@@ -410,15 +398,11 @@ bool c_preprocess_visual_studio(
410398
outstream << instream.rdbuf(); // copy
411399

412400
instream.close();
413-
unlink(tmpi.c_str());
414-
unlink(command_file_name.c_str());
415401

416402
// errors/warnings
417-
std::ifstream stderr_stream(stderr_file);
403+
std::ifstream stderr_stream(stderr_file());
418404
error_parse(stderr_stream, result==0, message);
419405

420-
unlink(stderr_file.c_str());
421-
422406
if(result!=0)
423407
{
424408
message.error() << "CL Preprocessing failed" << messaget::eom;
@@ -477,7 +461,7 @@ bool c_preprocess_codewarrior(
477461
// preprocessing
478462
messaget message(message_handler);
479463

480-
std::string stderr_file=get_temporary_file("tmp.stderr", "");
464+
temporary_filet stderr_file("tmp.stderr", "");
481465

482466
std::string command;
483467

@@ -497,37 +481,32 @@ bool c_preprocess_codewarrior(
497481

498482
int result;
499483

500-
std::string tmpi=get_temporary_file("tmp.cl", "");
484+
temporary_filet tmpi("tmp.cl", "");
501485
command+=" \""+file+"\"";
502-
command+=" -o \""+tmpi+"\"";
503-
command+=" 2> \""+stderr_file+"\"";
486+
command += " -o \"" + tmpi() + "\"";
487+
command += " 2> \"" + stderr_file() + "\"";
504488

505489
result=system(command.c_str());
506490

507-
std::ifstream stream_i(tmpi);
491+
std::ifstream stream_i(tmpi());
508492

509493
if(stream_i)
510494
{
511495
postprocess_codewarrior(stream_i, outstream);
512496

513497
stream_i.close();
514-
unlink(tmpi.c_str());
515498
}
516499
else
517500
{
518-
unlink(tmpi.c_str());
519-
unlink(stderr_file.c_str());
520501
message.error() << "Preprocessing failed (fopen failed)"
521502
<< messaget::eom;
522503
return true;
523504
}
524505

525506
// errors/warnings
526-
std::ifstream stderr_stream(stderr_file);
507+
std::ifstream stderr_stream(stderr_file());
527508
error_parse(stderr_stream, result==0, message);
528509

529-
unlink(stderr_file.c_str());
530-
531510
if(result!=0)
532511
{
533512
message.error() << "Preprocessing failed" << messaget::eom;
@@ -551,7 +530,7 @@ bool c_preprocess_gcc_clang(
551530
// preprocessing
552531
messaget message(message_handler);
553532

554-
std::string stderr_file=get_temporary_file("tmp.stderr", "");
533+
temporary_filet stderr_file("tmp.stderr", "");
555534

556535
std::string command;
557536

@@ -651,39 +630,35 @@ bool c_preprocess_gcc_clang(
651630
#endif
652631

653632
#ifdef _WIN32
654-
std::string tmpi=get_temporary_file("tmp.gcc", "");
633+
temporary_filet tmpi("tmp.gcc", "");
655634
command+=" \""+file+"\"";
656-
command+=" -o \""+tmpi+"\"";
657-
command+=" 2> \""+stderr_file+"\"";
635+
command += " -o \"" + tmpi() + "\"";
636+
command += " 2> \"" + stderr_file() + "\"";
658637

659638
// _popen isn't very reliable on WIN32
660639
// that's why we use system() and a temporary file
661640
result=system(command.c_str());
662641

663-
std::ifstream instream(tmpi);
642+
std::ifstream instream(tmpi());
664643

665644
// errors/warnings
666-
std::ifstream stderr_stream(stderr_file);
645+
std::ifstream stderr_stream(stderr_file());
667646
error_parse(stderr_stream, result==0, message);
668647

669-
unlink(stderr_file.c_str());
670-
671648
if(instream)
672649
{
673650
outstream << instream.rdbuf();
674651
instream.close();
675-
unlink(tmpi.c_str());
676652
}
677653
else
678654
{
679-
unlink(tmpi.c_str());
680655
message.error() << "GCC preprocessing failed (open failed)"
681656
<< messaget::eom;
682657
result=1;
683658
}
684659
#else
685660
command+=" \""+file+"\"";
686-
command+=" 2> \""+stderr_file+"\"";
661+
command += " 2> \"" + stderr_file() + "\"";
687662

688663
FILE *stream=popen(command.c_str(), "r");
689664

@@ -703,11 +678,9 @@ bool c_preprocess_gcc_clang(
703678
}
704679

705680
// errors/warnings
706-
std::ifstream stderr_stream(stderr_file);
681+
std::ifstream stderr_stream(stderr_file());
707682
error_parse(stderr_stream, result==0, message);
708683

709-
unlink(stderr_file.c_str());
710-
711684
#endif
712685

713686
if(result!=0)
@@ -732,7 +705,7 @@ bool c_preprocess_arm(
732705
// preprocessing using armcc
733706
messaget message(message_handler);
734707

735-
std::string stderr_file=get_temporary_file("tmp.stderr", "");
708+
temporary_filet stderr_file("tmp.stderr", "");
736709

737710
std::string command;
738711

@@ -770,34 +743,31 @@ bool c_preprocess_arm(
770743
int result;
771744

772745
#ifdef _WIN32
773-
std::string tmpi=get_temporary_file("tmp.cl", "");
746+
temporary_filet tmpi("tmp.cl", "");
774747
command+=" \""+file+"\"";
775-
command+=" > \""+tmpi+"\"";
776-
command+=" 2> \""+stderr_file+"\"";
748+
command += " > \"" + tmpi() + "\"";
749+
command += " 2> \"" + stderr_file() + "\"";
777750

778751
// _popen isn't very reliable on WIN32
779752
// that's why we use system() and a temporary file
780753
result=system(command.c_str());
781754

782-
std::ifstream instream(tmpi);
755+
std::ifstream instream(tmpi());
783756

784757
if(!instream)
785758
{
786759
outstream << instream.rdbuf(); // copy
787760
instream.close();
788-
unlink(tmpi.c_str());
789761
}
790762
else
791763
{
792-
unlink(tmpi.c_str());
793-
unlink(stderr_file.c_str());
794764
message.error() << "ARMCC preprocessing failed (fopen failed)"
795765
<< messaget::eom;
796766
return true;
797767
}
798768
#else
799769
command+=" \""+file+"\"";
800-
command+=" 2> \""+stderr_file+"\"";
770+
command += " 2> \"" + stderr_file() + "\"";
801771

802772
FILE *stream=popen(command.c_str(), "r");
803773

@@ -811,19 +781,16 @@ bool c_preprocess_arm(
811781
}
812782
else
813783
{
814-
unlink(stderr_file.c_str());
815784
message.error() << "ARMCC preprocessing failed (popen failed)"
816785
<< messaget::eom;
817786
return true;
818787
}
819788
#endif
820789

821790
// errors/warnings
822-
std::ifstream stderr_stream(stderr_file);
791+
std::ifstream stderr_stream(stderr_file());
823792
error_parse(stderr_stream, result==0, message);
824793

825-
unlink(stderr_file.c_str());
826-
827794
if(result!=0)
828795
{
829796
message.error() << "ARMCC preprocessing failed" << messaget::eom;

src/goto-programs/read_goto_binary.cpp

+19-38
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)