Skip to content

Commit fb8897e

Browse files
committed
fix keyvalues encode crash
Accomplished in exchange for my sanity, and a peek at Valve's work. Effectively, the bug is that m_pSub can be null, so when it does `dat->m_pSub->WriteAsBinary( buffer );`, it would be running WriteAsBinary with `this` set to null. So, if we detect a null m_pSub, we write a empty keyvalues structure manually by writing the ending marker directly. The previous "fix" for this accidentally did exactly this, just in a more roundabout way. Valve's solution seems to go about this fix by checking if `this` is null, at least in the decompiled code, but that method didn't work for me (presumably due to compiler optimizations).
1 parent d99a2a0 commit fb8897e

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/tier1/KeyValues.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -2607,7 +2607,15 @@ bool KeyValues::WriteAsBinary( CUtlBuffer &buffer )
26072607
{
26082608
case TYPE_NONE:
26092609
{
2610-
dat->m_pSub->WriteAsBinary( buffer );
2610+
if (dat->m_pSub)
2611+
{
2612+
dat->m_pSub->WriteAsBinary( buffer );
2613+
}
2614+
else {
2615+
// There was no m_pSub, create a fake empty KeyValues manually.
2616+
// write tail, marks end of peers
2617+
buffer.PutUnsignedChar( TYPE_NUMTYPES );
2618+
}
26112619
break;
26122620
}
26132621
case TYPE_STRING:

0 commit comments

Comments
 (0)