Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bounds check for ImageDrawRectangleRec #3732

Merged
merged 1 commit into from
Jan 14, 2024

Conversation

Blockguy24
Copy link
Contributor

I was having originally having issues with image drawing functions causing memory corruptions, I found that the condition for the bounds check in ImageDrawRectangleRec misses cases near the edges of the Image. I used code like this, running with valgrind, to verify that the memory corruption no longer occurs: (sorry for the mess)

#include <stdio.h>

#include "raylib.h"

#define IMAGE_SIZE 4
#define OVERSCAN 2
#define MAX_DRAW_SIZE (IMAGE_SIZE + OVERSCAN * 2)

int main()
{
    Image image = GenImageColor(IMAGE_SIZE, IMAGE_SIZE, BLANK);

    for (int y = -(MAX_DRAW_SIZE + OVERSCAN); y <= IMAGE_SIZE + OVERSCAN; y++)
    {
        for (int x = -(MAX_DRAW_SIZE + OVERSCAN); x <= IMAGE_SIZE + OVERSCAN; x++)
        {
            for (int w = -MAX_DRAW_SIZE; w <= MAX_DRAW_SIZE; w++)
            {
                for (int h = -MAX_DRAW_SIZE; h <= MAX_DRAW_SIZE; h++)
                {
                    printf("x=%d, y=%d, w=%d, h=%d\n", x, y, w, h);
                    ImageDrawRectangle(&image, x, y, w, h, WHITE);
                }
            }
        }
    }

    printf("unloading...\n");
    UnloadImage(image);

    return 0;
}

I also implemented the additional suggestion from (#3721) and verified that extra pixels are no longer drawn at the edge of the image.

@raysan5 raysan5 merged commit d2b1256 into raysan5:master Jan 14, 2024
@raysan5
Copy link
Owner

raysan5 commented Jan 14, 2024

@Blockguy24 Oh! Good catch! Thanks for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants