Skip to content

Commit 4bf8204

Browse files
NSProgrammermeta-codesync[bot]
authored andcommitted
Address -Wswitch-default issues (#54580)
Summary: Pull Request resolved: #54580 Changelog: [iOS] [Fixed] - Fix undefined behavior for values outside of enum range in generated `switch` methods in Objective-C. Differential Revision: D87282978 fbshipit-source-id: 12b74e0525d30fcc0ff4f9d8e84c930d923cce5a
1 parent 5a3759e commit 4bf8204

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

packages/react-native-codegen/e2e/deep_imports/__tests__/components/__snapshots__/GeneratePropsH-test.js.snap

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ static inline std::string toString(const EnumPropNativeComponentViewAlignment &v
421421
case EnumPropNativeComponentViewAlignment::Top: return \\"top\\";
422422
case EnumPropNativeComponentViewAlignment::Center: return \\"center\\";
423423
case EnumPropNativeComponentViewAlignment::BottomRight: return \\"bottom-right\\";
424+
default:
425+
abort();
424426
}
425427
}
426428
@@ -447,8 +449,9 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
447449
case 60:
448450
result = EnumPropNativeComponentViewIntervals::Intervals60;
449451
return;
452+
default:
453+
abort();
450454
}
451-
abort();
452455
}
453456
454457
static inline std::string toString(const EnumPropNativeComponentViewIntervals &value) {
@@ -457,6 +460,8 @@ static inline std::string toString(const EnumPropNativeComponentViewIntervals &v
457460
case EnumPropNativeComponentViewIntervals::Intervals15: return \\"15\\";
458461
case EnumPropNativeComponentViewIntervals::Intervals30: return \\"30\\";
459462
case EnumPropNativeComponentViewIntervals::Intervals60: return \\"60\\";
463+
default:
464+
abort();
460465
}
461466
}
462467
@@ -467,6 +472,8 @@ static inline folly::dynamic toDynamic(const EnumPropNativeComponentViewInterval
467472
case EnumPropNativeComponentViewIntervals::Intervals15: return 15;
468473
case EnumPropNativeComponentViewIntervals::Intervals30: return 30;
469474
case EnumPropNativeComponentViewIntervals::Intervals60: return 60;
475+
default:
476+
abort();
470477
}
471478
}
472479
#endif
@@ -926,6 +933,8 @@ static inline std::string toString(const ObjectPropsNativeComponentStringEnumPro
926933
switch (value) {
927934
case ObjectPropsNativeComponentStringEnumProp::Small: return \\"small\\";
928935
case ObjectPropsNativeComponentStringEnumProp::Large: return \\"large\\";
936+
default:
937+
abort();
929938
}
930939
}
931940
@@ -946,14 +955,17 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
946955
case 1:
947956
result = ObjectPropsNativeComponentIntEnumProp::IntEnumProp1;
948957
return;
958+
default:
959+
abort();
949960
}
950-
abort();
951961
}
952962
953963
static inline std::string toString(const ObjectPropsNativeComponentIntEnumProp &value) {
954964
switch (value) {
955965
case ObjectPropsNativeComponentIntEnumProp::IntEnumProp0: return \\"0\\";
956966
case ObjectPropsNativeComponentIntEnumProp::IntEnumProp1: return \\"1\\";
967+
default:
968+
abort();
957969
}
958970
}
959971
@@ -962,6 +974,8 @@ static inline folly::dynamic toDynamic(const ObjectPropsNativeComponentIntEnumPr
962974
switch (value) {
963975
case ObjectPropsNativeComponentIntEnumProp::IntEnumProp0: return 0;
964976
case ObjectPropsNativeComponentIntEnumProp::IntEnumProp1: return 1;
977+
default:
978+
abort();
965979
}
966980
}
967981
#endif

packages/react-native-codegen/e2e/namespaced/__tests__/components/__snapshots__/GeneratePropsH-test.js.snap

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ static inline std::string toString(const EnumPropNativeComponentViewAlignment &v
421421
case EnumPropNativeComponentViewAlignment::Top: return \\"top\\";
422422
case EnumPropNativeComponentViewAlignment::Center: return \\"center\\";
423423
case EnumPropNativeComponentViewAlignment::BottomRight: return \\"bottom-right\\";
424+
default:
425+
abort();
424426
}
425427
}
426428
@@ -447,8 +449,9 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
447449
case 60:
448450
result = EnumPropNativeComponentViewIntervals::Intervals60;
449451
return;
452+
default:
453+
abort();
450454
}
451-
abort();
452455
}
453456
454457
static inline std::string toString(const EnumPropNativeComponentViewIntervals &value) {
@@ -457,6 +460,8 @@ static inline std::string toString(const EnumPropNativeComponentViewIntervals &v
457460
case EnumPropNativeComponentViewIntervals::Intervals15: return \\"15\\";
458461
case EnumPropNativeComponentViewIntervals::Intervals30: return \\"30\\";
459462
case EnumPropNativeComponentViewIntervals::Intervals60: return \\"60\\";
463+
default:
464+
abort();
460465
}
461466
}
462467
@@ -467,6 +472,8 @@ static inline folly::dynamic toDynamic(const EnumPropNativeComponentViewInterval
467472
case EnumPropNativeComponentViewIntervals::Intervals15: return 15;
468473
case EnumPropNativeComponentViewIntervals::Intervals30: return 30;
469474
case EnumPropNativeComponentViewIntervals::Intervals60: return 60;
475+
default:
476+
abort();
470477
}
471478
}
472479
#endif
@@ -926,6 +933,8 @@ static inline std::string toString(const ObjectPropsNativeComponentStringEnumPro
926933
switch (value) {
927934
case ObjectPropsNativeComponentStringEnumProp::Small: return \\"small\\";
928935
case ObjectPropsNativeComponentStringEnumProp::Large: return \\"large\\";
936+
default:
937+
abort();
929938
}
930939
}
931940
@@ -946,14 +955,17 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
946955
case 1:
947956
result = ObjectPropsNativeComponentIntEnumProp::IntEnumProp1;
948957
return;
958+
default:
959+
abort();
949960
}
950-
abort();
951961
}
952962
953963
static inline std::string toString(const ObjectPropsNativeComponentIntEnumProp &value) {
954964
switch (value) {
955965
case ObjectPropsNativeComponentIntEnumProp::IntEnumProp0: return \\"0\\";
956966
case ObjectPropsNativeComponentIntEnumProp::IntEnumProp1: return \\"1\\";
967+
default:
968+
abort();
957969
}
958970
}
959971
@@ -962,6 +974,8 @@ static inline folly::dynamic toDynamic(const ObjectPropsNativeComponentIntEnumPr
962974
switch (value) {
963975
case ObjectPropsNativeComponentIntEnumProp::IntEnumProp0: return 0;
964976
case ObjectPropsNativeComponentIntEnumProp::IntEnumProp1: return 1;
977+
default:
978+
abort();
965979
}
966980
}
967981
#endif

