Skip to content

Commit f7507ae

Browse files
committed
Merge branch 'stdexcept' of github.com:CendioOssman/tigervnc
2 parents 7508e98 + 2b78572 commit f7507ae

File tree

131 files changed

+1154
-1058
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+1154
-1058
lines changed

common/network/Socket.cxx

+5-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#include <fcntl.h>
4040
#include <errno.h>
4141

42+
#include <rdr/Exception.h>
43+
4244
#include <network/Socket.h>
4345

4446
using namespace network;
@@ -53,7 +55,7 @@ void network::initSockets() {
5355
WSADATA initResult;
5456

5557
if (WSAStartup(requiredVersion, &initResult) != 0)
56-
throw rdr::SocketException("unable to initialise Winsock2", errorNumber);
58+
throw rdr::socket_error("unable to initialise Winsock2", errorNumber);
5759
#else
5860
signal(SIGPIPE, SIG_IGN);
5961
#endif
@@ -161,7 +163,7 @@ Socket* SocketListener::accept() {
161163

162164
// Accept an incoming connection
163165
if ((new_sock = ::accept(fd, nullptr, nullptr)) < 0)
164-
throw rdr::SocketException("unable to accept new connection", errorNumber);
166+
throw rdr::socket_error("unable to accept new connection", errorNumber);
165167

166168
// Create the socket object & check connection is allowed
167169
Socket* s = createSocket(new_sock);
@@ -179,7 +181,7 @@ void SocketListener::listen(int sock)
179181
if (::listen(sock, 5) < 0) {
180182
int e = errorNumber;
181183
closesocket(sock);
182-
throw rdr::SocketException("unable to set socket to listening mode", e);
184+
throw rdr::socket_error("unable to set socket to listening mode", e);
183185
}
184186

185187
fd = sock;

common/network/TcpSocket.cxx

+24-20
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838
#include <stdlib.h>
3939
#include <unistd.h>
4040

41+
#include <rdr/Exception.h>
42+
4143
#include <network/TcpSocket.h>
44+
4245
#include <rfb/LogWriter.h>
4346
#include <rfb/Configuration.h>
4447
#include <rfb/util.h>
@@ -82,15 +85,15 @@ int network::findFreeTcpPort (void)
8285
addr.sin_addr.s_addr = INADDR_ANY;
8386

8487
if ((sock = socket (AF_INET, SOCK_STREAM, 0)) < 0)
85-
throw SocketException ("unable to create socket", errorNumber);
88+
throw socket_error("unable to create socket", errorNumber);
8689

8790
addr.sin_port = 0;
8891
if (bind (sock, (struct sockaddr *)&addr, sizeof (addr)) < 0)
89-
throw SocketException ("unable to find free port", errorNumber);
92+
throw socket_error("unable to find free port", errorNumber);
9093

9194
socklen_t n = sizeof(addr);
9295
if (getsockname (sock, (struct sockaddr *)&addr, &n) < 0)
93-
throw SocketException ("unable to get port number", errorNumber);
96+
throw socket_error("unable to get port number", errorNumber);
9497

9598
closesocket (sock);
9699
return ntohs(addr.sin_port);
@@ -134,7 +137,7 @@ TcpSocket::TcpSocket(const char *host, int port)
134137
hints.ai_next = nullptr;
135138

136139
if ((result = getaddrinfo(host, nullptr, &hints, &ai)) != 0) {
137-
throw GAIException("unable to resolve host by name", result);
140+
throw getaddrinfo_error("unable to resolve host by name", result);
138141
}
139142

140143
sock = -1;
@@ -175,7 +178,7 @@ TcpSocket::TcpSocket(const char *host, int port)
175178
if (sock == -1) {
176179
err = errorNumber;
177180
freeaddrinfo(ai);
178-
throw SocketException("unable to create socket", err);
181+
throw socket_error("unable to create socket", err);
179182
}
180183

181184
/* Attempt to connect to the remote host */
@@ -200,9 +203,9 @@ TcpSocket::TcpSocket(const char *host, int port)
200203

201204
if (sock == -1) {
202205
if (err == 0)
203-
throw Exception("No useful address for host");
206+
throw std::runtime_error("No useful address for host");
204207
else
205-
throw SocketException("unable to connect to socket", err);
208+
throw socket_error("unable to connect to socket", err);
206209
}
207210

208211
// Take proper ownership of the socket
@@ -299,15 +302,15 @@ TcpListener::TcpListener(const struct sockaddr *listenaddr,
299302
int sock;
300303

301304
if ((sock = socket (listenaddr->sa_family, SOCK_STREAM, 0)) < 0)
302-
throw SocketException("unable to create listening socket", errorNumber);
305+
throw socket_error("unable to create listening socket", errorNumber);
303306

304307
memcpy (&sa, listenaddr, listenaddrlen);
305308
#ifdef IPV6_V6ONLY
306309
if (listenaddr->sa_family == AF_INET6) {
307310
if (setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&one, sizeof(one))) {
308311
int e = errorNumber;
309312
closesocket(sock);
310-
throw SocketException("unable to set IPV6_V6ONLY", e);
313+
throw socket_error("unable to set IPV6_V6ONLY", e);
311314
}
312315
}
313316
#endif /* defined(IPV6_V6ONLY) */
@@ -325,14 +328,14 @@ TcpListener::TcpListener(const struct sockaddr *listenaddr,
325328
(char *)&one, sizeof(one)) < 0) {
326329
int e = errorNumber;
327330
closesocket(sock);
328-
throw SocketException("unable to create listening socket", e);
331+
throw socket_error("unable to create listening socket", e);
329332
}
330333
#endif
331334

332335
if (bind(sock, &sa.u.sa, listenaddrlen) == -1) {
333336
int e = errorNumber;
334337
closesocket(sock);
335-
throw SocketException("failed to bind socket", e);
338+
throw socket_error("failed to bind socket", e);
336339
}
337340

338341
listen(sock);
@@ -443,7 +446,7 @@ void network::createTcpListeners(std::list<SocketListener*> *listeners,
443446
snprintf (service, sizeof (service) - 1, "%d", port);
444447
service[sizeof (service) - 1] = '\0';
445448
if ((result = getaddrinfo(addr, service, &hints, &ai)) != 0)
446-
throw GAIException("unable to resolve listening address", result);
449+
throw getaddrinfo_error("unable to resolve listening address", result);
447450

448451
try {
449452
createTcpListeners(listeners, ai);
@@ -482,7 +485,7 @@ void network::createTcpListeners(std::list<SocketListener*> *listeners,
482485
try {
483486
new_listeners.push_back(new TcpListener(current->ai_addr,
484487
current->ai_addrlen));
485-
} catch (SocketException& e) {
488+
} catch (socket_error& e) {
486489
// Ignore this if it is due to lack of address family support on
487490
// the interface or on the system
488491
if (e.err != EADDRNOTAVAIL && e.err != EAFNOSUPPORT) {
@@ -607,7 +610,7 @@ TcpFilter::Pattern TcpFilter::parsePattern(const char* p) {
607610

608611
parts = rfb::split(&p[1], '/');
609612
if (parts.size() > 2)
610-
throw Exception("invalid filter specified");
613+
throw std::invalid_argument("invalid filter specified");
611614

612615
if (parts[0].empty()) {
613616
// Match any address
@@ -630,7 +633,7 @@ TcpFilter::Pattern TcpFilter::parsePattern(const char* p) {
630633
}
631634

632635
if ((result = getaddrinfo (parts[0].c_str(), nullptr, &hints, &ai)) != 0) {
633-
throw GAIException("unable to resolve host by name", result);
636+
throw getaddrinfo_error("unable to resolve host by name", result);
634637
}
635638

636639
memcpy (&pattern.address.u.sa, ai->ai_addr, ai->ai_addrlen);
@@ -641,8 +644,8 @@ TcpFilter::Pattern TcpFilter::parsePattern(const char* p) {
641644
if (parts.size() > 1) {
642645
if (family == AF_INET &&
643646
(parts[1].find('.') != std::string::npos)) {
644-
throw Exception("mask no longer supported for filter, "
645-
"use prefix instead");
647+
throw std::invalid_argument("mask no longer supported for "
648+
"filter, use prefix instead");
646649
}
647650

648651
pattern.prefixlen = (unsigned int) atoi(parts[1].c_str());
@@ -655,16 +658,17 @@ TcpFilter::Pattern TcpFilter::parsePattern(const char* p) {
655658
pattern.prefixlen = 128;
656659
break;
657660
default:
658-
throw Exception("unknown address family");
661+
throw std::runtime_error("unknown address family");
659662
}
660663
}
661664
}
662665

663666
family = pattern.address.u.sa.sa_family;
664667

665668
if (pattern.prefixlen > (family == AF_INET ? 32: 128))
666-
throw Exception("invalid prefix length for filter address: %u",
667-
pattern.prefixlen);
669+
throw std::invalid_argument(rfb::format("invalid prefix length for "
670+
"filter address: %u",
671+
pattern.prefixlen));
668672

669673
// Compute mask from address and prefix length
670674
memset (&pattern.mask, 0, sizeof (pattern.mask));

common/network/UnixSocket.cxx

+10-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
#include <stdlib.h>
3030
#include <stddef.h>
3131

32+
#include <rdr/Exception.h>
33+
3234
#include <network/UnixSocket.h>
35+
3336
#include <rfb/LogWriter.h>
3437

3538
using namespace network;
@@ -50,12 +53,12 @@ UnixSocket::UnixSocket(const char *path)
5053
socklen_t salen;
5154

5255
if (strlen(path) >= sizeof(addr.sun_path))
53-
throw SocketException("socket path is too long", ENAMETOOLONG);
56+
throw socket_error("socket path is too long", ENAMETOOLONG);
5457

5558
// - Create a socket
5659
sock = socket(AF_UNIX, SOCK_STREAM, 0);
5760
if (sock == -1)
58-
throw SocketException("unable to create socket", errno);
61+
throw socket_error("unable to create socket", errno);
5962

6063
// - Attempt to connect
6164
memset(&addr, 0, sizeof(addr));
@@ -69,7 +72,7 @@ UnixSocket::UnixSocket(const char *path)
6972
}
7073

7174
if (result == -1)
72-
throw SocketException("unable to connect to socket", err);
75+
throw socket_error("unable to connect to socket", err);
7376

7477
setFd(sock);
7578
}
@@ -116,11 +119,11 @@ UnixListener::UnixListener(const char *path, int mode)
116119
int err, result;
117120

118121
if (strlen(path) >= sizeof(addr.sun_path))
119-
throw SocketException("socket path is too long", ENAMETOOLONG);
122+
throw socket_error("socket path is too long", ENAMETOOLONG);
120123

121124
// - Create a socket
122125
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
123-
throw SocketException("unable to create listening socket", errno);
126+
throw socket_error("unable to create listening socket", errno);
124127

125128
// - Delete existing socket (ignore result)
126129
unlink(path);
@@ -135,14 +138,14 @@ UnixListener::UnixListener(const char *path, int mode)
135138
umask(saved_umask);
136139
if (result < 0) {
137140
close(fd);
138-
throw SocketException("unable to bind listening socket", err);
141+
throw socket_error("unable to bind listening socket", err);
139142
}
140143

141144
// - Set socket mode
142145
if (chmod(path, mode) < 0) {
143146
err = errno;
144147
close(fd);
145-
throw SocketException("unable to set socket mode", err);
148+
throw socket_error("unable to set socket mode", err);
146149
}
147150

148151
listen(fd);

common/os/Mutex.cxx

+8-8
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Mutex::Mutex()
4343
systemMutex = new pthread_mutex_t;
4444
ret = pthread_mutex_init((pthread_mutex_t*)systemMutex, nullptr);
4545
if (ret != 0)
46-
throw rdr::PosixException("Failed to create mutex", ret);
46+
throw rdr::posix_error("Failed to create mutex", ret);
4747
#endif
4848
}
4949

@@ -67,7 +67,7 @@ void Mutex::lock()
6767

6868
ret = pthread_mutex_lock((pthread_mutex_t*)systemMutex);
6969
if (ret != 0)
70-
throw rdr::PosixException("Failed to lock mutex", ret);
70+
throw rdr::posix_error("Failed to lock mutex", ret);
7171
#endif
7272
}
7373

@@ -80,7 +80,7 @@ void Mutex::unlock()
8080

8181
ret = pthread_mutex_unlock((pthread_mutex_t*)systemMutex);
8282
if (ret != 0)
83-
throw rdr::PosixException("Failed to unlock mutex", ret);
83+
throw rdr::posix_error("Failed to unlock mutex", ret);
8484
#endif
8585
}
8686

@@ -97,7 +97,7 @@ Condition::Condition(Mutex* mutex_)
9797
systemCondition = new pthread_cond_t;
9898
ret = pthread_cond_init((pthread_cond_t*)systemCondition, nullptr);
9999
if (ret != 0)
100-
throw rdr::PosixException("Failed to create condition variable", ret);
100+
throw rdr::posix_error("Failed to create condition variable", ret);
101101
#endif
102102
}
103103

@@ -120,14 +120,14 @@ void Condition::wait()
120120
(CRITICAL_SECTION*)mutex->systemMutex,
121121
INFINITE);
122122
if (!ret)
123-
throw rdr::Win32Exception("Failed to wait on condition variable", GetLastError());
123+
throw rdr::win32_error("Failed to wait on condition variable", GetLastError());
124124
#else
125125
int ret;
126126

127127
ret = pthread_cond_wait((pthread_cond_t*)systemCondition,
128128
(pthread_mutex_t*)mutex->systemMutex);
129129
if (ret != 0)
130-
throw rdr::PosixException("Failed to wait on condition variable", ret);
130+
throw rdr::posix_error("Failed to wait on condition variable", ret);
131131
#endif
132132
}
133133

