Skip to contentSkip to conversation

ui/gtk: Negative Page number is not valid

PATCH In review [email protected] 2 participants 3 messages Jun 26, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ui/gtk: Negative Page number is not valid

From: Dongwon Kim <[email protected]>

Negative page number means the page with that number does not belong to the notebook so it shouldn't be used as a valid page number in gd_vc_find_by_page. This function should just return null in such case.

This change, however, will cause a segfault during detaching /untabifying process in gtk_release_modifiers because the current VC's page number suddenly becomes '-1' as soon as the VC is detached, which makes gd_vc_find_by_page return null. So gtk_release_modifiers should do the null check on VC returned from gd_vc_find_by_page.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
---
 ui/gtk.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ui/gtk.c +2 -2
index 93b13b7a30..1f8523fd81 100644
@@ -164,7 +164,7 @@ static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
     for (i = 0; i < s->nb_vcs; i++) {
         vc = &s->vc[i];
         p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
-        if (p == page) {
+        if (p > -1 && p == page) {
             return vc;
         }
     }
@@ -357,7 +357,7 @@ static void gtk_release_modifiers(GtkDisplayState *s)
 {
     VirtualConsole *vc = gd_vc_find_current(s);
 
-    if (vc->type != GD_VC_GFX ||
+    if (!vc || vc->type != GD_VC_GFX ||
         !qemu_console_is_graphic(vc->gfx.dcl.con)) {
         return;
     }
Signature · 1 line
2.34.1
#1 lore raw

Hi Dongwon,

ui/gtk.c static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)

Quoted 27 lines · On 26/6/24 02:08, [email protected] wrote:

From: Dongwon Kim <[email protected]>

Negative page number means the page with that number does not belong to the notebook so it shouldn't be used as a valid page number in gd_vc_find_by_page. This function should just return null in such case.

This change, however, will cause a segfault during detaching /untabifying process in gtk_release_modifiers because the current VC's page number suddenly becomes '-1' as soon as the VC is detached, which makes gd_vc_find_by_page return null. So gtk_release_modifiers should do the null check on VC returned from gd_vc_find_by_page.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
---
  ui/gtk.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
ui/gtk.c +0 -0
index 93b13b7a30..1f8523fd81 100644
@@ -164,7 +164,7 @@ static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)

The caller should check gtk_notebook_get_current_page() != -1.

We might assert(page >= 0) here.

ui/gtk.c static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page) · line 167

Quoted 5 lines
ui/gtk.c +1 -1
      for (i = 0; i < s->nb_vcs; i++) {
          vc = &s->vc[i];
          p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
-        if (p == page) {
+        if (p > -1 && p == page) {

Then this is not necessary.

ui/gtk.c static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page) · line 170

Quoted 3 lines
ui/gtk.c +0 -0
              return vc;
          }
      }
        return NULL;

I wonder about returning NULL, maybe just

       g_assert_not_reached();

ui/gtk.c static void gtk_release_modifiers(GtkDisplayState *s) · line 360

Quoted 6 lines
ui/gtk.c +1 -1
@@ -357,7 +357,7 @@ static void gtk_release_modifiers(GtkDisplayState *s)
  {
      VirtualConsole *vc = gd_vc_find_current(s);
 
-    if (vc->type != GD_VC_GFX ||
+    if (!vc || vc->type != GD_VC_GFX ||

Then this is not necessary.

Quoted 3 lines
ui/gtk.c +0 -0
          !qemu_console_is_graphic(vc->gfx.dcl.con)) {
          return;
      }
#2 lore raw
Quoted 34 lines · Philippe Mathieu-Daudé

Hi Dongwon,

Quoted 27 lines · On 26/6/24 02:08, [email protected] wrote:

From: Dongwon Kim <[email protected]>

Negative page number means the page with that number does not belong to the notebook so it shouldn't be used as a valid page number in gd_vc_find_by_page. This function should just return null in such case.

This change, however, will cause a segfault during detaching /untabifying process in gtk_release_modifiers because the current VC's page number suddenly becomes '-1' as soon as the VC is detached, which makes gd_vc_find_by_page return null. So gtk_release_modifiers should do the null check on VC returned from gd_vc_find_by_page.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>

---
  ui/gtk.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
ui/gtk.c +0 -0
index 93b13b7a30..1f8523fd81 100644

ui/gtk.c static VirtualConsole · line 164

Quoted 1 line
ui/gtk.c +0 -0
@@ -164,7 +164,7 @@ static VirtualConsole 
 *gd_vc_find_by_page(GtkDisplayState *s, gint page)

The caller should check gtk_notebook_get_current_page() != -1.

Quoted 1 line

We might assert(page >= 0) here.

We could do that but it means there should be more checks in other functions where gd_vc_find_by_page is called, like gd_vc_find_current. And we just can't do assert in\
gd_vc_find_current because detached VC has the page number of -1.

Quoted 30 lines

ui/gtk.c static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page) · line 167

Quoted 6 lines
ui/gtk.c +1 -1
       for (i = 0; i < s->nb_vcs; i++) {
           vc = &s->vc[i];
           p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
-        if (p == page) {
+        if (p > -1 && p == page) {

Then this is not necessary.

Quoted 3 lines
ui/gtk.c +0 -0
               return vc;
           }

      }

        return NULL;

I wonder about returning NULL, maybe just

       g_assert_not_reached();

ui/gtk.c static void gtk_release_modifiers(GtkDisplayState *s) · line 359

Quoted 5 lines
ui/gtk.c +1 -1
@@ -357,7 +357,7 @@ static void gtk_release_modifiers(GtkDisplayState *s)
   {
       VirtualConsole *vc = gd_vc_find_current(s);
-    if (vc->type != GD_VC_GFX ||
+    if (!vc || vc->type != GD_VC_GFX ||

Then this is not necessary.

Quoted 3 lines
ui/gtk.c +0 -0
           !qemu_console_is_graphic(vc->gfx.dcl.con)) {
           return;
       }