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 #3950: Div by 0 during DXT4 DDS load #3959

Merged
merged 4 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/dds.imageio/ddsinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ GetBaseType(Compression cmp)
static int
GetChannelCount(Compression cmp, bool isNormal)
{
if (isNormal)
return 3;
if (cmp == Compression::DXT5)
return isNormal ? 3 : 4;
if (cmp == Compression::BC5)
return isNormal ? 3 : 2;
if (cmp == Compression::BC4)
return 1;
if (cmp == Compression::BC5)
return 2;
if (cmp == Compression::BC6HU || cmp == Compression::BC6HS)
return 3;
return 4;
Expand Down Expand Up @@ -788,13 +788,15 @@ DDSInput::internal_readimg(unsigned char* dst, int w, int h, int d)
int k;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
k = (y * w + x) * 4;
dst[k + 0] = (unsigned char)((int)dst[k + 0] * 255
/ (int)dst[k + 3]);
dst[k + 1] = (unsigned char)((int)dst[k + 1] * 255
/ (int)dst[k + 3]);
dst[k + 2] = (unsigned char)((int)dst[k + 2] * 255
/ (int)dst[k + 3]);
k = (y * w + x) * 4;
if (dst[k + 3]) {
dst[k + 0] = (unsigned char)((int)dst[k + 0] * 255
/ (int)dst[k + 3]);
dst[k + 1] = (unsigned char)((int)dst[k + 1] * 255
/ (int)dst[k + 3]);
dst[k + 2] = (unsigned char)((int)dst[k + 2] * 255
/ (int)dst[k + 3]);
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions testsuite/dds/ref/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,9 @@ src/crash-1634.dds : 16 x 8, 1 channel, uint8 dds
oiiotool ERROR: read : "src/crash-1635.dds": Unsupported DDS bit depth: 1048576 (maybe it's a corrupted file?)
Full command line was:
> oiiotool --info -v -a --hash src/crash-1635.dds
Reading src/crash-3950.dds
src/crash-3950.dds : 16 x 1, 4 channel, uint8 dds
SHA-1: EAAFC1B407635293A2F156CCBA32225D37469178
channel list: R, G, B, A
compression: "DXT4"
textureformat: "Plain Texture"
1 change: 1 addition & 0 deletions testsuite/dds/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@
# Test more corrupted files or those that used to crash
command += info_command ("src/crash-1634.dds", hash=True)
command += info_command ("src/crash-1635.dds", hash=True)
command += info_command ("src/crash-3950.dds", hash=True)
Binary file added testsuite/dds/src/crash-3950.dds
Binary file not shown.
Loading