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

Blockmap Size Error Checking (Fixes bug #1287) #1289

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Blockmap Size Error Checking (Fixes bug #1287) #1289

wants to merge 10 commits into from

Conversation

MistUnky
Copy link

@MistUnky MistUnky commented Jun 3, 2020

This checks for a large blockmap size and produces an error instead of allowing the map to mess up memory and cause a segfault.

@@ -531,6 +531,10 @@ void P_LoadBlockMap (int lump)

lumplen = W_LumpLength(lump);
count = lumplen / 2;
if(count >= 32766)
Copy link
Member

Choose a reason for hiding this comment

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

Why exactly this number?

Copy link
Author

Choose a reason for hiding this comment

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

It's the blockmap limit for doom / 2 (count = blockmap size / 2, and blockmap limit is 65536 in doom, so I put that /2 and checked it against count.

Copy link
Author

Choose a reason for hiding this comment

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

Source: https://doomwiki.org/wiki/Blockmap#Compression
and C data type size for short (which is what blockmap is) is 65536

Copy link
Member

Choose a reason for hiding this comment

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

Thank you, but this was a rhetorical question. 😉 I'd prefer if magic numbers in source code didn't "fall from the sky". Something like USHRT_MAX/2 and an accompanying comment would probably be more meaningful.

Copy link
Member

Choose a reason for hiding this comment

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

Also, 32766 is the wrong number since 65536 / 2 = 32768. A good example of why Fabian's approach is the better one.

Copy link
Member

@fabiangreffrath fabiangreffrath left a comment

Choose a reason for hiding this comment

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

Apparently this breaks demo compatibility.

@MistUnky
Copy link
Author

MistUnky commented Jun 3, 2020

Apparently this breaks demo compatibility.

How so? Demos seem to work fine when I'm running them.
EDIT: Oh, some demos seem to take advantage of the size limit to overwrite themselves in certain custom maps. I don't think the above code breaks the demos, but it should probably be checked first.

@fabiangreffrath
Copy link
Member

Well, refusing to load a map that played back before qualifies as breakage, I guess. 😉

@MistUnky
Copy link
Author

MistUnky commented Jun 3, 2020

Is there another way to check if the blockmap will crash? It seems it should be possible to make a way so that any list starting points past the 65536th byte won't load, but everything else will, so that it doesn't break compatibility. I'm not so sure about the internals of that, so I don't know how it would be implemented in the source code.

if(count >= 32766)
{
I_Error("Blockmap too large");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Respect the code style. For example, put a space between if and (. Also, don't use tabs for indentation.

Code style: https://github.com/chocolate-doom/chocolate-doom/blob/master/HACKING.md

Copy link
Member

Choose a reason for hiding this comment

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

+1

@@ -531,6 +531,10 @@ void P_LoadBlockMap (int lump)

lumplen = W_LumpLength(lump);
count = lumplen / 2;
if(count >= 32766)
{
I_Error("Blockmap too large");
Copy link
Member

Choose a reason for hiding this comment

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

This is not a good error message. A better error message would include the two values that were compared and a link to the doomwiki.org page about the static limit.

@@ -531,6 +531,10 @@ void P_LoadBlockMap (int lump)

lumplen = W_LumpLength(lump);
count = lumplen / 2;
if(count >= 32766)
Copy link
Member

Choose a reason for hiding this comment

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

Also, 32766 is the wrong number since 65536 / 2 = 32768. A good example of why Fabian's approach is the better one.

if(count >= 32766)
{
I_Error("Blockmap too large");
}
Copy link
Member

Choose a reason for hiding this comment

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

+1

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.

None yet

4 participants