test: Add test coverage for surface size overflows Signed-off-by: Simon McVittie <smcv@collabora.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
diff --git a/test/testautomation_surface.c b/test/testautomation_surface.c
index d159c1f..63d4c29 100644
--- a/test/testautomation_surface.c
+++ b/test/testautomation_surface.c
@@ -593,6 +593,177 @@ surface_testBlitBlendLoop(void *arg) {
}
+int
+surface_testOverflow(void *arg)
+{
+ char buf[1024];
+ const char *expectedError;
+ SDL_Surface *surface;
+
+ SDL_memset(buf, '\0', sizeof(buf));
+
+ expectedError = "Parameter 'width' is invalid";
+ surface = SDL_CreateRGBSurfaceWithFormat(0, -3, 100, 8, SDL_PIXELFORMAT_INDEX8);
+ SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+ surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, -1, 1, 8, 4, SDL_PIXELFORMAT_INDEX8);
+ SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+ surface = SDL_CreateRGBSurfaceFrom(buf, -1, 1, 32, 4, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
+ SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+
+ expectedError = "Parameter 'height' is invalid";
+ surface = SDL_CreateRGBSurfaceWithFormat(0, 100, -3, 8, SDL_PIXELFORMAT_INDEX8);
+ SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+ surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 1, -1, 8, 4, SDL_PIXELFORMAT_INDEX8);
+ SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+ surface = SDL_CreateRGBSurfaceFrom(buf, 1, -1, 32, 4, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
+ SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+
+ expectedError = "Parameter 'pitch' is invalid";
+ surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 4, 1, 8, -1, SDL_PIXELFORMAT_INDEX8);
+ SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+ surface = SDL_CreateRGBSurfaceFrom(buf, 1, 1, 32, -1, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
+ SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+
+ /* Less than 1 byte per pixel: the pitch can legitimately be less than
+ * the width, but it must be enough to hold the appropriate number of
+ * bits per pixel. SDL_PIXELFORMAT_INDEX4* needs 1 byte per 2 pixels. */
+ surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 6, 1, 4, 3, SDL_PIXELFORMAT_INDEX4LSB);
+ SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
+ surface != NULL ? "(success)" : SDL_GetError());
+ SDL_FreeSurface(surface);
+ surface = SDL_CreateRGBSurfaceFrom(buf, 6, 1, 4, 3, 0, 0, 0, 0);
+ SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
+ surface != NULL ? "(success)" : SDL_GetError());
+ SDL_FreeSurface(surface);
+
+ surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 7, 1, 4, 3, SDL_PIXELFORMAT_INDEX4LSB);
+ SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+ surface = SDL_CreateRGBSurfaceFrom(buf, 7, 1, 4, 3, 0, 0, 0, 0);
+ SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+
+ surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 7, 1, 4, 4, SDL_PIXELFORMAT_INDEX4LSB);
+ SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
+ surface != NULL ? "(success)" : SDL_GetError());
+ SDL_FreeSurface(surface);
+ surface = SDL_CreateRGBSurfaceFrom(buf, 7, 1, 4, 4, 0, 0, 0, 0);
+ SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
+ surface != NULL ? "(success)" : SDL_GetError());
+ SDL_FreeSurface(surface);
+
+ /* SDL_PIXELFORMAT_INDEX1* needs 1 byte per 8 pixels. */
+ surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 16, 1, 1, 2, SDL_PIXELFORMAT_INDEX1LSB);
+ SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
+ surface != NULL ? "(success)" : SDL_GetError());
+ SDL_FreeSurface(surface);
+ surface = SDL_CreateRGBSurfaceFrom(buf, 16, 1, 1, 2, 0, 0, 0, 0);
+ SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
+ surface != NULL ? "(success)" : SDL_GetError());
+ SDL_FreeSurface(surface);
+
+ surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 17, 1, 1, 2, SDL_PIXELFORMAT_INDEX1LSB);
+ SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+ surface = SDL_CreateRGBSurfaceFrom(buf, 17, 1, 1, 2, 0, 0, 0, 0);
+ SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+
+ surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 17, 1, 1, 3, SDL_PIXELFORMAT_INDEX1LSB);
+ SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
+ surface != NULL ? "(success)" : SDL_GetError());
+ SDL_FreeSurface(surface);
+ surface = SDL_CreateRGBSurfaceFrom(buf, 7, 1, 1, 3, 0, 0, 0, 0);
+ SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
+ surface != NULL ? "(success)" : SDL_GetError());
+ SDL_FreeSurface(surface);
+
+ /* SDL_PIXELFORMAT_INDEX8 and SDL_PIXELFORMAT_RGB332 require 1 byte per pixel. */
+ surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 5, 1, 8, 5, SDL_PIXELFORMAT_RGB332);
+ SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
+ surface != NULL ? "(success)" : SDL_GetError());
+ SDL_FreeSurface(surface);
+ surface = SDL_CreateRGBSurfaceFrom(buf, 5, 1, 8, 5, 0, 0, 0, 0);
+ SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
+ surface != NULL ? "(success)" : SDL_GetError());
+ SDL_FreeSurface(surface);
+
+ surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 6, 1, 8, 5, SDL_PIXELFORMAT_RGB332);
+ SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+ surface = SDL_CreateRGBSurfaceFrom(buf, 6, 1, 8, 5, 0, 0, 0, 0);
+ SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+
+ /* Everything else requires more than 1 byte per pixel, and rounds up
+ * each pixel to an integer number of bytes (e.g. RGB555 is really
+ * XRGB1555, with 1 bit per pixel wasted). */
+ surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 3, 1, 15, 6, SDL_PIXELFORMAT_RGB555);
+ SDLTest_AssertCheck(surface != NULL, "3px * 15 (really 16) bits per px fits in 6 bytes: %s",
+ surface != NULL ? "(success)" : SDL_GetError());
+ SDL_FreeSurface(surface);
+ surface = SDL_CreateRGBSurfaceFrom(buf, 3, 1, 15, 6, 0, 0, 0, 0);
+ SDLTest_AssertCheck(surface != NULL, "5px * 15 (really 16) bits per px fits in 6 bytes: %s",
+ surface != NULL ? "(success)" : SDL_GetError());
+ SDL_FreeSurface(surface);
+
+ surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 4, 1, 15, 6, SDL_PIXELFORMAT_RGB555);
+ SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+ surface = SDL_CreateRGBSurfaceFrom(buf, 4, 1, 15, 6, 0, 0, 0, 0);
+ SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+
+ if (sizeof (size_t) == 4 && sizeof (int) >= 4) {
+ expectedError = "Out of memory";
+ surface = SDL_CreateRGBSurfaceWithFormat(0, SDL_MAX_SINT32, 1, 8, SDL_PIXELFORMAT_INDEX8);
+ SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width + alignment");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+ surface = SDL_CreateRGBSurfaceWithFormat(0, SDL_MAX_SINT32 / 2, 1, 32, SDL_PIXELFORMAT_ARGB8888);
+ SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * bytes per pixel");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+ surface = SDL_CreateRGBSurfaceWithFormat(0, (1 << 29) - 1, (1 << 29) - 1, 8, SDL_PIXELFORMAT_INDEX8);
+ SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+ surface = SDL_CreateRGBSurfaceWithFormat(0, (1 << 15) + 1, (1 << 15) + 1, 32, SDL_PIXELFORMAT_ARGB8888);
+ SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height * bytes per pixel");
+ SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
+ "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
+ }
+ else {
+ SDLTest_Log("Can't easily overflow size_t on this platform");
+ }
+
+ return TEST_COMPLETED;
+}
+
/* ================= Test References ================== */
/* Surface test cases */
@@ -635,11 +806,14 @@ static const SDLTest_TestCaseReference surfaceTest11 =
static const SDLTest_TestCaseReference surfaceTest12 =
{ (SDLTest_TestCaseFp)surface_testBlitBlendMod, "surface_testBlitBlendMod", "Tests blitting routines with mod blending mode.", TEST_ENABLED};
+static const SDLTest_TestCaseReference surfaceTestOverflow =
+ { surface_testOverflow, "surface_testOverflow", "Test overflow detection.", TEST_ENABLED};
+
/* Sequence of Surface test cases */
static const SDLTest_TestCaseReference *surfaceTests[] = {
&surfaceTest1, &surfaceTest2, &surfaceTest3, &surfaceTest4, &surfaceTest5,
&surfaceTest6, &surfaceTest7, &surfaceTest8, &surfaceTest9, &surfaceTest10,
- &surfaceTest11, &surfaceTest12, NULL
+ &surfaceTest11, &surfaceTest12, &surfaceTestOverflow, NULL
};
/* Surface test suite (global) */