@@ -140,7 +140,7 @@ void Condition::signal()
140140

141141
ret = pthread_cond_signal((pthread_cond_t*)systemCondition);
142142
if (ret != 0)
143-
throw rdr::PosixException("Failed to signal condition variable", ret);
143+
throw rdr::posix_error("Failed to signal condition variable", ret);
144144
#endif
145145
}
146146

@@ -153,6 +153,6 @@ void Condition::broadcast()
153153

154154
ret = pthread_cond_broadcast((pthread_cond_t*)systemCondition);
155155
if (ret != 0)
156-
throw rdr::PosixException("Failed to broadcast condition variable", ret);
156+
throw rdr::posix_error("Failed to broadcast condition variable", ret);
157157
#endif
158158
}

common/os/Thread.cxx

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void Thread::start()
6666
#ifdef WIN32
6767
*(HANDLE*)threadId = CreateThread(nullptr, 0, startRoutine, this, 0, nullptr);
6868
if (*(HANDLE*)threadId == nullptr)
69-
throw rdr::Win32Exception("Failed to create thread", GetLastError());
69+
throw rdr::win32_error("Failed to create thread", GetLastError());
7070
#else
7171
int ret;
7272
sigset_t all, old;
@@ -76,14 +76,14 @@ void Thread::start()
7676
sigfillset(&all);
7777
ret = pthread_sigmask(SIG_SETMASK, &all, &old);
7878
if (ret != 0)
79-
throw rdr::PosixException("Failed to mask signals", ret);
79+
throw rdr::posix_error("Failed to mask signals", ret);
8080

