Fix for null pointer bug found by UBSAN (#53686)
authormonojenkins <jo.shields+jenkins@xamarin.com>
Thu, 3 Jun 2021 20:27:39 +0000 (16:27 -0400)
committerGitHub <noreply@github.com>
Thu, 3 Jun 2021 20:27:39 +0000 (16:27 -0400)
Fix for null-pointer bug found with Clang's undefined-behavior-sanitizer using `-fsanitize=null`

https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html

Co-authored-by: jbcoe <jbcoe@users.noreply.github.com>
src/mono/mono/mini/method-to-ir.c

index 104be3d4e583cf551d92734362ed823166b3c7dd..86a4add50608f3ae0f980a63eedd686d519fb42c 100644 (file)
@@ -7211,7 +7211,9 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (!dont_verify && !cfg->skip_visibility) {
                                MonoMethod *target_method = cil_method;
                                if (method->is_inflated) {
-                                       target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context), cfg->error);
+                                       MonoGenericContainer *container = mono_method_get_generic_container(method_definition);
+                                       MonoGenericContext *context = (container != NULL ? &container->context : NULL);
+                                       target_method = mini_get_method_allow_open (method, token, NULL, context, cfg->error);
                                        CHECK_CFG_ERROR;
                                }
                                if (!mono_method_can_access_method (method_definition, target_method) &&
@@ -8675,7 +8677,9 @@ calli_end:
                                MonoMethod *target_method = cil_method;
 
                                if (method->is_inflated) {
-                                       target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context), cfg->error);
+                                       MonoGenericContainer *container = mono_method_get_generic_container(method_definition);
+                                       MonoGenericContext *context = (container != NULL ? &container->context : NULL);
+                                       target_method = mini_get_method_allow_open (method, token, NULL, context, cfg->error);
                                        CHECK_CFG_ERROR;
                                }