basil00/Divert — pull request #33 by tpmorais (2014-12-12 18:40:47+00:00)
Title:  - Correcting invalid TLS index when calling WinDivertIoControl.

Testing my application with application verifier, I found a TLS problem calling WinDivertIoControl function.

Error description: "This stop is generated if the function on the top of the stack passed an invalid TLS index to TLS system routines. The typical issue here is to assume a certain value for a TLS index instead of calling TlsAlloc. This can happen by assuming you always get TLS index value N, therefore there is no need to call TlsAlloc, or it may be due to an un-initialized variable."

In other words, the index was invalid because TlsAlloc was not called before the use of TlsGetValue or TlsSetValue.
So, I solved the problem calling TlsAlloc in the begin of the function and TlsFree in the end, because each thread receives its own slot for the index.