1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-10 01:48:00 +00:00

Make new_error_socket() into a printf-style function.

Almost all the call sites were doing a cumbersome dupprintf-use-free
cycle to get a formatted message into an ErrorSocket anyway, so it
seems more sensible to give them an easier way of doing so.

The few call sites that were passing a constant string literal
_shouldn't_ have been - they'll be all the better for adding a
strerror suffix to the message they were previously giving!
This commit is contained in:
Simon Tatham 2018-10-07 14:47:16 +01:00
parent 55860cace7
commit cea1329b9e
5 changed files with 38 additions and 37 deletions

2
Recipe
View File

@ -270,7 +270,7 @@ MISC = misc marshal
MISCNET = timing callback MISC version settings tree234 proxy CONF be_misc MISCNET = timing callback MISC version settings tree234 proxy CONF be_misc
WINMISC = MISCNET winstore winnet winhandl cmdline windefs winmisc winproxy WINMISC = MISCNET winstore winnet winhandl cmdline windefs winmisc winproxy
+ wintime winhsock errsock winsecur winucs miscucs + wintime winhsock errsock winsecur winucs miscucs
UXMISC = MISCNET uxstore uxsel uxnet uxpeer uxmisc uxproxy time UXMISC = MISCNET uxstore uxsel uxnet uxpeer uxmisc uxproxy errsock time
# import.c and dependencies, for PuTTYgen-like utilities that have to # import.c and dependencies, for PuTTYgen-like utilities that have to
# load foreign key files. # load foreign key files.

View File

@ -56,11 +56,23 @@ static const SocketVtable ErrorSocket_sockvt = {
sk_error_peer_info, sk_error_peer_info,
}; };
Socket *new_error_socket(const char *errmsg, Plug *plug) static Socket *new_error_socket_internal(char *errmsg, Plug *plug)
{ {
ErrorSocket *es = snew(ErrorSocket); ErrorSocket *es = snew(ErrorSocket);
es->sock.vt = &ErrorSocket_sockvt; es->sock.vt = &ErrorSocket_sockvt;
es->plug = plug; es->plug = plug;
es->error = dupstr(errmsg); es->error = errmsg;
return &es->sock; return &es->sock;
} }
Socket *new_error_socket_fmt(Plug *plug, const char *fmt, ...)
{
va_list ap;
char *msg;
va_start(ap, fmt);
msg = dupvprintf(fmt, ap);
va_end(ap);
return new_error_socket_internal(msg, plug);
}

View File

@ -220,7 +220,7 @@ char *get_hostname(void);
* Trivial socket implementation which just stores an error. Found in * Trivial socket implementation which just stores an error. Found in
* errsock.c. * errsock.c.
*/ */
Socket *new_error_socket(const char *errmsg, Plug *plug); Socket *new_error_socket_fmt(Plug *plug, const char *fmt, ...);
/* /*
* Trivial plug that does absolutely nothing. Found in nullplug.c. * Trivial plug that does absolutely nothing. Found in nullplug.c.

View File

@ -23,7 +23,6 @@ Socket *new_named_pipe_client(const char *pipename, Plug *plug)
HANDLE pipehandle; HANDLE pipehandle;
PSID usersid, pipeowner; PSID usersid, pipeowner;
PSECURITY_DESCRIPTOR psd; PSECURITY_DESCRIPTOR psd;
char *err;
Socket *ret; Socket *ret;
assert(strncmp(pipename, "\\\\.\\pipe\\", 9) == 0); assert(strncmp(pipename, "\\\\.\\pipe\\", 9) == 0);
@ -38,11 +37,9 @@ Socket *new_named_pipe_client(const char *pipename, Plug *plug)
break; break;
if (GetLastError() != ERROR_PIPE_BUSY) { if (GetLastError() != ERROR_PIPE_BUSY) {
err = dupprintf("Unable to open named pipe '%s': %s", return new_error_socket_fmt(
pipename, win_strerror(GetLastError())); plug, "Unable to open named pipe '%s': %s",
ret = new_error_socket(err, plug); pipename, win_strerror(GetLastError()));
sfree(err);
return ret;
} }
/* /*
@ -53,41 +50,33 @@ Socket *new_named_pipe_client(const char *pipename, Plug *plug)
* take excessively long.) * take excessively long.)
*/ */
if (!WaitNamedPipe(pipename, NMPWAIT_USE_DEFAULT_WAIT)) { if (!WaitNamedPipe(pipename, NMPWAIT_USE_DEFAULT_WAIT)) {
err = dupprintf("Error waiting for named pipe '%s': %s", return new_error_socket_fmt(
pipename, win_strerror(GetLastError())); plug, "Error waiting for named pipe '%s': %s",
ret = new_error_socket(err, plug); pipename, win_strerror(GetLastError()));
sfree(err);
return ret;
} }
} }
if ((usersid = get_user_sid()) == NULL) { if ((usersid = get_user_sid()) == NULL) {
CloseHandle(pipehandle); CloseHandle(pipehandle);
err = dupprintf("Unable to get user SID"); return new_error_socket_fmt(
ret = new_error_socket(err, plug); plug, "Unable to get user SID: %s", win_strerror(GetLastError()));
sfree(err);
return ret;
} }
if (p_GetSecurityInfo(pipehandle, SE_KERNEL_OBJECT, if (p_GetSecurityInfo(pipehandle, SE_KERNEL_OBJECT,
OWNER_SECURITY_INFORMATION, OWNER_SECURITY_INFORMATION,
&pipeowner, NULL, NULL, NULL, &pipeowner, NULL, NULL, NULL,
&psd) != ERROR_SUCCESS) { &psd) != ERROR_SUCCESS) {
err = dupprintf("Unable to get named pipe security information: %s",
win_strerror(GetLastError()));
ret = new_error_socket(err, plug);
sfree(err);
CloseHandle(pipehandle); CloseHandle(pipehandle);
return ret; return new_error_socket_fmt(
plug, "Unable to get named pipe security information: %s",
win_strerror(GetLastError()));
} }
if (!EqualSid(pipeowner, usersid)) { if (!EqualSid(pipeowner, usersid)) {
err = dupprintf("Owner of named pipe '%s' is not us", pipename);
ret = new_error_socket(err, plug);
sfree(err);
CloseHandle(pipehandle); CloseHandle(pipehandle);
LocalFree(psd); LocalFree(psd);
return ret; return new_error_socket_fmt(
plug, "Owner of named pipe '%s' is not us", pipename);
} }
LocalFree(psd); LocalFree(psd);

View File

@ -50,30 +50,30 @@ Socket *platform_new_connection(SockAddr *addr, const char *hostname,
sa.lpSecurityDescriptor = NULL; /* default */ sa.lpSecurityDescriptor = NULL; /* default */
sa.bInheritHandle = TRUE; sa.bInheritHandle = TRUE;
if (!CreatePipe(&us_from_cmd, &cmd_to_us, &sa, 0)) { if (!CreatePipe(&us_from_cmd, &cmd_to_us, &sa, 0)) {
Socket *ret =
new_error_socket("Unable to create pipes for proxy command", plug);
sfree(cmd); sfree(cmd);
return ret; return new_error_socket_fmt(
plug, "Unable to create pipes for proxy command: %s",
win_strerror(GetLastError()));
} }
if (!CreatePipe(&cmd_from_us, &us_to_cmd, &sa, 0)) { if (!CreatePipe(&cmd_from_us, &us_to_cmd, &sa, 0)) {
Socket *ret =
new_error_socket("Unable to create pipes for proxy command", plug);
sfree(cmd); sfree(cmd);
CloseHandle(us_from_cmd); CloseHandle(us_from_cmd);
CloseHandle(cmd_to_us); CloseHandle(cmd_to_us);
return ret; return new_error_socket_fmt(
plug, "Unable to create pipes for proxy command: %s",
win_strerror(GetLastError()));
} }
if (!CreatePipe(&us_from_cmd_err, &cmd_err_to_us, &sa, 0)) { if (!CreatePipe(&us_from_cmd_err, &cmd_err_to_us, &sa, 0)) {
Socket *ret = new_error_socket
("Unable to create pipes for proxy command", plug);
sfree(cmd); sfree(cmd);
CloseHandle(us_from_cmd); CloseHandle(us_from_cmd);
CloseHandle(cmd_to_us); CloseHandle(cmd_to_us);
CloseHandle(us_to_cmd); CloseHandle(us_to_cmd);
CloseHandle(cmd_from_us); CloseHandle(cmd_from_us);
return ret; return new_error_socket_fmt(
plug, "Unable to create pipes for proxy command: %s",
win_strerror(GetLastError()));
} }
SetHandleInformation(us_to_cmd, HANDLE_FLAG_INHERIT, 0); SetHandleInformation(us_to_cmd, HANDLE_FLAG_INHERIT, 0);