Added additional input validation to SDL_AllocPalette; added error codes; added test case to Pixels suite for coverage
authorAndreas Schiffler <aschiffler@ferzkopp.net>
Sat, 04 May 2013 09:11:18 -0700
changeset 7142f4a670e51cde
parent 7141 e276777b4247
child 7143 fa117b9cc89b
Added additional input validation to SDL_AllocPalette; added error codes; added test case to Pixels suite for coverage
src/video/SDL_pixels.c
test/testautomation_pixels.c
     1.1 --- a/src/video/SDL_pixels.c	Sat May 04 04:46:00 2013 -0700
     1.2 +++ b/src/video/SDL_pixels.c	Sat May 04 09:11:18 2013 -0700
     1.3 @@ -503,6 +503,7 @@
     1.4      }
     1.5      if (SDL_InitFormat(format, pixel_format) < 0) {
     1.6          SDL_free(format);
     1.7 +        SDL_InvalidParamError("format");
     1.8          return NULL;
     1.9      }
    1.10  
    1.11 @@ -585,6 +586,7 @@
    1.12      SDL_PixelFormat *prev;
    1.13  
    1.14      if (!format) {
    1.15 +        SDL_InvalidParamError("format");
    1.16          return;
    1.17      }
    1.18      if (--format->refcount > 0) {
    1.19 @@ -613,6 +615,12 @@
    1.20  SDL_AllocPalette(int ncolors)
    1.21  {
    1.22      SDL_Palette *palette;
    1.23 +    
    1.24 +    /* Input validation */
    1.25 +    if (ncolors < 1) {
    1.26 +      SDL_InvalidParamError("ncolors");
    1.27 +      return NULL;
    1.28 +    }
    1.29  
    1.30      palette = (SDL_Palette *) SDL_malloc(sizeof(*palette));
    1.31      if (!palette) {
    1.32 @@ -693,6 +701,7 @@
    1.33  SDL_FreePalette(SDL_Palette * palette)
    1.34  {
    1.35      if (!palette) {
    1.36 +        SDL_InvalidParamError("palette");
    1.37          return;
    1.38      }
    1.39      if (--palette->refcount > 0) {
     2.1 --- a/test/testautomation_pixels.c	Sat May 04 04:46:00 2013 -0700
     2.2 +++ b/test/testautomation_pixels.c	Sat May 04 09:11:18 2013 -0700
     2.3 @@ -101,6 +101,9 @@
     2.4  
     2.5  /**
     2.6   * @brief Call to SDL_AllocFormat and SDL_FreeFormat
     2.7 + *
     2.8 + * @sa http://wiki.libsdl.org/moin.fcg/SDL_AllocFormat
     2.9 + * @sa http://wiki.libsdl.org/moin.fcg/SDL_FreeFormat 
    2.10   */
    2.11  int
    2.12  pixels_allocFreeFormat(void *arg)
    2.13 @@ -145,18 +148,100 @@
    2.14      SDLTest_AssertCheck(result == NULL, "Verify result is NULL");  
    2.15    }
    2.16    
    2.17 +  /* Negative cases */
    2.18 +  format = 0xffffffff;
    2.19 +  result = SDL_AllocFormat(format);
    2.20 +  SDLTest_AssertPass("Call to SDL_AllocFormat(0xffffffff)");
    2.21 +  SDLTest_AssertCheck(result == NULL, "Verify result is NULL");  
    2.22 +  /* TODO: check error code */
    2.23 +
    2.24 +  SDL_FreeFormat(NULL);
    2.25 +  SDLTest_AssertPass("Call to SDL_FreeFormat(NULL)");
    2.26 +  /* TODO: check error code */
    2.27 +  
    2.28 +  return TEST_COMPLETED;
    2.29 +}
    2.30 +
    2.31 +/**
    2.32 + * @brief Call to SDL_AllocPalette and SDL_FreePalette
    2.33 + *
    2.34 + * @sa http://wiki.libsdl.org/moin.fcg/SDL_AllocPalette
    2.35 + * @sa http://wiki.libsdl.org/moin.fcg/SDL_FreePalette
    2.36 + */
    2.37 +int
    2.38 +pixels_allocFreePalette(void *arg)
    2.39 +{
    2.40 +  int variation;
    2.41 +  int i;
    2.42 +  int ncolors;
    2.43 +  SDL_Palette* result;
    2.44 +
    2.45 +  /* Allocate palette */
    2.46 +  for (variation = 1; variation <= 3; variation++) {  
    2.47 +    switch (variation) {
    2.48 +      /* Just one color */
    2.49 +      case 1:
    2.50 +        ncolors = 1;
    2.51 +        break;
    2.52 +      /* Two colors */
    2.53 +      case 2:
    2.54 +        ncolors = 2;
    2.55 +        break;
    2.56 +      /* More than two colors */
    2.57 +      case 3:
    2.58 +        ncolors = SDLTest_RandomIntegerInRange(8, 16);
    2.59 +        break;
    2.60 +    }
    2.61 +
    2.62 +    result = SDL_AllocPalette(ncolors);
    2.63 +    SDLTest_AssertPass("Call to SDL_AllocPalette(%d)", ncolors);
    2.64 +    SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");  
    2.65 +    if (result != NULL) {
    2.66 +      SDLTest_AssertCheck(result->ncolors == ncolors, "Verify value of result.ncolors; expected: %u, got %u", ncolors, result->ncolors);  
    2.67 +      if (result->ncolors > 0) {
    2.68 +        SDLTest_AssertCheck(result->colors != NULL, "Verify value of result.colors is not NULL");
    2.69 +        if (result->colors != NULL) {
    2.70 +          for(i = 0; i < result->ncolors; i++) {
    2.71 +            SDLTest_AssertCheck(result->colors[i].r == 255, "Verify value of result.colors[%d].r; expected: 255, got %u", i, result->colors[i].r);  
    2.72 +            SDLTest_AssertCheck(result->colors[i].g == 255, "Verify value of result.colors[%d].g; expected: 255, got %u", i, result->colors[i].g);  
    2.73 +            SDLTest_AssertCheck(result->colors[i].b == 255, "Verify value of result.colors[%d].b; expected: 255, got %u", i, result->colors[i].b);  
    2.74 +           }
    2.75 +         } 
    2.76 +      }
    2.77 +      
    2.78 +      /* Deallocate again */
    2.79 +      SDL_FreePalette(result);
    2.80 +      SDLTest_AssertPass("Call to SDL_FreePalette()");          
    2.81 +    }
    2.82 +  }
    2.83 +
    2.84 +  /* Negative cases */
    2.85 +  for (ncolors = 0; ncolors > -3; ncolors--) {
    2.86 +    result = SDL_AllocPalette(ncolors);
    2.87 +    SDLTest_AssertPass("Call to SDL_AllocPalette(%d)", ncolors);
    2.88 +    SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
    2.89 +    /* TODO: check error code */
    2.90 +  }
    2.91 +
    2.92 +  SDL_FreePalette(NULL);
    2.93 +  SDLTest_AssertPass("Call to SDL_FreePalette(NULL)");
    2.94 +  /* TODO: check error code */
    2.95 +    
    2.96    return TEST_COMPLETED;
    2.97  }
    2.98  
    2.99  /* ================= Test References ================== */
   2.100  
   2.101 -/* SysWM test cases */
   2.102 +/* Pixels test cases */
   2.103  static const SDLTest_TestCaseReference pixelsTest1 =
   2.104  		{ (SDLTest_TestCaseFp)pixels_allocFreeFormat, "pixels_allocFreeFormat", "Call to SDL_AllocFormat and SDL_FreeFormat", TEST_ENABLED };
   2.105  
   2.106 +static const SDLTest_TestCaseReference pixelsTest2 =
   2.107 +		{ (SDLTest_TestCaseFp)pixels_allocFreePalette, "pixels_allocFreePalette", "Call to SDL_AllocPalette and SDL_FreePalette", TEST_ENABLED };
   2.108 +
   2.109  /* Sequence of Pixels test cases */
   2.110  static const SDLTest_TestCaseReference *pixelsTests[] =  {
   2.111 -	&pixelsTest1, NULL
   2.112 +	&pixelsTest1, &pixelsTest2, NULL
   2.113  };
   2.114  
   2.115  /* Pixels test suite (global) */