Skip to content

Commit 50fd483

Browse files
Cleanup invariants and throws in util t-z
1 parent 49431be commit 50fd483

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
@@ -32,7 +32,7 @@ Author: CM Wintersteiger
3232
#include <unistd.h>
3333
#endif
3434

35-
#include "invariant.h"
35+
#include "exception_utils.h"
3636
#include "file_util.h"
3737

3838
std::string get_temporary_directory(const std::string &name_template)
@@ -45,7 +45,9 @@ std::string get_temporary_directory(const std::string &name_template)
4545
DWORD dwRetVal = GetTempPathA(dwBufSize, lpPathBuffer);
4646

4747
if(dwRetVal > dwBufSize || (dwRetVal == 0))
48-
throw "GetTempPath failed"; // NOLINT(readability/throw)
48+
{
49+
throw system_exceptiont("Couldn't get temporary path");
50+
}
4951

5052
// GetTempFileNameA produces <path>\<pre><uuuu>.TMP
5153
// where <pre> = "TLO"
@@ -54,12 +56,18 @@ std::string get_temporary_directory(const std::string &name_template)
5456
char t[MAX_PATH];
5557
UINT uRetVal=GetTempFileNameA(lpPathBuffer, "TLO", 0, t);
5658
if(uRetVal == 0)
57-
throw "GetTempFileName failed"; // NOLINT(readability/throw)
59+
{
60+
throw system_exceptiont(
61+
std::string("Couldn't get new temporary file name in directory") +
62+
lpPathBuffer);
63+
}
5864

5965
unlink(t);
60-
if(_mkdir(t)!=0)
61-
throw "_mkdir failed";
62-
66+
if(_mkdir(t) != 0)
67+
{
68+
throw system_exceptiont(
69+
std::string("Couldn't create temporary directory at ") + t);
70+
}
6371
result=std::string(t);
6472

6573
#else
@@ -75,7 +83,7 @@ std::string get_temporary_directory(const std::string &name_template)
7583
t.push_back('\0'); // add the zero
7684
const char *td = mkdtemp(t.data());
7785
if(!td)
78-
throw "mkdtemp failed";
86+
throw system_exceptiont("Failed to create temporary directory");
7987
result=std::string(td);
8088
#endif
8189

@@ -107,11 +115,13 @@ temp_working_dirt::temp_working_dirt(const std::string &name_template):
107115
{
108116
old_working_directory=get_current_working_directory();
109117
if(chdir(path.c_str())!=0)
110-
CHECK_RETURN(false);
118+
throw system_exceptiont("Couldn't change directory to " + path);
111119
}
112120

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

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
@@ -31,6 +31,8 @@ Author: Daniel Kroening
3131
#include <cstdio>
3232
#include <cstring>
3333

34+
#include "exception_utils.h"
35+
3436
#if defined(__linux__) || \
3537
defined(__FreeBSD_kernel__) || \
3638
defined(__GNU__) || \
@@ -104,7 +106,7 @@ std::string get_temporary_file(
104106
lpTempPathBuffer); // buffer for path
105107

106108
if(dwRetVal>MAX_PATH || (dwRetVal==0))
107-
throw "GetTempPath failed"; // NOLINT(readability/throw)
109+
throw system_exceptiont("Failed to get temporary directory");
108110

109111
// the path returned by GetTempPath ends with a backslash
110112
std::string t_template=
@@ -127,7 +129,7 @@ std::string get_temporary_file(
127129
int fd=mkstemps(t_ptr, suffix.size());
128130

129131
if(fd<0)
130-
throw "mkstemps failed";
132+
throw system_exceptiont("Failed to open temporary file");
131133

132134
close(fd);
133135

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)