Fixed all known static analysis bugs, with checker-279 on macOS.

Ryan C. Gordon 2016-11-24 21:41:09 -05:00
parent fd250bd636
commit fb5fd67ccb
8 changed files with 159 additions and 88 deletions

View File

@ -24,6 +24,8 @@ static const char rcsid[] =
#include "math_libm.h" #include "math_libm.h"
#include "math_private.h" #include "math_private.h"
#include "SDL_assert.h"
libm_hidden_proto(fabs) libm_hidden_proto(fabs)
/* /*
@ -189,7 +191,12 @@ __ieee754_rem_pio2(x, y)
} }
tx[2] = z; tx[2] = z;
nx = 3; nx = 3;
while (tx[nx - 1] == zero)
/* If this assertion ever fires, here's the static analysis that warned about it:
http://buildbot.libsdl.org/sdl-static-analysis/sdl-macosx-static-analysis/sdl-macosx-static-analysis-1101/report-8c6ccb.html#EndPath */
SDL_assert((tx[0] != zero) || (tx[1] != zero) || (tx[2] != zero));
while (nx && tx[nx - 1] == zero)
nx--; /* skip zero term */ nx--; /* skip zero term */
n = __kernel_rem_pio2(tx, y, e0, nx, 2, two_over_pi); n = __kernel_rem_pio2(tx, y, e0, nx, 2, two_over_pi);
if (hx < 0) { if (hx < 0) {

View File

@ -204,8 +204,13 @@ __kernel_rem_pio2(x, y, e0, nx, prec, ipio2)
/* compute q[0],q[1],...q[jk] */ /* compute q[0],q[1],...q[jk] */
for (i = 0; i <= jk; i++) { for (i = 0; i <= jk; i++) {
for (j = 0, fw = 0.0; j <= jx; j++) for (j = 0, fw = 0.0; j <= jx; j++) {
fw += x[j] * f[jx + i - j]; const int32_t idx = jx + i - j;
SDL_assert(idx >= 0);
SDL_assert(idx < 20);
SDL_assert(idx <= m);
fw += x[j] * f[idx];
}
q[i] = fw; q[i] = fw;
} }

View File

@ -734,8 +734,8 @@ SDL_UpdateTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
if (texture->access == SDL_TEXTUREACCESS_STREAMING) { if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
/* We can lock the texture and copy to it */ /* We can lock the texture and copy to it */
void *native_pixels; void *native_pixels = NULL;
int native_pitch; int native_pitch = 0;
if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) { if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
return -1; return -1;
@ -745,11 +745,10 @@ SDL_UpdateTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
SDL_UnlockTexture(native); SDL_UnlockTexture(native);
} else { } else {
/* Use a temporary buffer for updating */ /* Use a temporary buffer for updating */
void *temp_pixels; const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
int temp_pitch; const size_t alloclen = rect->h * temp_pitch;
if (alloclen > 0) {
temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3); void *temp_pixels = SDL_malloc(alloclen);
temp_pixels = SDL_malloc(rect->h * temp_pitch);
if (!temp_pixels) { if (!temp_pixels) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
@ -758,6 +757,7 @@ SDL_UpdateTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch); SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
SDL_free(temp_pixels); SDL_free(temp_pixels);
} }
}
return 0; return 0;
} }
@ -767,10 +767,14 @@ SDL_UpdateTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
{ {
SDL_Texture *native = texture->native; SDL_Texture *native = texture->native;
if (!rect->w || !rect->h) {
return 0; /* nothing to do. */
}
if (texture->access == SDL_TEXTUREACCESS_STREAMING) { if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
/* We can lock the texture and copy to it */ /* We can lock the texture and copy to it */
void *native_pixels; void *native_pixels = NULL;
int native_pitch; int native_pitch = 0;
if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) { if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
return -1; return -1;
@ -781,11 +785,10 @@ SDL_UpdateTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
SDL_UnlockTexture(native); SDL_UnlockTexture(native);
} else { } else {
/* Use a temporary buffer for updating */ /* Use a temporary buffer for updating */
void *temp_pixels; const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
int temp_pitch; const size_t alloclen = rect->h * temp_pitch;
if (alloclen > 0) {
temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3); void *temp_pixels = SDL_malloc(alloclen);
temp_pixels = SDL_malloc(rect->h * temp_pitch);
if (!temp_pixels) { if (!temp_pixels) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
@ -795,6 +798,7 @@ SDL_UpdateTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch); SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
SDL_free(temp_pixels); SDL_free(temp_pixels);
} }
}
return 0; return 0;
} }
@ -853,10 +857,14 @@ SDL_UpdateTextureYUVPlanar(SDL_Texture * texture, const SDL_Rect * rect,
full_rect.h = texture->h; full_rect.h = texture->h;
rect = &full_rect; rect = &full_rect;
if (!rect->w || !rect->h) {
return 0; /* nothing to do. */
}
if (texture->access == SDL_TEXTUREACCESS_STREAMING) { if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
/* We can lock the texture and copy to it */ /* We can lock the texture and copy to it */
void *native_pixels; void *native_pixels = NULL;
int native_pitch; int native_pitch = 0;
if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) { if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
return -1; return -1;
@ -866,11 +874,10 @@ SDL_UpdateTextureYUVPlanar(SDL_Texture * texture, const SDL_Rect * rect,
SDL_UnlockTexture(native); SDL_UnlockTexture(native);
} else { } else {
/* Use a temporary buffer for updating */ /* Use a temporary buffer for updating */
void *temp_pixels; const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
int temp_pitch; const size_t alloclen = rect->h * temp_pitch;
if (alloclen > 0) {
temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3); void *temp_pixels = SDL_malloc(alloclen);
temp_pixels = SDL_malloc(rect->h * temp_pitch);
if (!temp_pixels) { if (!temp_pixels) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
@ -879,6 +886,7 @@ SDL_UpdateTextureYUVPlanar(SDL_Texture * texture, const SDL_Rect * rect,
SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch); SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
SDL_free(temp_pixels); SDL_free(temp_pixels);
} }
}
return 0; return 0;
} }
@ -924,6 +932,10 @@ int SDL_UpdateYUVTexture(SDL_Texture * texture, const SDL_Rect * rect,
rect = &full_rect; rect = &full_rect;
} }
if (!rect->w || !rect->h) {
return 0; /* nothing to do. */
}
if (texture->yuv) { if (texture->yuv) {
return SDL_UpdateTextureYUVPlanar(texture, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch); return SDL_UpdateTextureYUVPlanar(texture, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
} else { } else {
@ -1882,29 +1894,12 @@ SDL_RenderPresent(SDL_Renderer * renderer)
renderer->RenderPresent(renderer); renderer->RenderPresent(renderer);
} }
void /* this isn't responsible for removing the deleted texture from the list!
SDL_DestroyTexture(SDL_Texture * texture) (this is to keep static analysis happy in SDL_DestroyRenderer().) */
static void
SDL_DestroyTextureInternal(SDL_Texture * texture)
{ {
SDL_Renderer *renderer; SDL_Renderer *renderer = texture->renderer;
CHECK_TEXTURE_MAGIC(texture, );
renderer = texture->renderer;
if (texture == renderer->target) {
SDL_SetRenderTarget(renderer, NULL);
}
texture->magic = NULL;
if (texture->next) {
texture->next->prev = texture->prev;
}
if (texture->prev) {
texture->prev->next = texture->next;
} else {
renderer->textures = texture->next;
}
if (texture->native) { if (texture->native) {
SDL_DestroyTexture(texture->native); SDL_DestroyTexture(texture->native);
} }
@ -1917,17 +1912,50 @@ SDL_DestroyTexture(SDL_Texture * texture)
SDL_free(texture); SDL_free(texture);
} }
void
SDL_DestroyTexture(SDL_Texture * texture)
{
SDL_Renderer *renderer;
CHECK_TEXTURE_MAGIC(texture, );
renderer = texture->renderer;
if (texture == renderer->target) {
SDL_SetRenderTarget(renderer, NULL);
}
texture->magic = NULL; /* just in case, but we're about to free this... */
if (texture->next) {
texture->next->prev = texture->prev;
}
if (texture->prev) {
texture->prev->next = texture->next;
} else {
renderer->textures = texture->next;
}
SDL_DestroyTextureInternal(texture);
}
void void
SDL_DestroyRenderer(SDL_Renderer * renderer) SDL_DestroyRenderer(SDL_Renderer * renderer)
{ {
SDL_Texture *texture = NULL;
SDL_Texture *nexttexture = NULL;
CHECK_RENDERER_MAGIC(renderer, ); CHECK_RENDERER_MAGIC(renderer, );
SDL_DelEventWatch(SDL_RendererEventWatch, renderer); SDL_DelEventWatch(SDL_RendererEventWatch, renderer);
/* Free existing textures for this renderer */ /* Free existing textures for this renderer */
while (renderer->textures) { SDL_SetRenderTarget(renderer, NULL);
SDL_DestroyTexture(renderer->textures);
for (texture = renderer->textures; texture; texture = nexttexture) {
nexttexture = texture->next;
SDL_DestroyTexture(texture);
} }
renderer->textures = NULL;
if (renderer->window) { if (renderer->window) {
SDL_SetWindowData(renderer->window, SDL_WINDOWRENDERDATA, NULL); SDL_SetWindowData(renderer->window, SDL_WINDOWRENDERDATA, NULL);

View File

@ -875,14 +875,16 @@ number_of_bits_set(Uint32 a)
* Low performance, do not call often. * Low performance, do not call often.
*/ */
static int static int
free_bits_at_bottom_nonzero(Uint32 a)
{
SDL_assert(a != 0);
return (((Sint32) a) & 1l) ? 0 : 1 + free_bits_at_bottom_nonzero(a >> 1);
}
static SDL_INLINE int
free_bits_at_bottom(Uint32 a) free_bits_at_bottom(Uint32 a)
{ {
/* assume char is 8 bits */ return a ? free_bits_at_bottom_nonzero(a) : 32;
if (!a)
return sizeof(Uint32) * 8;
if (((Sint32) a) & 1l)
return 0;
return 1 + free_bits_at_bottom(a >> 1);
} }
static int static int
@ -894,6 +896,7 @@ SDL_SW_SetupYUVDisplay(SDL_SW_YUVTexture * swdata, Uint32 target_format)
int i; int i;
int bpp; int bpp;
Uint32 Rmask, Gmask, Bmask, Amask; Uint32 Rmask, Gmask, Bmask, Amask;
int freebits;
if (!SDL_PixelFormatEnumToMasks if (!SDL_PixelFormatEnumToMasks
(target_format, &bpp, &Rmask, &Gmask, &Bmask, &Amask) || bpp < 15) { (target_format, &bpp, &Rmask, &Gmask, &Bmask, &Amask) || bpp < 15) {
@ -910,13 +913,24 @@ SDL_SW_SetupYUVDisplay(SDL_SW_YUVTexture * swdata, Uint32 target_format)
*/ */
for (i = 0; i < 256; ++i) { for (i = 0; i < 256; ++i) {
r_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(Rmask)); r_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(Rmask));
r_2_pix_alloc[i + 256] <<= free_bits_at_bottom(Rmask); freebits = free_bits_at_bottom(Rmask);
if (freebits < 32) {
r_2_pix_alloc[i + 256] <<= freebits;
}
r_2_pix_alloc[i + 256] |= Amask; r_2_pix_alloc[i + 256] |= Amask;
g_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(Gmask)); g_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(Gmask));
g_2_pix_alloc[i + 256] <<= free_bits_at_bottom(Gmask); freebits = free_bits_at_bottom(Gmask);
if (freebits < 32) {
g_2_pix_alloc[i + 256] <<= freebits;
}
g_2_pix_alloc[i + 256] |= Amask; g_2_pix_alloc[i + 256] |= Amask;
b_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(Bmask)); b_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(Bmask));
b_2_pix_alloc[i + 256] <<= free_bits_at_bottom(Bmask); freebits = free_bits_at_bottom(Bmask);
if (freebits < 32) {
b_2_pix_alloc[i + 256] <<= freebits;
}
b_2_pix_alloc[i + 256] |= Amask; b_2_pix_alloc[i + 256] |= Amask;
} }

View File

@ -1430,18 +1430,21 @@ GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
GL_ActivateRenderer(renderer); GL_ActivateRenderer(renderer);
if (!convert_format(data, temp_format, &internalFormat, &format, &type)) {
return SDL_SetError("Texture format %s not supported by OpenGL",
SDL_GetPixelFormatName(temp_format));
}
if (!rect->w || !rect->h) {
return 0; /* nothing to do. */
}
temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format); temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
temp_pixels = SDL_malloc(rect->h * temp_pitch); temp_pixels = SDL_malloc(rect->h * temp_pitch);
if (!temp_pixels) { if (!temp_pixels) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
if (!convert_format(data, temp_format, &internalFormat, &format, &type)) {
SDL_free(temp_pixels);
return SDL_SetError("Texture format %s not supported by OpenGL",
SDL_GetPixelFormatName(temp_format));
}
SDL_GetRendererOutputSize(renderer, &w, &h); SDL_GetRendererOutputSize(renderer, &w, &h);
data->glPixelStorei(GL_PACK_ALIGNMENT, 1); data->glPixelStorei(GL_PACK_ALIGNMENT, 1);

View File

@ -1277,7 +1277,7 @@ RLEColorkeySurface(SDL_Surface * surface)
int y; int y;
Uint8 *srcbuf, *lastline; Uint8 *srcbuf, *lastline;
int maxsize = 0; int maxsize = 0;
int bpp = surface->format->BytesPerPixel; const int bpp = surface->format->BytesPerPixel;
getpix_func getpix; getpix_func getpix;
Uint32 ckey, rgbmask; Uint32 ckey, rgbmask;
int w, h; int w, h;
@ -1300,6 +1300,9 @@ RLEColorkeySurface(SDL_Surface * surface)
maxsize = surface->h * (4 * (surface->w / 65535 + 1) maxsize = surface->h * (4 * (surface->w / 65535 + 1)
+ surface->w * 4) + 4; + surface->w * 4) + 4;
break; break;
default:
return -1;
} }
rlebuf = (Uint8 *) SDL_malloc(maxsize); rlebuf = (Uint8 *) SDL_malloc(maxsize);
@ -1393,7 +1396,7 @@ RLEColorkeySurface(SDL_Surface * surface)
surface->map->data = p; surface->map->data = p;
} }
return (0); return 0;
} }
int int

