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 stbi__parse_png_file() reading too much bytes #526

Merged
merged 1 commit into from
Feb 25, 2025

Conversation

miniupnp
Copy link
Contributor

just "rewind" the bytes that have been read past the IEND chunk.

should fix #360 and #524

just "rewind" the bytes that have been read past the IEND
chunk.

should fix libsdl-org#360 and libsdl-org#524
@miniupnp
Copy link
Contributor Author

@flowCRANE could you check if that fixes your issue ?

I added a couple of bytes at the end of a file and checked using ftell() that is is indeed rewinding the additional bytes ;)

I guess I should submit the patch to https://github.com/nothings/stb if it works for you

@flowCRANE
Copy link

flowCRANE commented Feb 24, 2025

If it works for you, it will work for me. You can test it on my sample file — test.files.zip (inside is sdl.dat file).

If you are able to open sdl.dat file and read two images from the stream successively, without any errors, in this way:

Stream := SDL_IOFromFile('sdl.dat', 'rb');

Image1 := IMG_LoadTexture_IO(Renderer, Stream, False);
Image2 := IMG_LoadTexture_IO(Renderer, Stream, False);

then it means that your fix is correct and solves the problem. And thanks so much for looking into this topic and finding the cause of the problems!

@miniupnp
Copy link
Contributor Author

Some Pascal :)

@flowCRANE So you are not able to test the patch ?

@miniupnp
Copy link
Contributor Author

With the following code, img2 is null without the patch and is not null with the patch.
So I guess it works.

{
	SDL_IOStream *src = SDL_IOFromFile("/home/nanard/code/git/SDL_image/sdl.dat", "rb");
	SDL_Texture *img1 = IMG_LoadTexture_IO(renderer, src, false);
	SDL_Texture *img2 = IMG_LoadTexture_IO(renderer, src, true);
        printf("src=%p img1=%p img2=%p\n", src, img1, img2);
}

@miniupnp
Copy link
Contributor Author

nothings/stb#1751

@slouken slouken merged commit 4e165ac into libsdl-org:main Feb 25, 2025
5 checks passed
@slouken
Copy link
Collaborator

slouken commented Feb 25, 2025

Merged, thanks!

@flowCRANE
Copy link

@miniupnp: yeah, I'm in Free Pascal's camp and have no thought of betraying the colors. ;)

The main idea is that the data stream can be constructed in such a way that multiple PNG images can be written one after the other and that they can be loaded one after the other, like in the example above. This will mean that when the image is read, the pointer is in the correct place.

I haven't done extensive testing, so I don't know if the same problem exists for other formats. But I'll look into it and let you know if I find anything. Thanks again for the fix!

@sezero
Copy link
Contributor

sezero commented Feb 25, 2025

Merged, thanks!

Add to SDL2 branch too.

And also to SDL3 main repo?

@slouken
Copy link
Collaborator

slouken commented Feb 25, 2025

Yep, done

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.

IMG_LoadTexture_RW read too much data, far beyond the end of the PNG data
4 participants