- [NSValue getValue:] should take a expectedSize argument and yield a success return value
| Originator: | doug | ||
| Number: | rdar://13278122 | Date Originated: | 22-Feb-2013 04:27 PM |
| Status: | Open | Resolved: | |
| Product: | Mac OS X SDK | Product Version: | 10.8 |
| Classification: | Enhancement | Reproducible: | Always |
Summary:
- [NSValue getValue:] offers no way to confirm that a value passed by reference has been initialized.
It also offers no way to provide guidance as to the passed in value references expected size.
Steps to Reproduce:
Use - [NSValue getValue:]
Expected Results:
A return value that indicates success and failure on values where expected size doesn't match
Actual Results:
No expected size or return value.
Regression:
N/A
Notes:
Sample code:
#import <Foundation/Foundation.h>
@interface RSTLValue : NSValue
- (BOOL)getValue:(void *)value expectedSize:(size_t)expectedSize;
@end
struct Foo {
NSUInteger i;
char * bar;
};
int main(int argc, const char * argv[])
{
@autoreleasepool {
struct Foo foo;
foo.i = 1;
foo.bar = "string";
RSTLValue *value = [[RSTLValue alloc] initWithBytes:&foo objCType:@encode(struct Foo)];
struct Foo foo2;
if ([value getValue:&foo2 expectedSize:sizeof(struct Foo)])
{
NSLog(@"%li %s\n", foo2.i, foo2.bar);
}
else
{
NSLog(@"Fail");
}
value = [[RSTLValue alloc] init];
struct Foo foo3;
if ([value getValue:&foo3 expectedSize:sizeof(struct Foo)])
{
NSLog(@"%li %s\n", foo3.i, foo3.bar);
}
else
{
NSLog(@"Fail");
}
}
return 0;
}
@implementation RSTLValue
{
NSValue *_realValue;
size_t _expectedSize;
}
- (instancetype)init
{
self = [super init];
if (self)
{
_expectedSize = 0;
}
return self;
}
- (id)initWithBytes:(const void *)value objCType:(const char *)type
{
self = [self init];
if (self)
{
_realValue = [[NSValue alloc] initWithBytes:value objCType:type];
NSUInteger size;
NSUInteger align;
//const char * retval =
NSGetSizeAndAlignment(type, &size, &align);
_expectedSize = size;
}
return self;
}
- (BOOL)getValue:(void *)value expectedSize:(size_t)expectedSize
{
if (_realValue == nil)
{
return NO;
}
if (_expectedSize != expectedSize)
{
return NO;
}
[_realValue getValue:value];
return YES;
}
- (void)getValue:(void *)value
{
@throw [NSException exceptionWithName:@"" reason:@"" userInfo:nil];
}
- (id)forwardingTargetForSelector:(SEL)aSelector
{
return _realValue;
}
@end
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!