View File

@ -22,7 +22,7 @@
#include "SDL_rect.h" #include "SDL_rect.h"
#include "SDL_rect_c.h" #include "SDL_rect_c.h"
#include "SDL_assert.h"
SDL_bool SDL_bool
SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B) SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B)
@ -441,9 +441,15 @@ SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2,
y = recty2; y = recty2;
x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1); x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
} else if (outcode2 & CODE_LEFT) { } else if (outcode2 & CODE_LEFT) {
/* If this assertion ever fires, here's the static analysis that warned about it:
http://buildbot.libsdl.org/sdl-static-analysis/sdl-macosx-static-analysis/sdl-macosx-static-analysis-1101/report-b0d01a.html#EndPath */
SDL_assert(x2 != x1); /* if equal: division by zero. */
x = rectx1; x = rectx1;
y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1); y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
} else if (outcode2 & CODE_RIGHT) { } else if (outcode2 & CODE_RIGHT) {
/* If this assertion ever fires, here's the static analysis that warned about it:
http://buildbot.libsdl.org/sdl-static-analysis/sdl-macosx-static-analysis/sdl-macosx-static-analysis-1101/report-39b114.html#EndPath */
SDL_assert(x2 != x1); /* if equal: division by zero. */
x = rectx2; x = rectx2;
y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1); y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
} }

View File

@ -338,10 +338,15 @@ SDL_CreateWindowTexture(SDL_VideoDevice *unused, SDL_Window * window, Uint32 * f
/* Create framebuffer data */ /* Create framebuffer data */
data->bytes_per_pixel = SDL_BYTESPERPIXEL(*format); data->bytes_per_pixel = SDL_BYTESPERPIXEL(*format);
data->pitch = (((window->w * data->bytes_per_pixel) + 3) & ~3); data->pitch = (((window->w * data->bytes_per_pixel) + 3) & ~3);
data->pixels = SDL_malloc(window->h * data->pitch);
{
/* Make static analysis happy about potential malloc(0) calls. */
const size_t allocsize = window->h * data->pitch;
data->pixels = SDL_malloc((allocsize > 0) ? allocsize : 1);
if (!data->pixels) { if (!data->pixels) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
}
*pixels = data->pixels; *pixels = data->pixels;
*pitch = data->pitch; *pitch = data->pitch;