8181
ret = pthread_create((pthread_t*)threadId, nullptr, startRoutine, this);
8282

8383
pthread_sigmask(SIG_SETMASK, &old, nullptr);
8484

8585
if (ret != 0)
86-
throw rdr::PosixException("Failed to create thread", ret);
86+
throw rdr::posix_error("Failed to create thread", ret);
8787
#endif
8888

8989
running = true;
@@ -99,13 +99,13 @@ void Thread::wait()
9999

100100
ret = WaitForSingleObject(*(HANDLE*)threadId, INFINITE);
101101
if (ret != WAIT_OBJECT_0)
102-
throw rdr::Win32Exception("Failed to join thread", GetLastError());
102+
throw rdr::win32_error("Failed to join thread", GetLastError());
103103
#else
104104
int ret;
105105

106106
ret = pthread_join(*(pthread_t*)threadId, nullptr);
107107
if (ret != 0)
108-
throw rdr::PosixException("Failed to join thread", ret);
108+
throw rdr::posix_error("Failed to join thread", ret);
109109
#endif
110110
}
111111

common/os/winerrno.h

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ cat /usr/i686-pc-mingw32/sys-root/mingw/include/winerror.h \
55
| egrep -v 'EINTR|EBADF|EACCES|EFAULT|EINVAL|EMFILE|_QOS|PROVIDER|PROCTABLE'
66
*/
77

8+
#include <winsock2.h>
9+
810
#undef EWOULDBLOCK
911
#define EWOULDBLOCK WSAEWOULDBLOCK
1012
#undef EINPROGRESS

0 commit comments

Comments
 (0)