Skip to content

Simplify WBMP imagecreatefromstring() detection #16782

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

Merged
merged 1 commit into from
Nov 14, 2024

Conversation

cmb69
Copy link
Member

@cmb69 cmb69 commented Nov 13, 2024

According to the WBMP specification[1], the first field (type) of a WBMP is a multi-byte integer, but only type 0 is supported. Thus there is no need to read a multi-byte integer. The second field (fix header) is a single byte; reading a multi-byte integer is not really wrong, since the fix header field is laid out in a way which allows it to be treated as such, but the check whether the MBI is greater than or equal to zero is pretty useless, because negative values could only be returned if overflow occurs (MBIs are unsigned).

So the only useful assumption we can make is that the first byte is zero; we let gdImageCreateFromWBMPCtx() figure out the rest.

[1] https://www.wapforum.org/what/technical/SPEC-WAESpec-19990524.pdf section 6


At first I wanted to use src.wbmp for the test but it failed due to

php-src/ext/gd/gd.c

Lines 1386 to 1389 in b948332

if (ZSTR_LEN(data) < 12) {
/* Handle this the same way as an unknown image type. */
return -1;
}

src.wbmp is only 9 bytes. We could fix this, but I don't think it's worth it, because such small WBMPs likely don't exist in practice (and even larger WBMPs likely are no longer relevant).

According to the WBMP specification[1], the first field (type) of a
WBMP is a multi-byte integer, but only type `0` is supported.  Thus
there is no need to read a multi-byte integer.  The second field (fix
header) is a single byte; reading a multi-byte integer is not really
wrong, since the fix header field is laid out in a way which allows it
to be treated as such, but the check whether the MBI is greater than
or equal to zero is pretty useless, because negative values could only
be returned if overflow occurs (MBIs are unsigned).

So the only useful assumption we can make is that the first byte is
zero; we let `gdImageCreateFromWBMPCtx()` figure out the rest.

[1] <https://www.wapforum.org/what/technical/SPEC-WAESpec-19990524.pdf> section 6
Copy link
Member

@devnexen devnexen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess good riddance ? LGTM

@cmb69 cmb69 merged commit 2d1c382 into php:master Nov 14, 2024
10 checks passed
@cmb69 cmb69 deleted the cmb/imagecreatefromstring-wbmp branch November 14, 2024 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants