Artifact f2e2a2e8ce35616073c5978aa276279374894382207cba584ba594714e16cb99:
- File
src/exceptions/OFAddressTranslationFailedException.m
— part of check-in
[62e2de30b9]
at
2015-02-16 08:39:17
on branch trunk
— Explicitly pass errno to exceptions
The old behaviour where the exception would access errno directly on
creation of the exception was very fragile. The two main problems with
it were that sometimes it would pick up an errno even though none had
been set and in other cases that when the exception was created errno
had already been overridden.This also greatly increases errno handling on Win32, especially in
conjunction with sockets. It can still be improved further, though. (user: js, size: 3351) [annotate] [blame] [check-ins using]
/* * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 * Jonathan Schleifer <js@webkeks.org> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "OFAddressTranslationFailedException.h" #import "OFString.h" #import "OFInitializationFailedException.h" #import "OFLockFailedException.h" #import "OFUnlockFailedException.h" #include "socket_helpers.h" #if !defined(HAVE_GETADDRINFO) && defined(OF_HAVE_THREADS) # include "threading.h" static of_mutex_t mutex; #endif @implementation OFAddressTranslationFailedException #if !defined(HAVE_GETADDRINFO) && defined(OF_HAVE_THREADS) + (void)initialize { if (self != [OFAddressTranslationFailedException class]) return; if (!of_mutex_new(&mutex)) @throw [OFInitializationFailedException exception]; } #endif + (instancetype)exceptionWithHost: (OFString*)host { return [[[self alloc] initWithHost: host] autorelease]; } + (instancetype)exceptionWithHost: (OFString*)host error: (int)error { return [[[self alloc] initWithHost: host error: error] autorelease]; } + (instancetype)exceptionWithError: (int)error { return [[[self alloc] initWithError: error] autorelease]; } - initWithHost: (OFString*)host { self = [super init]; @try { _host = [host copy]; } @catch (id e) { [self release]; @throw e; } return self; } - initWithHost: (OFString*)host error: (int)error { self = [super init]; @try { _host = [host copy]; _error = error; } @catch (id e) { [self release]; @throw e; } return self; } - initWithError: (int)error { self = [super init]; _error = error; return self; } - (void)dealloc { [_host release]; [super dealloc]; } - (OFString*)description { /* FIXME: Add proper description for Win32 */ #ifndef _WIN32 if (_error == 0) { #endif if (_host != nil) return [OFString stringWithFormat: @"The host %@ could not be translated to an " @"address!", _host]; else return @"An address could not be translated!"; #ifndef _WIN32 } # ifdef HAVE_GETADDRINFO if (_host != nil) return [OFString stringWithFormat: @"The host %@ could not be translated to an address: %s", _host, gai_strerror(_error)]; else return [OFString stringWithFormat: @"An address could not be translated: %s", gai_strerror(_error)]; # else # ifdef OF_HAVE_THREADS if (!of_mutex_lock(&mutex)) @throw [OFLockFailedException exception]; @try { # endif if (_host != nil) return [OFString stringWithFormat: @"The host %@ could not be translated to an " "address: %s", _host, hstrerror(_error)]; else return [OFString stringWithFormat: @"An address could not be translated: %s", hstrerror(_error)]; # ifdef OF_HAVE_THREADS } @finally { if (!of_mutex_unlock(&mutex)) @throw [OFUnlockFailedException exception]; } # endif # endif #endif } - (OFString*)host { OF_GETTER(_host, true) } @end