:: commit b5fa9795810762ed1731785c11e7516bec4686e7

mintsuki <mintsuki@protonmail.com> — 2022-10-05 13:01

parents: b259041a4d

term: Better ensure term is not accessed when NULL

diff --git a/common/drivers/vga_textmode.c b/common/drivers/vga_textmode.c
index 7a0df2b0..163e1eb9 100644
--- a/common/drivers/vga_textmode.c
+++ b/common/drivers/vga_textmode.c
@@ -253,6 +253,11 @@ static void text_deinit(struct term_context *_ctx, void (*_free)(void *, size_t)
 static struct textmode_context term_local_struct;
 
 void vga_textmode_init(bool managed) {
+    if (term != NULL) {
+        term->deinit(term, pmm_free);
+        term = NULL;
+    }
+
     if (quiet) {
         return;
     }
@@ -265,11 +270,6 @@ void vga_textmode_init(bool managed) {
         current_video_mode = 0x3;
     }
 
-    if (term != NULL) {
-        term->deinit(term, pmm_free);
-        term = NULL;
-    }
-
     struct textmode_context *ctx = &term_local_struct;
     term = &term_local_struct.term;
 
diff --git a/common/lib/gterm.c b/common/lib/gterm.c
index bceac153..c496ad98 100644
--- a/common/lib/gterm.c
+++ b/common/lib/gterm.c
@@ -538,6 +538,10 @@ static char *last_config = NULL;
 
 bool gterm_init(char *config, size_t width, size_t height) {
     if (quiet) {
+        if (term != NULL) {
+            term->deinit(term, pmm_free);
+            term = NULL;
+        }
         return false;
     }
 
diff --git a/common/protos/chainload.c b/common/protos/chainload.c
index 2221c458..24aa695b 100644
--- a/common/protos/chainload.c
+++ b/common/protos/chainload.c
@@ -216,8 +216,10 @@ noreturn void efi_chainload_file(char *config, struct file_handle *image) {
     pmm_free(_ptr, image->size);
     fclose(image);
 
-    term->deinit(term, pmm_free);
-    term = NULL;
+    if (term != NULL) {
+        term->deinit(term, pmm_free);
+        term = NULL;
+    }
 
     size_t req_width = 0, req_height = 0, req_bpp = 0;
 
diff --git a/common/protos/linux.c b/common/protos/linux.c
index 9160e9b4..c72cac6a 100644
--- a/common/protos/linux.c
+++ b/common/protos/linux.c
@@ -493,8 +493,10 @@ noreturn void linux_load(char *config, char *cmdline) {
     // Video
     ///////////////////////////////////////
 
-    term->deinit(term, pmm_free);
-    term = NULL;
+    if (term != NULL) {
+        term->deinit(term, pmm_free);
+        term = NULL;
+    }
 
     struct screen_info *screen_info = &boot_params->screen_info;
 
@@ -520,7 +522,7 @@ noreturn void linux_load(char *config, char *cmdline) {
 #endif
     if (!fb_init(&fbinfo, req_width, req_height, req_bpp)) {
 #if defined (UEFI)
-        panic(true, "linux: Unable to set video mode");
+        goto no_fb;
 #elif defined (BIOS)
 set_textmode:;
         vga_textmode_init(false);
@@ -560,6 +562,9 @@ set_textmode:;
         }
     }
 
+#if defined (UEFI)
+no_fb:;
+#endif
     struct edid_info_struct *edid_info = get_edid_info();
 
     if (edid_info != NULL) {
diff --git a/common/protos/multiboot1.c b/common/protos/multiboot1.c
index c49e4f6b..edc20930 100644
--- a/common/protos/multiboot1.c
+++ b/common/protos/multiboot1.c
@@ -297,8 +297,10 @@ noreturn void multiboot1_load(char *config, char *cmdline) {
     multiboot1_info->bootloader_name = (uint32_t)(size_t)lowmem_bootname - mb1_info_slide;
     multiboot1_info->flags |= (1 << 9);
 
-    term->deinit(term, pmm_free);
-    term = NULL;
+    if (term != NULL) {
+        term->deinit(term, pmm_free);
+        term = NULL;
+    }
 
     if (header.flags & (1 << 2)) {
         size_t req_width  = header.fb_width;
diff --git a/common/protos/multiboot2.c b/common/protos/multiboot2.c
index bd449845..99e2045f 100644
--- a/common/protos/multiboot2.c
+++ b/common/protos/multiboot2.c
@@ -500,8 +500,10 @@ noreturn void multiboot2_load(char *config, char* cmdline) {
         tag->common.type = MULTIBOOT_TAG_TYPE_FRAMEBUFFER;
         tag->common.size = sizeof(struct multiboot_tag_framebuffer);
 
-        term->deinit(term, pmm_free);
-        term = NULL;
+        if (term != NULL) {
+            term->deinit(term, pmm_free);
+            term = NULL;
+        }
 
         if (fbtag) {
             size_t req_width = fbtag->width;
tab: 248 wrap: offon