[PATCH] Fix segfault in XFree86 mode switching

The following patch fixes a segfault in SDL_x11modes.c when changing
video modes on a display with only one configured modeline.

It was possible for `i’ to be negative after falling through to the
’match:’ block – this patch adds a new variable to keep track of the
best known mode instead of reusing the loop index.

#:jamesjb

— SDL-1.2.6/src/video/x11/SDL_x11modes.c 2003-08-30 12:13:12.000000000 -0700
+++ SDL-1.2.6-jamesjb/src/video/x11/SDL_x11modes.c 2003-08-31 14:20:32.000000000 -0700
@@ -100,6 +100,7 @@
SDL_NAME(XF86VidModeModeLine) mode;
SDL_NAME(XF86VidModeModeInfo) **modes;
int i;

  •    int best_mode_index = -1;
       int best_width = 0, best_height = 0;
       int nmodes;
    

@@ -116,8 +117,10 @@
#endif
for ( i = 0; i < nmodes ; i++ ) {
if ( (modes[i]->hdisplay == width) &&

  •                 (modes[i]->vdisplay == height) )
    
  •                 (modes[i]->vdisplay == height) ) {
    
  •                best_mode_index = i;
                   goto match;
    
  •   }
           }
           qsort(modes, nmodes, sizeof *modes, cmpmodes);
           for ( i = nmodes-1; i >= 0 ; i-- ) {
    

@@ -126,20 +129,25 @@
(modes[i]->vdisplay >= height) ) {
best_width = modes[i]->hdisplay;
best_height = modes[i]->vdisplay;

  •                    best_mode_index = i;
                   }
               } else {
                   if ( (modes[i]->hdisplay != best_width) ||
                        (modes[i]->vdisplay != best_height) ) {
    
  •                    i++;
    
  •                    break;
    
  •   	best_mode_index = i;
    
  •   	break;
                   }
               }
           }
      match:
    
  •        if ( (modes[i]->hdisplay != mode.hdisplay) ||
    
  •             (modes[i]->vdisplay != mode.vdisplay) ) {
    
  •            SDL_NAME(XF86VidModeSwitchToMode)(SDL_Display, SDL_Screen, modes[i]);
    
  •        }
    
  •   if (best_mode_index >= 0) {
    
  •   if ( (modes[best_mode_index]->hdisplay != mode.hdisplay) ||
    
  •        (modes[best_mode_index]->vdisplay != mode.vdisplay) ) {
    
  •       SDL_NAME(XF86VidModeSwitchToMode)(SDL_Display, SDL_Screen, 
    
  •   				      modes[best_mode_index]);
    
  •   }
    
  •   }+
           XFree(modes);
       }
    
    }

Or, you could just fix it as follows (back to the original loop before
I’d accidently changed it - sorry);

— SDL-1.2.6/src/video/x11/SDL_x11modes.c 2003-08-30
20:13:12.000000000 +0100
+++ SDL-1.2.6-fix/src/video/x11/SDL_x11modes.c 2003-09-02
01:40:57.000000000 +0100
@@ -120,7 +120,7 @@
goto match;
}
qsort(modes, nmodes, sizeof *modes, cmpmodes);

  •        for ( i = nmodes-1; i >= 0 ; i-- ) {
    
  •        for ( i = nmodes-1; i > 0 ; i-- ) {
              if ( ! best_width ) {
                   if ( (modes[i]->hdisplay >= width) &&
                        (modes[i]->vdisplay >= height) ) {On Sun, 2003-08-31 at 22:47, James Bielman wrote:
    

The following patch fixes a segfault in SDL_x11modes.c when changing
video modes on a display with only one configured modeline.

It was possible for `i’ to be negative after falling through to the
’match:’ block – this patch adds a new variable to keep track of the
best known mode instead of reusing the loop index.


Alan.

“One must never be purposelessnessnesslessness.”
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20030901/fcc7d8cc/attachment.pgp

Or, you could just fix it as follows (back to the original loop before
I’d accidently changed it - sorry);

Done!
-Sam Lantinga, Software Engineer, Blizzard Entertainment

I wouldn’t have written the thanks in the CVS. More a “What a dozy
eejit” for making the mistake in the first place :-/

Must buy laptop or PDA for testing.On Fri, 2003-09-05 at 16:21, Sam Lantinga wrote:

Or, you could just fix it as follows (back to the original loop before
I’d accidently changed it - sorry);


Alan.

“One must never be purposelessnessnesslessness.”
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20030907/84b286b1/attachment.pgp