@@ -31,6 +31,11 @@ int FuzzTestFieldPath(const uint8_t *data, size_t size) {
31
31
// Convert the raw bytes to a string with UTF-8 format.
32
32
NSData *d = [NSData dataWithBytes: data length: size];
33
33
NSString *str = [[NSString alloc ] initWithData: d encoding: NSUTF8StringEncoding];
34
+ if (!str) {
35
+ // TODO(varconst): this happens when `NSData` doesn't happen to contain valid UTF-8, perhaps
36
+ // find a way to still convert it to a string.
37
+ return 0 ;
38
+ }
34
39
35
40
// Create a FieldPath object from a string.
36
41
@try {
@@ -42,7 +47,7 @@ int FuzzTestFieldPath(const uint8_t *data, size_t size) {
42
47
// Fuzz test creating a FieldPath from an array with a single string.
43
48
NSArray *str_arr1 = [NSArray arrayWithObjects: str, nil ];
44
49
@try {
45
- [[FIRFieldPath alloc ] initWithFields: str_arr1];
50
+ ( void ) [[FIRFieldPath alloc ] initWithFields: str_arr1];
46
51
} @catch (...) {
47
52
// Caught exceptions are ignored because they are not what we are after in
48
53
// fuzz testing.
@@ -52,7 +57,7 @@ int FuzzTestFieldPath(const uint8_t *data, size_t size) {
52
57
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString: @" .,/_" ];
53
58
NSArray *str_arr2 = [str componentsSeparatedByCharactersInSet: set];
54
59
@try {
55
- [[FIRFieldPath alloc ] initWithFields: str_arr2];
60
+ ( void ) [[FIRFieldPath alloc ] initWithFields: str_arr2];
56
61
} @catch (...) {
57
62
// Ignore caught exceptions.
58
63
}
@@ -62,9 +67,25 @@ int FuzzTestFieldPath(const uint8_t *data, size_t size) {
62
67
// created as mutable objects. Returns nil if there is a parsing error.
63
68
NSArray *str_arr3 =
64
69
[NSJSONSerialization JSONObjectWithData: d options: NSJSONReadingMutableContainers error: nil ];
70
+ NSMutableArray *mutable_array = [[NSMutableArray alloc ] initWithArray: str_arr3];
71
+ if (str_arr3) {
72
+ for (int i = 0 ; i < str_arr3.count ; ++i) {
73
+ NSObject *value = str_arr3[i];
74
+ // `FIRFieldPath initWithFields:` relies on all members having `length` attribute.
75
+ if (![value isKindOfClass: [NSString class ]]) {
76
+ if ([value isKindOfClass: [NSNumber class ]]) {
77
+ mutable_array[i] = [[NSString alloc ] initWithFormat: @" %@ " , (NSNumber *)value];
78
+ } else {
79
+ // TODO(varconst): convert to string recursively.
80
+ return 0 ;
81
+ }
82
+ }
83
+ }
84
+ }
85
+
65
86
@try {
66
- if (str_arr3 ) {
67
- [[FIRFieldPath alloc ] initWithFields: str_arr3 ];
87
+ if (mutable_array ) {
88
+ ( void ) [[FIRFieldPath alloc ] initWithFields: mutable_array ];
68
89
}
69
90
} @catch (...) {
70
91
// Ignore caught exceptions.
0 commit comments