Fixed SDL_ListModes() to not return pointers to things that got realloc'd.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 02 Apr 2019 15:05:00 -0400
changeset 1198d4f708adba1
parent 118 80c6b5470095
child 120 dc55edfe5d2f
Fixed SDL_ListModes() to not return pointers to things that got realloc'd.
src/SDL12_compat.c
     1.1 --- a/src/SDL12_compat.c	Tue Apr 02 14:15:37 2019 -0400
     1.2 +++ b/src/SDL12_compat.c	Tue Apr 02 15:05:00 2019 -0400
     1.3 @@ -705,6 +705,7 @@
     1.4  typedef struct
     1.5  {
     1.6      Uint32 format;
     1.7 +    int nummodes;
     1.8      SDL12_Rect *modeslist12;
     1.9      SDL12_Rect **modes12;  /* ptrs to each item in modeslist, for SDL_ListModes() */
    1.10  } VideoModeList;
    1.11 @@ -717,7 +718,7 @@
    1.12  
    1.13  // !!! FIXME: go through all of these.
    1.14  static VideoModeList *VideoModes = NULL;
    1.15 -static int VideoModesCount = 0;
    1.16 +static int VideoModesCount = 0;  // this counts items in VideoModeList, not total video modes.
    1.17  static SDL12_VideoInfo VideoInfo12;
    1.18  static SDL12_Palette VideoInfoPalette12;
    1.19  static SDL12_PixelFormat VideoInfoVfmt12;
    1.20 @@ -1071,7 +1072,6 @@
    1.21  {
    1.22      const int total = SDL20_GetNumDisplayModes(VideoDisplayIndex);
    1.23      VideoModeList *vmode = NULL;
    1.24 -    int num_modes = 0;
    1.25      void *ptr = NULL;
    1.26      int i, j;
    1.27  
    1.28 @@ -1092,9 +1092,6 @@
    1.29          }
    1.30  
    1.31          if (!vmode || (mode.format != vmode->format)) {  // SDL20_GetDisplayMode() sorts on bpp first. We know when to change arrays.
    1.32 -            if (VideoModesCount > 0) {
    1.33 -                VideoModes[VideoModesCount-1].modes12[num_modes] = NULL;
    1.34 -            }
    1.35              ptr = (VideoModeList *) SDL20_realloc(VideoModes, sizeof (VideoModeList) * (VideoModesCount+1));
    1.36              if (!ptr) {
    1.37                  return SDL20_OutOfMemory();
    1.38 @@ -1102,49 +1099,49 @@
    1.39              VideoModes = (VideoModeList *) ptr;
    1.40              vmode = &VideoModes[VideoModesCount];
    1.41              vmode->format = mode.format;
    1.42 +            vmode->nummodes = 0;
    1.43              vmode->modeslist12 = NULL;
    1.44              vmode->modes12 = NULL;
    1.45              VideoModesCount++;
    1.46 -            num_modes = 0;
    1.47          }
    1.48  
    1.49          /* make sure we don't have this one already (with a different refresh rate, etc). */
    1.50 -        for (j = 0; j < num_modes; j++) {
    1.51 +        for (j = 0; j < vmode->nummodes; j++) {
    1.52              if ((vmode->modeslist12[j].w == mode.w) && (vmode->modeslist12[j].h == mode.h)) {
    1.53                  break;
    1.54              }
    1.55          }
    1.56  
    1.57 -        if (j < num_modes) {
    1.58 +        if (j < vmode->nummodes) {
    1.59              continue;  /* already have this one. */
    1.60          }
    1.61  
    1.62          FIXME("Make sure mode dimensions fit in 16-bits for SDL12_Rect");
    1.63  
    1.64 -        ptr = SDL20_realloc(vmode->modes12, sizeof (SDL12_Rect *) * (num_modes + 2));
    1.65 -        if (ptr == NULL) {
    1.66 -            return SDL20_OutOfMemory();
    1.67 -        }
    1.68 -        vmode->modes12 = (SDL12_Rect **) ptr;
    1.69 -
    1.70 -        ptr = SDL20_realloc(vmode->modeslist12, sizeof (SDL12_Rect) * (num_modes + 1));
    1.71 +        ptr = SDL20_realloc(vmode->modeslist12, sizeof (SDL12_Rect) * (vmode->nummodes + 1));
    1.72          if (ptr == NULL) {
    1.73              return SDL20_OutOfMemory();
    1.74          }
    1.75          vmode->modeslist12 = (SDL12_Rect *) ptr;
    1.76  
    1.77 -        vmode->modeslist12[num_modes].x = 0;
    1.78 -        vmode->modeslist12[num_modes].y = 0;
    1.79 -        vmode->modeslist12[num_modes].w = mode.w;
    1.80 -        vmode->modeslist12[num_modes].h = mode.h;
    1.81 -
    1.82 -        vmode->modes12[num_modes] = &vmode->modeslist12[num_modes];
    1.83 -
    1.84 -        num_modes++;
    1.85 +        vmode->modeslist12[vmode->nummodes].x = 0;
    1.86 +        vmode->modeslist12[vmode->nummodes].y = 0;
    1.87 +        vmode->modeslist12[vmode->nummodes].w = mode.w;
    1.88 +        vmode->modeslist12[vmode->nummodes].h = mode.h;
    1.89 +
    1.90 +        vmode->nummodes++;
    1.91      }
    1.92  
    1.93 -    if (VideoModesCount > 0) {
    1.94 -        VideoModes[VideoModesCount-1].modes12[num_modes] = NULL;
    1.95 +    // link up modes12 for SDL_ListModes()'s use...
    1.96 +    for (i = 0, vmode = VideoModes; i < VideoModesCount; i++, vmode++) {
    1.97 +        const int nummodes = vmode->nummodes;
    1.98 +        vmode->modes12 = (SDL12_Rect **) SDL20_calloc(sizeof (SDL12_Rect *), nummodes + 1);
    1.99 +        if (vmode->modes12 == NULL) {
   1.100 +            return SDL20_OutOfMemory();
   1.101 +        }
   1.102 +        for (j = 0; j < nummodes; j++) {
   1.103 +            vmode->modes12[j] = &vmode->modeslist12[j];
   1.104 +        }
   1.105      }
   1.106  
   1.107      return 0;