Fixed all known static analysis bugs, with checker-279 on macOS.
authorRyan C. Gordon <icculus@icculus.org>
Thu, 24 Nov 2016 21:41:09 -0500
changeset 10650b6ec7005ca15
parent 10649 ddd8049e9d39
child 10651 f0065b8e6541
Fixed all known static analysis bugs, with checker-279 on macOS.
src/libm/e_rem_pio2.c
src/libm/k_rem_pio2.c
src/render/SDL_render.c
src/render/SDL_yuv_sw.c
src/render/opengl/SDL_render_gl.c
src/video/SDL_RLEaccel.c
src/video/SDL_rect.c
src/video/SDL_video.c
     1.1 --- a/src/libm/e_rem_pio2.c	Thu Nov 24 21:13:23 2016 -0500
     1.2 +++ b/src/libm/e_rem_pio2.c	Thu Nov 24 21:41:09 2016 -0500
     1.3 @@ -24,6 +24,8 @@
     1.4  #include "math_libm.h"
     1.5  #include "math_private.h"
     1.6  
     1.7 +#include "SDL_assert.h"
     1.8 +
     1.9  libm_hidden_proto(fabs)
    1.10  
    1.11  /*
    1.12 @@ -189,7 +191,12 @@
    1.13      }
    1.14      tx[2] = z;
    1.15      nx = 3;
    1.16 -    while (tx[nx - 1] == zero)
    1.17 +
    1.18 +    /* If this assertion ever fires, here's the static analysis that warned about it:
    1.19 +        http://buildbot.libsdl.org/sdl-static-analysis/sdl-macosx-static-analysis/sdl-macosx-static-analysis-1101/report-8c6ccb.html#EndPath */
    1.20 +    SDL_assert((tx[0] != zero) || (tx[1] != zero) || (tx[2] != zero));
    1.21 +
    1.22 +    while (nx && tx[nx - 1] == zero)
    1.23          nx--;                   /* skip zero term */
    1.24      n = __kernel_rem_pio2(tx, y, e0, nx, 2, two_over_pi);
    1.25      if (hx < 0) {
     2.1 --- a/src/libm/k_rem_pio2.c	Thu Nov 24 21:13:23 2016 -0500
     2.2 +++ b/src/libm/k_rem_pio2.c	Thu Nov 24 21:41:09 2016 -0500
     2.3 @@ -204,8 +204,13 @@
     2.4  
     2.5      /* compute q[0],q[1],...q[jk] */
     2.6      for (i = 0; i <= jk; i++) {
     2.7 -        for (j = 0, fw = 0.0; j <= jx; j++)
     2.8 -            fw += x[j] * f[jx + i - j];
     2.9 +        for (j = 0, fw = 0.0; j <= jx; j++) {
    2.10 +            const int32_t idx = jx + i - j;
    2.11 +            SDL_assert(idx >= 0);
    2.12 +            SDL_assert(idx < 20);
    2.13 +            SDL_assert(idx <= m);
    2.14 +            fw += x[j] * f[idx];
    2.15 +        }
    2.16          q[i] = fw;
    2.17      }
    2.18  
     3.1 --- a/src/render/SDL_render.c	Thu Nov 24 21:13:23 2016 -0500
     3.2 +++ b/src/render/SDL_render.c	Thu Nov 24 21:41:09 2016 -0500
     3.3 @@ -734,8 +734,8 @@
     3.4  
     3.5      if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
     3.6          /* We can lock the texture and copy to it */
     3.7 -        void *native_pixels;
     3.8 -        int native_pitch;
     3.9 +        void *native_pixels = NULL;
    3.10 +        int native_pitch = 0;
    3.11  
    3.12          if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
    3.13              return -1;
    3.14 @@ -745,18 +745,18 @@
    3.15          SDL_UnlockTexture(native);
    3.16      } else {
    3.17          /* Use a temporary buffer for updating */
    3.18 -        void *temp_pixels;
    3.19 -        int temp_pitch;
    3.20 -
    3.21 -        temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
    3.22 -        temp_pixels = SDL_malloc(rect->h * temp_pitch);
    3.23 -        if (!temp_pixels) {
    3.24 -            return SDL_OutOfMemory();
    3.25 +        const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
    3.26 +        const size_t alloclen = rect->h * temp_pitch;
    3.27 +        if (alloclen > 0) {
    3.28 +            void *temp_pixels = SDL_malloc(alloclen);
    3.29 +            if (!temp_pixels) {
    3.30 +                return SDL_OutOfMemory();
    3.31 +            }
    3.32 +            SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
    3.33 +                                rect->w, rect->h, temp_pixels, temp_pitch);
    3.34 +            SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
    3.35 +            SDL_free(temp_pixels);
    3.36          }
    3.37 -        SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
    3.38 -                            rect->w, rect->h, temp_pixels, temp_pitch);
    3.39 -        SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
    3.40 -        SDL_free(temp_pixels);
    3.41      }
    3.42      return 0;
    3.43  }
    3.44 @@ -767,10 +767,14 @@
    3.45  {
    3.46      SDL_Texture *native = texture->native;
    3.47  
    3.48 +    if (!rect->w || !rect->h) {
    3.49 +        return 0;  /* nothing to do. */
    3.50 +    }
    3.51 +
    3.52      if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
    3.53          /* We can lock the texture and copy to it */
    3.54 -        void *native_pixels;
    3.55 -        int native_pitch;
    3.56 +        void *native_pixels = NULL;
    3.57 +        int native_pitch = 0;
    3.58  
    3.59          if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
    3.60              return -1;
    3.61 @@ -781,19 +785,19 @@
    3.62          SDL_UnlockTexture(native);
    3.63      } else {
    3.64          /* Use a temporary buffer for updating */
    3.65 -        void *temp_pixels;
    3.66 -        int temp_pitch;
    3.67 -
    3.68 -        temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
    3.69 -        temp_pixels = SDL_malloc(rect->h * temp_pitch);
    3.70 -        if (!temp_pixels) {
    3.71 -            return SDL_OutOfMemory();
    3.72 +        const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
    3.73 +        const size_t alloclen = rect->h * temp_pitch;
    3.74 +        if (alloclen > 0) {
    3.75 +            void *temp_pixels = SDL_malloc(alloclen);
    3.76 +            if (!temp_pixels) {
    3.77 +                return SDL_OutOfMemory();
    3.78 +            }
    3.79 +            SDL_ConvertPixels(rect->w, rect->h,
    3.80 +                              texture->format, pixels, pitch,
    3.81 +                              native->format, temp_pixels, temp_pitch);
    3.82 +            SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
    3.83 +            SDL_free(temp_pixels);
    3.84          }
    3.85 -        SDL_ConvertPixels(rect->w, rect->h,
    3.86 -                          texture->format, pixels, pitch,
    3.87 -                          native->format, temp_pixels, temp_pitch);
    3.88 -        SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
    3.89 -        SDL_free(temp_pixels);
    3.90      }
    3.91      return 0;
    3.92  }
    3.93 @@ -853,10 +857,14 @@
    3.94      full_rect.h = texture->h;
    3.95      rect = &full_rect;
    3.96  
    3.97 +    if (!rect->w || !rect->h) {
    3.98 +        return 0;  /* nothing to do. */
    3.99 +    }
   3.100 +
   3.101      if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
   3.102          /* We can lock the texture and copy to it */
   3.103 -        void *native_pixels;
   3.104 -        int native_pitch;
   3.105 +        void *native_pixels = NULL;
   3.106 +        int native_pitch = 0;
   3.107  
   3.108          if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
   3.109              return -1;
   3.110 @@ -866,18 +874,18 @@
   3.111          SDL_UnlockTexture(native);
   3.112      } else {
   3.113          /* Use a temporary buffer for updating */
   3.114 -        void *temp_pixels;
   3.115 -        int temp_pitch;
   3.116 -
   3.117 -        temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
   3.118 -        temp_pixels = SDL_malloc(rect->h * temp_pitch);
   3.119 -        if (!temp_pixels) {
   3.120 -            return SDL_OutOfMemory();
   3.121 +        const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
   3.122 +        const size_t alloclen = rect->h * temp_pitch;
   3.123 +        if (alloclen > 0) {
   3.124 +            void *temp_pixels = SDL_malloc(alloclen);
   3.125 +            if (!temp_pixels) {
   3.126 +                return SDL_OutOfMemory();
   3.127 +            }
   3.128 +            SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
   3.129 +                                rect->w, rect->h, temp_pixels, temp_pitch);
   3.130 +            SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
   3.131 +            SDL_free(temp_pixels);
   3.132          }
   3.133 -        SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
   3.134 -                            rect->w, rect->h, temp_pixels, temp_pitch);
   3.135 -        SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
   3.136 -        SDL_free(temp_pixels);
   3.137      }
   3.138      return 0;
   3.139  }
   3.140 @@ -924,6 +932,10 @@
   3.141          rect = &full_rect;
   3.142      }
   3.143  
   3.144 +    if (!rect->w || !rect->h) {
   3.145 +        return 0;  /* nothing to do. */
   3.146 +    }
   3.147 +
   3.148      if (texture->yuv) {
   3.149          return SDL_UpdateTextureYUVPlanar(texture, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
   3.150      } else {
   3.151 @@ -1882,29 +1894,12 @@
   3.152      renderer->RenderPresent(renderer);
   3.153  }
   3.154  
   3.155 -void
   3.156 -SDL_DestroyTexture(SDL_Texture * texture)
   3.157 +/* this isn't responsible for removing the deleted texture from the list!
   3.158 +   (this is to keep static analysis happy in SDL_DestroyRenderer().) */
   3.159 +static void
   3.160 +SDL_DestroyTextureInternal(SDL_Texture * texture)
   3.161  {
   3.162 -    SDL_Renderer *renderer;
   3.163 -
   3.164 -    CHECK_TEXTURE_MAGIC(texture, );
   3.165 -
   3.166 -    renderer = texture->renderer;
   3.167 -    if (texture == renderer->target) {
   3.168 -        SDL_SetRenderTarget(renderer, NULL);
   3.169 -    }
   3.170 -
   3.171 -    texture->magic = NULL;
   3.172 -
   3.173 -    if (texture->next) {
   3.174 -        texture->next->prev = texture->prev;
   3.175 -    }
   3.176 -    if (texture->prev) {
   3.177 -        texture->prev->next = texture->next;
   3.178 -    } else {
   3.179 -        renderer->textures = texture->next;
   3.180 -    }
   3.181 -
   3.182 +    SDL_Renderer *renderer = texture->renderer;
   3.183      if (texture->native) {
   3.184          SDL_DestroyTexture(texture->native);
   3.185      }
   3.186 @@ -1918,16 +1913,49 @@
   3.187  }
   3.188  
   3.189  void
   3.190 +SDL_DestroyTexture(SDL_Texture * texture)
   3.191 +{
   3.192 +    SDL_Renderer *renderer;
   3.193 +
   3.194 +    CHECK_TEXTURE_MAGIC(texture, );
   3.195 +
   3.196 +    renderer = texture->renderer;
   3.197 +    if (texture == renderer->target) {
   3.198 +        SDL_SetRenderTarget(renderer, NULL);
   3.199 +    }
   3.200 +
   3.201 +    texture->magic = NULL;  /* just in case, but we're about to free this... */
   3.202 +
   3.203 +    if (texture->next) {
   3.204 +        texture->next->prev = texture->prev;
   3.205 +    }
   3.206 +    if (texture->prev) {
   3.207 +        texture->prev->next = texture->next;
   3.208 +    } else {
   3.209 +        renderer->textures = texture->next;
   3.210 +    }
   3.211 +
   3.212 +    SDL_DestroyTextureInternal(texture);
   3.213 +}
   3.214 +
   3.215 +void
   3.216  SDL_DestroyRenderer(SDL_Renderer * renderer)
   3.217  {
   3.218 +    SDL_Texture *texture = NULL;
   3.219 +    SDL_Texture *nexttexture = NULL;
   3.220 +
   3.221      CHECK_RENDERER_MAGIC(renderer, );
   3.222  
   3.223      SDL_DelEventWatch(SDL_RendererEventWatch, renderer);
   3.224  
   3.225      /* Free existing textures for this renderer */
   3.226 -    while (renderer->textures) {
   3.227 -        SDL_DestroyTexture(renderer->textures);
   3.228 +    SDL_SetRenderTarget(renderer, NULL);
   3.229 +
   3.230 +    for (texture = renderer->textures; texture; texture = nexttexture) {
   3.231 +        nexttexture = texture->next;
   3.232 +        SDL_DestroyTexture(texture);
   3.233      }
   3.234 +    renderer->textures = NULL;
   3.235  
   3.236      if (renderer->window) {
   3.237          SDL_SetWindowData(renderer->window, SDL_WINDOWRENDERDATA, NULL);
     4.1 --- a/src/render/SDL_yuv_sw.c	Thu Nov 24 21:13:23 2016 -0500
     4.2 +++ b/src/render/SDL_yuv_sw.c	Thu Nov 24 21:41:09 2016 -0500
     4.3 @@ -875,14 +875,16 @@
     4.4   * Low performance, do not call often.
     4.5   */
     4.6  static int
     4.7 +free_bits_at_bottom_nonzero(Uint32 a)
     4.8 +{
     4.9 +    SDL_assert(a != 0);
    4.10 +    return (((Sint32) a) & 1l) ? 0 : 1 + free_bits_at_bottom_nonzero(a >> 1);
    4.11 +}
    4.12 +
    4.13 +static SDL_INLINE int
    4.14  free_bits_at_bottom(Uint32 a)
    4.15  {
    4.16 -    /* assume char is 8 bits */
    4.17 -    if (!a)
    4.18 -        return sizeof(Uint32) * 8;
    4.19 -    if (((Sint32) a) & 1l)
    4.20 -        return 0;
    4.21 -    return 1 + free_bits_at_bottom(a >> 1);
    4.22 +    return a ? free_bits_at_bottom_nonzero(a) : 32;
    4.23  }
    4.24  
    4.25  static int
    4.26 @@ -894,6 +896,7 @@
    4.27      int i;
    4.28      int bpp;
    4.29      Uint32 Rmask, Gmask, Bmask, Amask;
    4.30 +    int freebits;
    4.31  
    4.32      if (!SDL_PixelFormatEnumToMasks
    4.33          (target_format, &bpp, &Rmask, &Gmask, &Bmask, &Amask) || bpp < 15) {
    4.34 @@ -910,13 +913,24 @@
    4.35       */
    4.36      for (i = 0; i < 256; ++i) {
    4.37          r_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(Rmask));
    4.38 -        r_2_pix_alloc[i + 256] <<= free_bits_at_bottom(Rmask);
    4.39 +        freebits = free_bits_at_bottom(Rmask);
    4.40 +        if (freebits < 32) {
    4.41 +            r_2_pix_alloc[i + 256] <<= freebits;
    4.42 +        }
    4.43          r_2_pix_alloc[i + 256] |= Amask;
    4.44 +
    4.45          g_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(Gmask));
    4.46 -        g_2_pix_alloc[i + 256] <<= free_bits_at_bottom(Gmask);
    4.47 +        freebits = free_bits_at_bottom(Gmask);
    4.48 +        if (freebits < 32) {
    4.49 +            g_2_pix_alloc[i + 256] <<= freebits;
    4.50 +        }
    4.51          g_2_pix_alloc[i + 256] |= Amask;
    4.52 +
    4.53          b_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(Bmask));
    4.54 -        b_2_pix_alloc[i + 256] <<= free_bits_at_bottom(Bmask);
    4.55 +        freebits = free_bits_at_bottom(Bmask);
    4.56 +        if (freebits < 32) {
    4.57 +            b_2_pix_alloc[i + 256] <<= freebits;
    4.58 +        }
    4.59          b_2_pix_alloc[i + 256] |= Amask;
    4.60      }
    4.61  
     5.1 --- a/src/render/opengl/SDL_render_gl.c	Thu Nov 24 21:13:23 2016 -0500
     5.2 +++ b/src/render/opengl/SDL_render_gl.c	Thu Nov 24 21:41:09 2016 -0500
     5.3 @@ -1430,18 +1430,21 @@
     5.4  
     5.5      GL_ActivateRenderer(renderer);
     5.6  
     5.7 +    if (!convert_format(data, temp_format, &internalFormat, &format, &type)) {
     5.8 +        return SDL_SetError("Texture format %s not supported by OpenGL",
     5.9 +                            SDL_GetPixelFormatName(temp_format));
    5.10 +    }
    5.11 +
    5.12 +    if (!rect->w || !rect->h) {
    5.13 +        return 0;  /* nothing to do. */
    5.14 +    }
    5.15 +
    5.16      temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
    5.17      temp_pixels = SDL_malloc(rect->h * temp_pitch);
    5.18      if (!temp_pixels) {
    5.19          return SDL_OutOfMemory();
    5.20      }
    5.21  
    5.22 -    if (!convert_format(data, temp_format, &internalFormat, &format, &type)) {
    5.23 -        SDL_free(temp_pixels);
    5.24 -        return SDL_SetError("Texture format %s not supported by OpenGL",
    5.25 -                            SDL_GetPixelFormatName(temp_format));
    5.26 -    }
    5.27 -
    5.28      SDL_GetRendererOutputSize(renderer, &w, &h);
    5.29  
    5.30      data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
     6.1 --- a/src/video/SDL_RLEaccel.c	Thu Nov 24 21:13:23 2016 -0500
     6.2 +++ b/src/video/SDL_RLEaccel.c	Thu Nov 24 21:41:09 2016 -0500
     6.3 @@ -1277,7 +1277,7 @@
     6.4      int y;
     6.5      Uint8 *srcbuf, *lastline;
     6.6      int maxsize = 0;
     6.7 -    int bpp = surface->format->BytesPerPixel;
     6.8 +    const int bpp = surface->format->BytesPerPixel;
     6.9      getpix_func getpix;
    6.10      Uint32 ckey, rgbmask;
    6.11      int w, h;
    6.12 @@ -1300,6 +1300,9 @@
    6.13          maxsize = surface->h * (4 * (surface->w / 65535 + 1)
    6.14                                  + surface->w * 4) + 4;
    6.15          break;
    6.16 +
    6.17 +    default:
    6.18 +        return -1;
    6.19      }
    6.20  
    6.21      rlebuf = (Uint8 *) SDL_malloc(maxsize);
    6.22 @@ -1393,7 +1396,7 @@
    6.23          surface->map->data = p;
    6.24      }
    6.25  
    6.26 -    return (0);
    6.27 +    return 0;
    6.28  }
    6.29  
    6.30  int
     7.1 --- a/src/video/SDL_rect.c	Thu Nov 24 21:13:23 2016 -0500
     7.2 +++ b/src/video/SDL_rect.c	Thu Nov 24 21:41:09 2016 -0500
     7.3 @@ -22,7 +22,7 @@
     7.4  
     7.5  #include "SDL_rect.h"
     7.6  #include "SDL_rect_c.h"
     7.7 -
     7.8 +#include "SDL_assert.h"
     7.9  
    7.10  SDL_bool
    7.11  SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B)
    7.12 @@ -441,9 +441,15 @@
    7.13                  y = recty2;
    7.14                  x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
    7.15              } else if (outcode2 & CODE_LEFT) {
    7.16 +                /* If this assertion ever fires, here's the static analysis that warned about it:
    7.17 +                   http://buildbot.libsdl.org/sdl-static-analysis/sdl-macosx-static-analysis/sdl-macosx-static-analysis-1101/report-b0d01a.html#EndPath */
    7.18 +                SDL_assert(x2 != x1);  /* if equal: division by zero. */
    7.19                  x = rectx1;
    7.20                  y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
    7.21              } else if (outcode2 & CODE_RIGHT) {
    7.22 +                /* If this assertion ever fires, here's the static analysis that warned about it:
    7.23 +                   http://buildbot.libsdl.org/sdl-static-analysis/sdl-macosx-static-analysis/sdl-macosx-static-analysis-1101/report-39b114.html#EndPath */
    7.24 +                SDL_assert(x2 != x1);  /* if equal: division by zero. */
    7.25                  x = rectx2;
    7.26                  y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
    7.27              }
     8.1 --- a/src/video/SDL_video.c	Thu Nov 24 21:13:23 2016 -0500
     8.2 +++ b/src/video/SDL_video.c	Thu Nov 24 21:41:09 2016 -0500
     8.3 @@ -338,9 +338,14 @@
     8.4      /* Create framebuffer data */
     8.5      data->bytes_per_pixel = SDL_BYTESPERPIXEL(*format);
     8.6      data->pitch = (((window->w * data->bytes_per_pixel) + 3) & ~3);
     8.7 -    data->pixels = SDL_malloc(window->h * data->pitch);
     8.8 -    if (!data->pixels) {
     8.9 -        return SDL_OutOfMemory();
    8.10 +
    8.11 +    {
    8.12 +        /* Make static analysis happy about potential malloc(0) calls. */
    8.13 +        const size_t allocsize = window->h * data->pitch;
    8.14 +        data->pixels = SDL_malloc((allocsize > 0) ? allocsize : 1);
    8.15 +        if (!data->pixels) {
    8.16 +            return SDL_OutOfMemory();
    8.17 +        }
    8.18      }
    8.19  
    8.20      *pixels = data->pixels;