Skip to content

Commit

Permalink
Improve handling of nullable values in iOS renderer
Browse files Browse the repository at this point in the history
This pull request improves the usage of nullable values that can be returned by various bits of the iOS renderer.

The initial place this appeared was [this rive-ios issue](#330), where in the provided stack trace, we can see the following:

```
...
  "exception" : {"codes":"0x0000000000000000, 0x0000000000000000","rawCodes":[0,0],"type":"EXC_CRASH","signal":"SIGABRT"},
  "termination" : {"flags":0,"code":6,"namespace":"SIGNAL","indicator":"Abort trap: 6","byProc":"Application","byPid":1045},
  "asi" : {"libsystem_c.dylib":["abort() called"]},
  "exceptionReason" : {"arguments":["-[__NSArrayM insertObject:atIndex:]"],"format_string":"*** %s: object cannot be nil","name":"NSInvalidArgumentException","type":"objc-exception","composed_message":"*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil","class":"NSException"},
  "extMods" : {"caller":{"thread_create":0,"thread_set_state":0,"task_for_pid":0},"system":{"thread_create":0,"thread_set_state":0,"task_for_pid":0},"targeted":{"thread_create":0,"thread_set_state":0,"task_for_pid":0},"warnings":0},
  "lastExceptionBacktrace" : [
{"imageOffset":978358,"symbol":"__exceptionPreprocess","symbolLocation":226,"imageIndex":9},
{"imageOffset":81565,"symbol":"objc_exception_throw","symbolLocation":48,"imageIndex":7},
{"imageOffset":123841,"symbol":"-[__NSArrayM insertObject:atIndex:]","symbolLocation":1762,"imageIndex":9},
{"imageOffset":35776,"symbol":"-[RiveStateMachineInstance stateChanges]","symbolLocation":178,"imageIndex":1},
...
```

Peeking into `-[RiveStateMachineInstance stateChanges]` showed that we were not checking if the nullable value was valid before attempting to insert into an array, thus under certain conditions inserting nil (those conditions still tbd; I've asked the dev for a hopefully reproducible file).

I went ahead and peeked at various other portions of the renderer for nullable usage and added some other checks for array insertion that were similar.

Diffs=
cd8fef48f Improve handling of nullable values in iOS renderer (#7529)

Co-authored-by: David Skuza <[email protected]>
  • Loading branch information
dskuza and dskuza committed Jul 5, 2024
1 parent 701a1df commit 76aef26
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a22d3ea8c780dbaed55b55bc340f25104b7aa134
cd8fef48ffd302962e63b8452e07b1cce6aa4af6
12 changes: 10 additions & 2 deletions Source/Renderer/RiveArtboard.mm
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ - (NSArray*)animationNames
NSMutableArray* animationNames = [NSMutableArray array];
for (NSUInteger i = 0; i < [self animationCount]; i++)
{
[animationNames addObject:[[self animationFromIndex:i error:nil] name]];
RiveLinearAnimationInstance* animation = [self animationFromIndex:i error:nil];
if (animation != nil)
{
[animationNames addObject:[animation name]];
}
}
return animationNames;
}
Expand Down Expand Up @@ -197,7 +201,11 @@ - (NSArray*)stateMachineNames
NSMutableArray* stateMachineNames = [NSMutableArray array];
for (NSUInteger i = 0; i < [self stateMachineCount]; i++)
{
[stateMachineNames addObject:[[self stateMachineFromIndex:i error:nil] name]];
RiveStateMachineInstance* stateMachine = [self stateMachineFromIndex:i error:nil];
if (stateMachine != nil)
{
[stateMachineNames addObject:[stateMachine name]];
}
}
return stateMachineNames;
}
Expand Down
6 changes: 5 additions & 1 deletion Source/Renderer/RiveFile.mm
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,11 @@ - (NSArray*)artboardNames

for (NSUInteger i = 0; i < [self artboardCount]; i++)
{
[artboardNames addObject:[[self artboardFromIndex:i error:nil] name]];
RiveArtboard* artboard = [self artboardFromIndex:i error:nil];
if (artboard != nil)
{
[artboardNames addObject:[artboard name]];
}
}
return artboardNames;
}
Expand Down
12 changes: 10 additions & 2 deletions Source/Renderer/RiveStateMachineInstance.mm
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,11 @@ - (NSArray*)inputNames

for (NSUInteger i = 0; i < [self inputCount]; i++)
{
[inputNames addObject:[[self inputFromIndex:i error:nil] name]];
RiveSMIInput* input = [self inputFromIndex:i error:nil];
if (input != nil)
{
[inputNames addObject:[input name]];
}
}
return inputNames;
}
Expand Down Expand Up @@ -339,7 +343,11 @@ - (NSArray*)stateChanges

for (NSUInteger i = 0; i < [self stateChangedCount]; i++)
{
[inputNames addObject:[[self stateChangedFromIndex:i error:nil] name]];
RiveLayerState* state = [self stateChangedFromIndex:i error:nil];
if (state != nil)
{
[inputNames addObject:[state name]];
}
}
return inputNames;
}
Expand Down

0 comments on commit 76aef26

Please sign in to comment.