Commit e31d9c6
authored
Context: dotnet/android#7285 (comment) (#1029)
`Java.Inteorp.JniEnvironment.Types.RegisterNatives()` is overloaded:
namespace Java.Interop {
partial class JniEnvironment {
partial class Types {
public static void RegisterNatives (JniObjectReference type, JniNativeMethodRegistration[] methods);
public static void RegisterNatives (JniObjectReference type, JniNativeMethodRegistration[] methods, int numMethods);
}
}
}
If the `int numMethods` overload is used, then:
1. it should be possible to pass an array which contains *more* than
`numMethods` elements, and
2. Passing such an array shouldn't throw an exception.
For example:
var methods = new JniNativeMethodRegistration [10];
JniEnvironment.Types.RegisterNatives (type, methods, 0);
Instead, when using a Debug configuration build of `Java.Interop.dll`,
a `NullReferenceException` would be thrown, because the `foreach`
loop would traverse *every element*, not just the first `numMethods`
elements, which could result in accessing
`methods[i].Marshaler.GetType()`, which could be `null`.
Fix the `DEBUG && NETCOREAPP` check so that only the first
`numMethods` elements are accessed, *not* all of them, and add a
`null` check around `JniNativeMethodRegistration.Marshaler` access.
This prevents the `NullReferenceException`.
Additionally, add some parameter validation and throw an
`ArgumentOutOfRangeException` if `numMethods` is negative or is
greater than `methods.Length`.1 parent d3ea180 commit e31d9c6
1 file changed
+8
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
200 | 200 | | |
201 | 201 | | |
202 | 202 | | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
203 | 208 | | |
204 | | - | |
205 | | - | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
206 | 212 | | |
207 | 213 | | |
208 | 214 | | |
| |||
0 commit comments