Skip to content

Commit 2f46062

Browse files
Cleanup invariants and throws in util t-z
1 parent 0610f70 commit 2f46062

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

src/util/tempdir.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ Author: CM Wintersteiger
2626
#include <unistd.h>
2727
#endif
2828

29-
#include "invariant.h"
3029
#include "file_util.h"
30+
#include "exception_utils.h"
3131

3232
std::string get_temporary_directory(const std::string &name_template)
3333
{
@@ -38,22 +38,26 @@ std::string get_temporary_directory(const std::string &name_template)
3838
char lpPathBuffer[MAX_PATH+1];
3939
DWORD dwRetVal = GetTempPathA(dwBufSize, lpPathBuffer);
4040

41-
if(dwRetVal > dwBufSize || (dwRetVal == 0))
42-
throw "GetTempPath failed"; // NOLINT(readability/throw)
41+
if(dwRetVal > dwBufSize || (dwRetVal == 0)) {
42+
throw system_exceptiont("Couldn't get temporary path");
43+
}
4344

4445
// GetTempFileNameA produces <path>\<pre><uuuu>.TMP
4546
// where <pre> = "TLO"
4647
// Thus, we must make the buffer 1+3+4+1+3=12 characters longer.
4748

4849
char t[MAX_PATH];
4950
UINT uRetVal=GetTempFileNameA(lpPathBuffer, "TLO", 0, t);
50-
if(uRetVal == 0)
51-
throw "GetTempFileName failed"; // NOLINT(readability/throw)
51+
if(uRetVal == 0) {
52+
throw system_exceptiont("Couldn't get new temporary file name in directory"
53+
+ lpPathBuffer);
54+
}
5255

5356
unlink(t);
54-
if(_mkdir(t)!=0)
55-
throw "_mkdir failed";
56-
57+
if(_mkdir(t)!=0) {
58+
throw system_exceptiont("Couldn't create temporary directory at "
59+
+ t);
60+
}
5761
result=std::string(t);
5862

5963
#else
@@ -69,7 +73,7 @@ std::string get_temporary_directory(const std::string &name_template)
6973
t.push_back('\0'); // add the zero
7074
const char *td = mkdtemp(t.data());
7175
if(!td)
72-
throw "mkdtemp failed";
76+
throw system_exceptiont("Failed to create temporary directory");
7377
result=std::string(td);
7478
#endif
7579

@@ -101,11 +105,13 @@ temp_working_dirt::temp_working_dirt(const std::string &name_template):
101105
{
102106
old_working_directory=get_current_working_directory();
103107
if(chdir(path.c_str())!=0)
104-
CHECK_RETURN(false);
108+
throw system_exceptiont("Couldn't change directory to " + path);
105109
}
106110

107-
temp_working_dirt::~temp_working_dirt()
111+
temp_working_dirt::~temp_working_dirt() noexcept(false)
108112
{
113+
// FIXME throwing from destructors is generally a bad idea
114+
// but I can't think of anything else that'd be reasonable here?
109115
if(chdir(old_working_directory.c_str())!=0)
110-
CHECK_RETURN(false);
116+
throw system_exceptiont("Couldn't change directory to " + path);
111117
}

src/util/tempdir.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class temp_working_dirt:public temp_dirt
3939
std::string old_working_directory;
4040

4141
explicit temp_working_dirt(const std::string &name_template);
42-
~temp_working_dirt();
42+
~temp_working_dirt() noexcept(false);
4343
};
4444

4545
#endif // CPROVER_UTIL_TEMPDIR_H

src/util/tempfile.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Author: Daniel Kroening
2525
#include <cstdio>
2626
#include <cstring>
2727

28+
#include "exception_utils.h"
29+
2830
#if defined(__linux__) || \
2931
defined(__FreeBSD_kernel__) || \
3032
defined(__GNU__) || \
@@ -98,7 +100,7 @@ std::string get_temporary_file(
98100
lpTempPathBuffer); // buffer for path
99101

100102
if(dwRetVal>MAX_PATH || (dwRetVal==0))
101-
throw "GetTempPath failed"; // NOLINT(readability/throw)
103+
throw system_exceptiont("Failed to get temporary directory");
102104

103105
// the path returned by GetTempPath ends with a backslash
104106
std::string t_template=
@@ -121,7 +123,7 @@ std::string get_temporary_file(
121123
int fd=mkstemps(t_ptr, suffix.size());
122124

123125
if(fd<0)
124-
throw "mkstemps failed";
126+
throw system_exceptiont("Failed to open temporary file");
125127

126128
close(fd);
127129

src/util/xml.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Author: Daniel Kroening, [email protected]
1111
#include <ostream>
1212

1313
#include "string2int.h"
14+
#include "exception_utils.h"
1415

1516
void xmlt::clear()
1617
{
@@ -240,7 +241,7 @@ std::string xmlt::unescape(const std::string &str)
240241
result+=c;
241242
}
242243
else
243-
throw "XML escape code not implemented"; // NOLINT(readability/throw)
244+
throw deserialization_exceptiont("unknown XML escape code: " + tmp);
244245
}
245246
else
246247
result+=*it;

0 commit comments

Comments
 (0)