Skip to content

Commit 543dc4d

Browse files
Cleanup invariants and throws in util t-z
1 parent 7f61797 commit 543dc4d

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

src/util/tempdir.cpp

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

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

3232
std::string get_temporary_directory(const std::string &name_template)
@@ -39,7 +39,9 @@ std::string get_temporary_directory(const std::string &name_template)
3939
DWORD dwRetVal = GetTempPathA(dwBufSize, lpPathBuffer);
4040

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

4446
// GetTempFileNameA produces <path>\<pre><uuuu>.TMP
4547
// where <pre> = "TLO"
@@ -48,12 +50,18 @@ std::string get_temporary_directory(const std::string &name_template)
4850
char t[MAX_PATH];
4951
UINT uRetVal=GetTempFileNameA(lpPathBuffer, "TLO", 0, t);
5052
if(uRetVal == 0)
51-
throw "GetTempFileName failed"; // NOLINT(readability/throw)
53+
{
54+
throw system_exceptiont(
55+
std::string("Couldn't get new temporary file name in directory") +
56+
lpPathBuffer);
57+
}
5258

5359
unlink(t);
54-
if(_mkdir(t)!=0)
55-
throw "_mkdir failed";
56-
60+
if(_mkdir(t) != 0)
61+
{
62+
throw system_exceptiont(
63+
std::string("Couldn't create temporary directory at ") + t);
64+
}
5765
result=std::string(t);
5866

5967
#else
@@ -69,7 +77,7 @@ std::string get_temporary_directory(const std::string &name_template)
6977
t.push_back('\0'); // add the zero
7078
const char *td = mkdtemp(t.data());
7179
if(!td)
72-
throw "mkdtemp failed";
80+
throw system_exceptiont("Failed to create temporary directory");
7381
result=std::string(td);
7482
#endif
7583

@@ -101,11 +109,13 @@ temp_working_dirt::temp_working_dirt(const std::string &name_template):
101109
{
102110
old_working_directory=get_current_working_directory();
103111
if(chdir(path.c_str())!=0)
104-
CHECK_RETURN(false);
112+
throw system_exceptiont("Couldn't change directory to " + path);
105113
}
106114

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

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
@@ -10,6 +10,7 @@ Author: Daniel Kroening, [email protected]
1010

1111
#include <ostream>
1212

13+
#include "exception_utils.h"
1314
#include "string2int.h"
1415

1516
void xmlt::clear()
@@ -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)