Avoid NSGetSizeAndAlignment bug
authorWincent Colaiuta <win@wincent.com>
Tue, 24 Jul 2007 17:25:12 +0000 (19:25 +0200)
committerWincent Colaiuta <win@wincent.com>
Tue, 24 Jul 2007 17:25:12 +0000 (19:25 +0200)
Leopard has a bug in the NSGetSizeAndAlignment() function which prevents it
from handling structs which have bitfields in them. This in turn breaks the
+[NSValue valueWithBytes:objCType:] method when used with structs containing
bitfields.

So this commit #ifdefs out the tests which fail as a result of this bug. A
simplified testcase demonstrating the problem has be en added to the "other"
subdirectory. A Radar has been filed (<rdar://problem/5357040>); currently
awaiting on a fix from Apple.

Signed-off-by: Wincent Colaiuta <win@wincent.com>
Tests/NSValueTests.m
other/NSGetSizeAndAlignment_bug/NSGetSizeAndAlignment_bug.m [new file with mode: 0644]
other/NSGetSizeAndAlignment_bug/NSGetSizeAndAlignment_bug.xcodeproj/.gitignore [new file with mode: 0644]
other/NSGetSizeAndAlignment_bug/NSGetSizeAndAlignment_bug.xcodeproj/project.pbxproj [new file with mode: 0644]
other/NSGetSizeAndAlignment_bug/NSGetSizeAndAlignment_bug_Prefix.pch [new file with mode: 0644]

index 0bfa236ae15a45605aca56e12a87c6ae49e8df97..ffaaaed0c6730769acdd0576aa47c3a2bd19cd44 100644 (file)
@@ -581,10 +581,17 @@ typedef struct {
     WO_TEST_GTE([NSValue WOTest_sizeForType:[value WOTest_objCTypeString]], sizeof(WOSubstruct));
     WO_TEST_GTE([value WOTest_bufferSize], sizeof(WOSubstruct));
 
+#ifdef RADAR_5357040_FIXED
+    // BUG: +[NSValue valueWithBytes:objCType:] fails in Leopard when passed structs which contain bitfields
+    // Specifically, an NSInvalidArgumentException is raised, ": NSGetSizeAndAlignment(): unsupported type encoding spec 'b'"
+    // So for now just return
+    // See testcase in other/NSGetSizeAndAlignment_bug/, and: <rdar://problem/5357040>
+
     WOComplicatedStruct complicated;
     value = [NSValue valueWithBytes:&complicated objCType:@encode(WOComplicatedStruct)];
     WO_TEST_GTE([NSValue WOTest_sizeForType:[value WOTest_objCTypeString]], sizeof(WOComplicatedStruct));
     WO_TEST_GTE([value WOTest_bufferSize], sizeof(WOComplicatedStruct));
+#endif /** RADAR_5357040_FIXED */
 
     // test with anonymous struct
     WOAnonymousStruct anonymous;
@@ -611,10 +618,17 @@ typedef struct {
     WO_TEST_GTE([value WOTest_bufferSize], sizeof(WOSubunion));
 
     // test with WOComplicatedUnion
+#ifdef RADAR_5357040_FIXED
+    // BUG: +[NSValue valueWithBytes:objCType:] fails in Leopard when passed structs which contain bitfields
+    // Specifically, an NSInvalidArgumentException is raised, ": NSGetSizeAndAlignment(): unsupported type encoding spec 'b'"
+    // So for now just return
+    // See testcase in other/NSGetSizeAndAlignment_bug/, and: <rdar://problem/5357040>
+
     WOComplicatedUnion complicated;
     value = [NSValue valueWithBytes:&complicated objCType:@encode(WOComplicatedUnion)];
     WO_TEST_GTE([NSValue WOTest_sizeForType:[value WOTest_objCTypeString]], sizeof(WOComplicatedUnion));
     WO_TEST_GTE([value WOTest_bufferSize], sizeof(WOComplicatedUnion));
+#endif /* RADAR_5357040_FIXED */
 
     // test with anonymous union
     WOAnonymousUnion anonymous;
diff --git a/other/NSGetSizeAndAlignment_bug/NSGetSizeAndAlignment_bug.m b/other/NSGetSizeAndAlignment_bug/NSGetSizeAndAlignment_bug.m
new file mode 100644 (file)
index 0000000..a147c88
--- /dev/null
@@ -0,0 +1,20 @@
+// Testcase for: rdar://problem/5357040
+
+#import <Foundation/Foundation.h>
+
+typedef struct StructWithBitfield {
+    int bitfield_a : 3;
+    int bitfield_b : 5;
+} StructWithBitfield;
+
+int main (int argc, const char * argv[]) {
+    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
+
+    // here we die with an uncaught exception:
+    // *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSGetSizeAndAlignment(): unsupported type encoding spec 'b' at 'b3b5}' in '{StructWithBitfield=b3b5}''
+    StructWithBitfield myStruct;
+    [NSValue valueWithBytes:&myStruct objCType:@encode(StructWithBitfield)];
+
+    [pool drain];
+    return 0;
+}
diff --git a/other/NSGetSizeAndAlignment_bug/NSGetSizeAndAlignment_bug.xcodeproj/.gitignore b/other/NSGetSizeAndAlignment_bug/NSGetSizeAndAlignment_bug.xcodeproj/.gitignore
new file mode 100644 (file)
index 0000000..9c66114
--- /dev/null
@@ -0,0 +1,4 @@
+*.mode1*
+*.pbxuser
+!default.pbxuser
+
diff --git a/other/NSGetSizeAndAlignment_bug/NSGetSizeAndAlignment_bug.xcodeproj/project.pbxproj b/other/NSGetSizeAndAlignment_bug/NSGetSizeAndAlignment_bug.xcodeproj/project.pbxproj
new file mode 100644 (file)
index 0000000..d9d8569
--- /dev/null
@@ -0,0 +1,214 @@
+// !$*UTF8*$!
+{
+       archiveVersion = 1;
+       classes = {
+       };
+       objectVersion = 43;
+       objects = {
+
+/* Begin PBXBuildFile section */
+               8DD76F9A0486AA7600D96B5E /* NSGetSizeAndAlignment_bug.m in Sources */ = {isa = PBXBuildFile; fileRef = 08FB7796FE84155DC02AAC07 /* NSGetSizeAndAlignment_bug.m */; settings = {ATTRIBUTES = (); }; };
+               8DD76F9C0486AA7600D96B5E /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 08FB779EFE84155DC02AAC07 /* Foundation.framework */; };
+/* End PBXBuildFile section */
+
+/* Begin PBXCopyFilesBuildPhase section */
+               8DD76F9E0486AA7600D96B5E /* CopyFiles */ = {
+                       isa = PBXCopyFilesBuildPhase;
+                       buildActionMask = 8;
+                       dstPath = /usr/share/man/man1/;
+                       dstSubfolderSpec = 0;
+                       files = (
+                       );
+                       runOnlyForDeploymentPostprocessing = 1;
+               };
+/* End PBXCopyFilesBuildPhase section */
+
+/* Begin PBXFileReference section */
+               08FB7796FE84155DC02AAC07 /* NSGetSizeAndAlignment_bug.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSGetSizeAndAlignment_bug.m; sourceTree = "<group>"; };
+               08FB779EFE84155DC02AAC07 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
+               32A70AAB03705E1F00C91783 /* NSGetSizeAndAlignment_bug_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSGetSizeAndAlignment_bug_Prefix.pch; sourceTree = "<group>"; };
+               8DD76FA10486AA7600D96B5E /* NSGetSizeAndAlignment_bug */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = NSGetSizeAndAlignment_bug; sourceTree = BUILT_PRODUCTS_DIR; };
+/* End PBXFileReference section */
+
+/* Begin PBXFrameworksBuildPhase section */
+               8DD76F9B0486AA7600D96B5E /* Frameworks */ = {
+                       isa = PBXFrameworksBuildPhase;
+                       buildActionMask = 2147483647;
+                       files = (
+                               8DD76F9C0486AA7600D96B5E /* Foundation.framework in Frameworks */,
+                       );
+                       runOnlyForDeploymentPostprocessing = 0;
+               };
+/* End PBXFrameworksBuildPhase section */
+
+/* Begin PBXGroup section */
+               08FB7794FE84155DC02AAC07 /* NSGetSizeAndAlignment_bug */ = {
+                       isa = PBXGroup;
+                       children = (
+                               08FB7795FE84155DC02AAC07 /* Source */,
+                               C6859EA2029092E104C91782 /* Documentation */,
+                               08FB779DFE84155DC02AAC07 /* External Frameworks and Libraries */,
+                               1AB674ADFE9D54B511CA2CBB /* Products */,
+                       );
+                       name = NSGetSizeAndAlignment_bug;
+                       sourceTree = "<group>";
+               };
+               08FB7795FE84155DC02AAC07 /* Source */ = {
+                       isa = PBXGroup;
+                       children = (
+                               32A70AAB03705E1F00C91783 /* NSGetSizeAndAlignment_bug_Prefix.pch */,
+                               08FB7796FE84155DC02AAC07 /* NSGetSizeAndAlignment_bug.m */,
+                       );
+                       name = Source;
+                       sourceTree = "<group>";
+               };
+               08FB779DFE84155DC02AAC07 /* External Frameworks and Libraries */ = {
+                       isa = PBXGroup;
+                       children = (
+                               08FB779EFE84155DC02AAC07 /* Foundation.framework */,
+                       );
+                       name = "External Frameworks and Libraries";
+                       sourceTree = "<group>";
+               };
+               1AB674ADFE9D54B511CA2CBB /* Products */ = {
+                       isa = PBXGroup;
+                       children = (
+                               8DD76FA10486AA7600D96B5E /* NSGetSizeAndAlignment_bug */,
+                       );
+                       name = Products;
+                       sourceTree = "<group>";
+               };
+               C6859EA2029092E104C91782 /* Documentation */ = {
+                       isa = PBXGroup;
+                       children = (
+                       );
+                       name = Documentation;
+                       sourceTree = "<group>";
+               };
+/* End PBXGroup section */
+
+/* Begin PBXNativeTarget section */
+               8DD76F960486AA7600D96B5E /* NSGetSizeAndAlignment_bug */ = {
+                       isa = PBXNativeTarget;
+                       buildConfigurationList = 1DEB927408733DD40010E9CD /* Build configuration list for PBXNativeTarget "NSGetSizeAndAlignment_bug" */;
+                       buildPhases = (
+                               8DD76F990486AA7600D96B5E /* Sources */,
+                               8DD76F9B0486AA7600D96B5E /* Frameworks */,
+                               8DD76F9E0486AA7600D96B5E /* CopyFiles */,
+                       );
+                       buildRules = (
+                       );
+                       dependencies = (
+                       );
+                       name = NSGetSizeAndAlignment_bug;
+                       productInstallPath = "$(HOME)/bin";
+                       productName = NSGetSizeAndAlignment_bug;
+                       productReference = 8DD76FA10486AA7600D96B5E /* NSGetSizeAndAlignment_bug */;
+                       productType = "com.apple.product-type.tool";
+               };
+/* End PBXNativeTarget section */
+
+/* Begin PBXProject section */
+               08FB7793FE84155DC02AAC07 /* Project object */ = {
+                       isa = PBXProject;
+                       buildConfigurationList = 1DEB927808733DD40010E9CD /* Build configuration list for PBXProject "NSGetSizeAndAlignment_bug" */;
+                       compatibilityVersion = "Xcode 3.0";
+                       hasScannedForEncodings = 1;
+                       mainGroup = 08FB7794FE84155DC02AAC07 /* NSGetSizeAndAlignment_bug */;
+                       projectDirPath = "";
+                       projectRoot = "";
+                       targets = (
+                               8DD76F960486AA7600D96B5E /* NSGetSizeAndAlignment_bug */,
+                       );
+               };
+/* End PBXProject section */
+
+/* Begin PBXSourcesBuildPhase section */
+               8DD76F990486AA7600D96B5E /* Sources */ = {
+                       isa = PBXSourcesBuildPhase;
+                       buildActionMask = 2147483647;
+                       files = (
+                               8DD76F9A0486AA7600D96B5E /* NSGetSizeAndAlignment_bug.m in Sources */,
+                       );
+                       runOnlyForDeploymentPostprocessing = 0;
+               };
+/* End PBXSourcesBuildPhase section */
+
+/* Begin XCBuildConfiguration section */
+               1DEB927508733DD40010E9CD /* Debug */ = {
+                       isa = XCBuildConfiguration;
+                       buildSettings = {
+                               COPY_PHASE_STRIP = NO;
+                               GCC_DYNAMIC_NO_PIC = NO;
+                               GCC_ENABLE_FIX_AND_CONTINUE = YES;
+                               GCC_MODEL_TUNING = G5;
+                               GCC_OPTIMIZATION_LEVEL = 0;
+                               GCC_PRECOMPILE_PREFIX_HEADER = YES;
+                               GCC_PREFIX_HEADER = NSGetSizeAndAlignment_bug_Prefix.pch;
+                               INSTALL_PATH = /usr/local/bin;
+                               PRODUCT_NAME = NSGetSizeAndAlignment_bug;
+                               ZERO_LINK = YES;
+                       };
+                       name = Debug;
+               };
+               1DEB927608733DD40010E9CD /* Release */ = {
+                       isa = XCBuildConfiguration;
+                       buildSettings = {
+                               ARCHS = (
+                                       ppc,
+                                       i386,
+                               );
+                               DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
+                               GCC_MODEL_TUNING = G5;
+                               GCC_PRECOMPILE_PREFIX_HEADER = YES;
+                               GCC_PREFIX_HEADER = NSGetSizeAndAlignment_bug_Prefix.pch;
+                               INSTALL_PATH = /usr/local/bin;
+                               PRODUCT_NAME = NSGetSizeAndAlignment_bug;
+                       };
+                       name = Release;
+               };
+               1DEB927908733DD40010E9CD /* Debug */ = {
+                       isa = XCBuildConfiguration;
+                       buildSettings = {
+                               GCC_WARN_ABOUT_RETURN_TYPE = YES;
+                               GCC_WARN_UNUSED_VARIABLE = YES;
+                               PREBINDING = NO;
+                               SDKROOTS = "$(DEVELOPER_SDK_DIR)/MacOSX10.5.sdk";
+                       };
+                       name = Debug;
+               };
+               1DEB927A08733DD40010E9CD /* Release */ = {
+                       isa = XCBuildConfiguration;
+                       buildSettings = {
+                               GCC_WARN_ABOUT_RETURN_TYPE = YES;
+                               GCC_WARN_UNUSED_VARIABLE = YES;
+                               PREBINDING = NO;
+                               SDKROOTS = "$(DEVELOPER_SDK_DIR)/MacOSX10.5.sdk";
+                       };
+                       name = Release;
+               };
+/* End XCBuildConfiguration section */
+
+/* Begin XCConfigurationList section */
+               1DEB927408733DD40010E9CD /* Build configuration list for PBXNativeTarget "NSGetSizeAndAlignment_bug" */ = {
+                       isa = XCConfigurationList;
+                       buildConfigurations = (
+                               1DEB927508733DD40010E9CD /* Debug */,
+                               1DEB927608733DD40010E9CD /* Release */,
+                       );
+                       defaultConfigurationIsVisible = 0;
+                       defaultConfigurationName = Release;
+               };
+               1DEB927808733DD40010E9CD /* Build configuration list for PBXProject "NSGetSizeAndAlignment_bug" */ = {
+                       isa = XCConfigurationList;
+                       buildConfigurations = (
+                               1DEB927908733DD40010E9CD /* Debug */,
+                               1DEB927A08733DD40010E9CD /* Release */,
+                       );
+                       defaultConfigurationIsVisible = 0;
+                       defaultConfigurationName = Release;
+               };
+/* End XCConfigurationList section */
+       };
+       rootObject = 08FB7793FE84155DC02AAC07 /* Project object */;
+}
diff --git a/other/NSGetSizeAndAlignment_bug/NSGetSizeAndAlignment_bug_Prefix.pch b/other/NSGetSizeAndAlignment_bug/NSGetSizeAndAlignment_bug_Prefix.pch
new file mode 100644 (file)
index 0000000..62460ec
--- /dev/null
@@ -0,0 +1,7 @@
+//
+// Prefix header for all source files of the 'NSGetSizeAndAlignment_bug' target in the 'NSGetSizeAndAlignment_bug' project.
+//
+
+#ifdef __OBJC__
+    #import <Foundation/Foundation.h>
+#endif