sourcehook/sourcehook.cpp
changeset 86 0c0c0ab2e9ba
parent 83 b41900c40742
child 87 4d1f04a1aee1
     1.1 --- a/sourcehook/sourcehook.cpp	Thu Jul 07 13:09:57 2005 +0000
     1.2 +++ b/sourcehook/sourcehook.cpp	Thu Jul 07 18:21:32 2005 +0000
     1.3 @@ -5,6 +5,7 @@
     1.4  * License: zlib/libpng
     1.5  *
     1.6  * Author(s): Pavol "PM OnoTo" Marko
     1.7 +* Contributors: Scott "Damaged Soul" Ehlert
     1.8  * ============================
     1.9  */
    1.10  
    1.11 @@ -299,6 +300,13 @@
    1.12  		if (hookman == m_HookMans.end())
    1.13  			return false;
    1.14  
    1.15 +		if (IsBadReadPtr(reinterpret_cast<char*>(adjustediface) + tmp.vtbl_offs, sizeof(char)))
    1.16 +		{
    1.17 +			hookman->vfnptrs.clear();
    1.18 +			hookman->func(HA_Unregister, NULL);
    1.19 +			return true;
    1.20 +		}
    1.21 +
    1.22  		void **cur_vtptr = *reinterpret_cast<void***>(
    1.23  			reinterpret_cast<char*>(adjustediface) + tmp.vtbl_offs);
    1.24  		void *cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.vtbl_idx);
    1.25 @@ -465,6 +473,38 @@
    1.26  		}
    1.27  	}
    1.28  
    1.29 +#if __linux__
    1.30 +	// Windows has an implentation for this already, but Linux does not :(
    1.31 +	static bool CSourceHookImpl::IsBadReadPtr(const void *ptr, size_t len)
    1.32 +	{
    1.33 +		void(*prevHandler)(int sig);
    1.34 +		g_BadReadCalled = true;
    1.35 +
    1.36 +		if (setjmp(g_BadReadJmpBuffer))
    1.37 +			return true;
    1.38 +
    1.39 +		prevHandler = signal(SIGSEGV, BadReadHandler);
    1.40 +
    1.41 +		volatile const char *p = reinterpret_cast<const char*>(ptr);
    1.42 +		char dummy;
    1.43 +
    1.44 +		for (size_t i = 0; i < len; i++)
    1.45 +			dummy = p[i];
    1.46 +
    1.47 +		g_BadReadCalled = false;
    1.48 +
    1.49 +		signal(SIGSEGV, prevHandler);
    1.50 +
    1.51 +		return false;
    1.52 +	}
    1.53 +
    1.54 +	static void CSourceHookImpl::BadReadHandler(int sig)
    1.55 +	{
    1.56 +		if (m_BadReadCalled)
    1.57 +			longjmp(m_BadReadJmpBuf, 1);
    1.58 +	}
    1.59 +#endif
    1.60 +
    1.61  	CSourceHookImpl::HookManInfoList::iterator CSourceHookImpl::FindHookMan(HookManInfoList::iterator begin,
    1.62  		HookManInfoList::iterator end, const char *proto, int vtblofs, int vtblidx)
    1.63  	{