packages/react-native-codegen/src/generators/components/GeneratePropsH.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
124124
static inline std::string toString(const ${enumName} &value) {
125125
switch (value) {
126126
${toCases}
127+
default:
128+
abort();
127129
}
128130
}
129131
@@ -154,20 +156,25 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
154156
assert(value.hasType<int>());
155157
auto integerValue = (int)value;
156158
switch (integerValue) {${fromCases}
159+
default:
160+
abort();
157161
}
158-
abort();
159162
}
160163
161164
static inline std::string toString(const ${enumName} &value) {
162165
switch (value) {
163166
${toCases}
167+
default:
168+
abort();
164169
}
165170
}
166171
167172
#ifdef RN_SERIALIZABLE_STATE
168173
static inline folly::dynamic toDynamic(const ${enumName} &value) {
169174
switch (value) {
170175
${toDynamicCases}
176+
default:
177+
abort();
171178
}
172179
}
173180
#endif

packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsH-test.js.snap

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,15 +1100,18 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
11001100
case 2:
11011101
result = Int32EnumPropsNativeComponentMaxInterval::MaxInterval2;
11021102
return;
1103+
default:
1104+
abort();
11031105
}
1104-
abort();
11051106
}
11061107
11071108
static inline std::string toString(const Int32EnumPropsNativeComponentMaxInterval &value) {
11081109
switch (value) {
11091110
case Int32EnumPropsNativeComponentMaxInterval::MaxInterval0: return \\"0\\";
11101111
case Int32EnumPropsNativeComponentMaxInterval::MaxInterval1: return \\"1\\";
11111112
case Int32EnumPropsNativeComponentMaxInterval::MaxInterval2: return \\"2\\";
1113+
default:
1114+
abort();
11121115
}
11131116
}
11141117
@@ -1118,6 +1121,8 @@ static inline folly::dynamic toDynamic(const Int32EnumPropsNativeComponentMaxInt
11181121
case Int32EnumPropsNativeComponentMaxInterval::MaxInterval0: return 0;
11191122
case Int32EnumPropsNativeComponentMaxInterval::MaxInterval1: return 1;
11201123
case Int32EnumPropsNativeComponentMaxInterval::MaxInterval2: return 2;
1124+
default:
1125+
abort();
11211126
}
11221127
}
11231128
#endif
@@ -1399,6 +1404,8 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
13991404
static inline std::string toString(const ObjectPropsStringEnumProp &value) {
14001405
switch (value) {
14011406
case ObjectPropsStringEnumProp::Option1: return \\"option1\\";
1407+
default:
1408+
abort();
14021409
}
14031410
}
14041411
@@ -1416,20 +1423,25 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
14161423
case 0:
14171424
result = ObjectPropsIntEnumProp::IntEnumProp0;
14181425
return;
1426+
default:
1427+
abort();
14191428
}
1420-
abort();
14211429
}
14221430
14231431
static inline std::string toString(const ObjectPropsIntEnumProp &value) {
14241432
switch (value) {
14251433
case ObjectPropsIntEnumProp::IntEnumProp0: return \\"0\\";
1434+
default:
1435+
abort();
14261436
}
14271437
}
14281438
14291439
#ifdef RN_SERIALIZABLE_STATE
14301440
static inline folly::dynamic toDynamic(const ObjectPropsIntEnumProp &value) {
14311441
switch (value) {
14321442
case ObjectPropsIntEnumProp::IntEnumProp0: return 0;
1443+
default:
1444+
abort();
14331445
}
14341446
}
14351447
#endif
@@ -1870,6 +1882,8 @@ static inline std::string toString(const StringEnumPropsNativeComponentAlignment
18701882
case StringEnumPropsNativeComponentAlignment::Top: return \\"top\\";
18711883
case StringEnumPropsNativeComponentAlignment::Center: return \\"center\\";
18721884
case StringEnumPropsNativeComponentAlignment::BottomRight: return \\"bottom-right\\";
1885+
default:
1886+
abort();
18731887
}
18741888
}
18751889

0 commit comments

Comments
 (0)