Skip to content

Commit

Permalink
Return error if task directory is bad
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Frith-Macdonald committed Oct 10, 2023
1 parent 9d0887e commit a5a9423
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
2023-10-10 Richard Frith-Macdonald <[email protected]>

* Source/GSPrivate.h: New method to set up system error information.
* Source/Additions/NSError+GNUstepBase.m: Implement method.
* Source/NSTask.m(launchAndReturnError:): Return an error if the
path to launch in does not exist, is not a directory or is not
accessible.

2023-10-09 Wolfgang Lux <[email protected]>

* Source/NSTask.m(launchAndReturnError:): Use _exit instead of
Expand Down
15 changes: 15 additions & 0 deletions Source/Additions/NSError+GNUstepBase.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#import "Foundation/NSDictionary.h"
#import "Foundation/NSError.h"
#import "Foundation/NSException.h"
#import "Foundation/NSLock.h"
#import "GSPrivate.h"

Expand Down Expand Up @@ -167,4 +168,18 @@ + (NSError*) _systemError: (long)code
error = [self errorWithDomain: domain code: code userInfo: info];
return error;
}

- (void) _setObject: (NSObject*)anObject forKey: (NSString*)aKey
{
NSMutableDictionary *ui = (NSMutableDictionary*)[self userInfo];

NSAssert([anObject isKindOfClass: [NSObject class]],
NSInvalidArgumentException);
NSAssert([aKey isKindOfClass: [NSString class]],
NSInvalidArgumentException);
NSAssert([ui isKindOfClass: [NSMutableDictionary class]],
NSGenericException);
[ui setObject: anObject forKey: aKey];
}

@end
1 change: 1 addition & 0 deletions Source/GSPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ typedef enum {
@interface NSError (GNUstepBase)
+ (NSError*) _last;
+ (NSError*) _systemError: (long)number;
- (void) _setObject: (NSObject*)anObject forKey: (NSString*)aKey;
@end

@class NSRunLoop;
Expand Down
45 changes: 41 additions & 4 deletions Source/NSTask.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@

#import "common.h"
#define EXPOSE_NSTask_IVARS 1
#import "Foundation/FoundationErrors.h"
#import "Foundation/NSAutoreleasePool.h"
#import "Foundation/NSCharacterSet.h"
#import "Foundation/NSData.h"
#import "Foundation/NSDate.h"
#import "Foundation/NSEnumerator.h"
#import "Foundation/NSError.h"
#import "Foundation/NSException.h"
#import "Foundation/NSFileHandle.h"
#import "Foundation/NSFileManager.h"
Expand Down Expand Up @@ -339,7 +341,7 @@ - (NSString*) currentDirectoryPath
if (_currentDirectoryPath == nil)
{
[self setCurrentDirectoryPath:
[[NSFileManager defaultManager] currentDirectoryPath]];
[[NSFileManager defaultManager] currentDirectoryPath]];
}
return _currentDirectoryPath;
}
Expand Down Expand Up @@ -1526,8 +1528,12 @@ @implementation NSConcreteUnixTask
// 10.13 method...
- (BOOL) launchAndReturnError: (NSError **)error
{
NSFileManager *mgr = [NSFileManager defaultManager];
NSDictionary *info;
NSMutableArray *toClose;
NSString *lpath;
NSString *cwd;
BOOL ok;
int pid;
const char *executable;
const char *path;
Expand All @@ -1548,8 +1554,6 @@ - (BOOL) launchAndReturnError: (NSError **)error
{
if (error)
{
NSDictionary *info;

info = [NSDictionary dictionaryWithObjectsAndKeys:
@"task has already been launched", NSLocalizedDescriptionKey, nil];
*error = [NSError errorWithDomain: NSCocoaErrorDomain
Expand Down Expand Up @@ -1589,7 +1593,40 @@ - (BOOL) launchAndReturnError: (NSError **)error
}
envl[ec] = 0;

path = [[self currentDirectoryPath] fileSystemRepresentation];
cwd = [self currentDirectoryPath];
if (NO == [mgr fileExistsAtPath: cwd isDirectory: &ok])
{
if (error)
{
info = [NSDictionary dictionaryWithObjectsAndKeys:
@"does not exist", NSLocalizedDescriptionKey,
cwd, NSFilePathErrorKey,
nil];
*error = [NSError errorWithDomain: NSCocoaErrorDomain
code: NSFileNoSuchFileError
userInfo: info];
}
return NO;
}
if (NO == ok)
{
if (error)
{
*error = [NSError _systemError: ENOTDIR];
[*error _setObject: cwd forKey: NSFilePathErrorKey];
}
return NO;
}
if (NO == [mgr isExecutableFileAtPath: cwd])
{
if (error)
{
*error = [NSError _systemError: EACCES];
[*error _setObject: cwd forKey: NSFilePathErrorKey];
}
return NO;
}
path = [cwd fileSystemRepresentation];

toClose = [NSMutableArray arrayWithCapacity: 3];
hdl = [self standardInput];
Expand Down

0 comments on commit a5a9423

Please sign in to comment.