aboutsummaryrefslogtreecommitdiff
path: root/src/scale.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/scale.c')
-rw-r--r--src/scale.c26
1 files changed, 19 insertions, 7 deletions
diff --git a/src/scale.c b/src/scale.c
index f435395..637db86 100644
--- a/src/scale.c
+++ b/src/scale.c
@@ -1,15 +1,23 @@
#include "scale.h"
+static bool is_safei(int i) {
+ return i >= -PW_MAX_SAFE_INT && i <= PW_MAX_SAFE_INT;
+}
+
+static bool is_safef(float f) {
+ return f >= -PW_MAX_SAFE_INT && f <= PW_MAX_SAFE_INT && (int32_t)f != 0;
+}
+
static SDL_ScaleMode scale_pixel_perfect(float twf, float thf, int rw, int rh,
SDL_FRect *target) {
- int tw = (int)twf;
- int th = (int)thf;
- int scale = SDL_min(rw / tw, rh / th);
+ int32_t tw = (int32_t)twf;
+ int32_t th = (int32_t)thf;
+ int32_t scale = SDL_min(rw / tw, rh / th);
- int w = scale * tw;
- int h = scale * th;
- int x = (rw - w) / 2;
- int y = (rh - h) / 2;
+ int32_t w = scale * tw;
+ int32_t h = scale * th;
+ int32_t x = (rw - w) / 2;
+ int32_t y = (rh - h) / 2;
target->w = (float)w;
target->h = (float)h;
@@ -92,6 +100,10 @@ bool pw_scale(enum pw_scale_mode mode, SDL_Texture *texture,
return false;
}
+ if (!is_safef(twf) || !is_safef(thf) || !is_safei(rw) || !is_safei(rh)) {
+ return SDL_SetError("unreasonable texture or render sizes");
+ }
+
SDL_FRect t;
SDL_ScaleMode m = scalefn(twf, thf, rw, rh, &t);
if (!SDL_SetTextureScaleMode(texture, m)) {