Skip to content

Instantly share code, notes, and snippets.

@HumanGamer
Last active August 5, 2019 17:10
Show Gist options
  • Save HumanGamer/9e9471457b89199e06e801bf74c2120b to your computer and use it in GitHub Desktop.
Save HumanGamer/9e9471457b89199e06e801bf74c2120b to your computer and use it in GitHub Desktop.
The worst code I think I've ever seen...

Worst Code Ever

This is the worst code I've ever seen, even worse than Terraria and it's in an MMORPG called flyff.

Basically, when compiling under x64 it fails to save the game due to an invalid pointer.

How does this happen?

Sending the value to the other thread:

PostQueuedCompletionStatus(g_DbManager.m_hIOCPUpdate, (DWORD)pMover2, (DWORD)0, NULL);

Using the value on the other thread:

pMover = (CMover*)dwBytesTransferred;

Do you see the problem here? NO??

Let's check the documentation...

BOOL WINAPI PostQueuedCompletionStatus(
  _In_     HANDLE       CompletionPort,
  _In_     DWORD        dwNumberOfBytesTransferred,
  _In_     ULONG_PTR    dwCompletionKey,
  _In_opt_ LPOVERLAPPED lpOverlapped
);

Now do you see the problem?

It's using the byte count parameter (dwNumberOfBytesTransferred) for the value instead of the value parameter (dwCompletionKey)

Here is the fixed code for x64:

Sending the value to the other thread:

PostQueuedCompletionStatus(g_DbManager.m_hIOCPUpdate, sizeof(pMover2), (ULONG_PTR)pMover2, NULL);

Using the value on the other thread:

pMover = (CMover*)dwCompletionKey;

How did this kind of code end up in an MMO out of anything?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment