Skip to content

Commit

Permalink
Code cleanup: unused vars, unreachable code, implicit casts, signedne…
Browse files Browse the repository at this point in the history
…ss mismatches.
  • Loading branch information
bonsaiviking committed Jun 1, 2021
1 parent c79cb23 commit f82685f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 53 deletions.
5 changes: 3 additions & 2 deletions packetWin7/npf/npf/Loopback.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ NPF_TapLoopback(
NET_BUFFER_CURRENT_MDL_OFFSET(pFakeNetBuffer) = Offset - numBytes;
NET_BUFFER_DATA_LENGTH(pFakeNetBuffer) = numBytes + NET_BUFFER_DATA_LENGTH(pNetBuffer);
// We didn't allocate a MDL, so make sure we don't free it.
(PMDL)(NET_BUFFER_PROTOCOL_RESERVED(pFakeNetBuffer)[0]) = NULL;
NET_BUFFER_PROTOCOL_RESERVED(pFakeNetBuffer)[0] = NULL;
}
else {
if (Offset > 0) {
Expand Down Expand Up @@ -286,7 +286,7 @@ NPF_TapLoopback(
NET_BUFFER_CURRENT_MDL(pFakeNetBuffer) = pMdl;
NET_BUFFER_CURRENT_MDL_OFFSET(pFakeNetBuffer) = 0;
// We use the ProtocolReserved field to indicate that the MDL needs to be freed.
(PMDL)(NET_BUFFER_PROTOCOL_RESERVED(pFakeNetBuffer)[0]) = pMdl;
NET_BUFFER_PROTOCOL_RESERVED(pFakeNetBuffer)[0] = pMdl;
}
/* Move down the chain! */
pNetBuffer = pNetBuffer->Next;
Expand Down Expand Up @@ -361,6 +361,7 @@ BOOL NPF_ShouldProcess(
_Out_ PBOOLEAN pbIPv4
)
{
UNREFERENCED_PARAMETER(inMetaValues);
UINT32 layerFlags = 0;

// Get the packet protocol (IPv4 or IPv6)
Expand Down
15 changes: 8 additions & 7 deletions packetWin7/npf/npf/Openclos.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ VOID NPF_ReturnNBCopies(PNPF_NB_COPIES pNBCopy, PDEVICE_EXTENSION pDevExt)
PBUFCHAIN_ELEM pDeleteMe = NULL;
// FirstElem is not separately allocated
PBUFCHAIN_ELEM pElem = pNBCopy->FirstElem.Next;
ULONG refcount = InterlockedDecrement(&pNBCopy->refcount);
ULONG refcount = NpfInterlockedDecrement(&(LONG)pNBCopy->refcount);

if (refcount == 0)
{
Expand All @@ -446,7 +446,7 @@ _Use_decl_annotations_
VOID NPF_ReturnNBLCopy(PNPF_NBL_COPY pNBLCopy, PDEVICE_EXTENSION pDevExt)
{
PUCHAR pDot11RadiotapHeader = pNBLCopy->Dot11RadiotapHeader;
ULONG refcount = InterlockedDecrement(&pNBLCopy->refcount);
ULONG refcount = NpfInterlockedDecrement(&(LONG)pNBLCopy->refcount);
if (refcount == 0)
{
ExFreeToLookasideListEx(&pDevExt->NBLCopyPool, pNBLCopy);
Expand Down Expand Up @@ -785,7 +785,7 @@ NPF_StartUsingOpenInstance(
if (pOpen->pFiltMod->Loopback)
{
// Keep track of how many active loopback captures there are
NpfInterlockedIncrement(&g_NumLoopbackInstances);
NpfInterlockedIncrement(&(LONG)g_NumLoopbackInstances);
}
#endif

Expand Down Expand Up @@ -869,7 +869,7 @@ NPF_DecrementLoopbackInstances(
_Inout_ PNPCAP_FILTER_MODULE pFiltMod)
{
NdisAcquireSpinLock(&pFiltMod->AdapterHandleLock);
if (NpfInterlockedDecrement(&g_NumLoopbackInstances) == 0)
if (NpfInterlockedDecrement(&(LONG)g_NumLoopbackInstances) == 0)
{
pFiltMod->OpsState = OpsDisabling;
NdisReleaseSpinLock(&pFiltMod->AdapterHandleLock);
Expand Down Expand Up @@ -1399,6 +1399,7 @@ NPF_CloseAdapter(
{
POPEN_INSTANCE pOpen;
PIO_STACK_LOCATION IrpSp;
UNREFERENCED_PARAMETER(DeviceObject);
TRACE_ENTER();

IrpSp = IoGetCurrentIrpStackLocation(Irp);
Expand Down Expand Up @@ -1440,7 +1441,7 @@ NPF_Cleanup(
POPEN_INSTANCE Open;
NDIS_STATUS Status;
PIO_STACK_LOCATION IrpSp;

UNREFERENCED_PARAMETER(DeviceObject);
TRACE_ENTER();

IrpSp = IoGetCurrentIrpStackLocation(Irp);
Expand Down Expand Up @@ -1843,7 +1844,6 @@ NPF_GetFilterModuleByAdapterName(
BOOLEAN Dot11 = FALSE;
BOOLEAN Found = FALSE;
NDIS_STRING BaseName = {0};
WCHAR *pName = NULL;
TRACE_ENTER();

if (pAdapterName->Buffer == NULL || pAdapterName->Length == 0) {
Expand Down Expand Up @@ -2069,6 +2069,7 @@ NPF_CreateFilterModule(
PNPCAP_FILTER_MODULE pFiltMod;
NET_BUFFER_LIST_POOL_PARAMETERS PoolParameters;
BOOLEAN bAllocFailed = FALSE;
UNREFERENCED_PARAMETER(SelectedIndex);

// allocate some memory for the filter module structure
pFiltMod = ExAllocatePoolWithTag(NonPagedPool, sizeof(NPCAP_FILTER_MODULE), NPF_FILTMOD_TAG);
Expand Down Expand Up @@ -2203,6 +2204,7 @@ Return Value:
--*/
{
UNREFERENCED_PARAMETER(NdisFilterHandle);
TRACE_ENTER();

if (!NT_VERIFY(FilterDriverContext == (NDIS_HANDLE)FilterDriverObject))
Expand Down Expand Up @@ -2431,7 +2433,6 @@ NPF_Pause(
PNPCAP_FILTER_MODULE pFiltMod = (PNPCAP_FILTER_MODULE)FilterModuleContext;
NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
NDIS_EVENT Event;
BOOLEAN PendingWrites = FALSE;

UNREFERENCED_PARAMETER(PauseParameters);
TRACE_ENTER();
Expand Down
42 changes: 17 additions & 25 deletions packetWin7/npf/npf/Packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ NDIS_HANDLE FilterDriverHandle_WiFi = NULL; // NDIS handle for WiFi fil
NDIS_HANDLE FilterDriverObject; // Driver object for filter driver
extern HANDLE g_WFPEngineHandle;

PQUERYSYSTEMTIME g_ptrQuerySystemTime = NULL;

#ifdef KeQuerySystemTime
// On Win x64, KeQuerySystemTime is defined as a macro,
// this function wraps the macro execution.
Expand All @@ -158,6 +156,9 @@ KeQuerySystemTimeWrapper(
{
KeQuerySystemTime(CurrentTime);
}
PQUERYSYSTEMTIME g_ptrQuerySystemTime = &KeQuerySystemTimeWrapper;
#else
PQUERYSYSTEMTIME g_ptrQuerySystemTime = &KeQuerySystemTime;
#endif

#ifdef NPCAP_READ_ONLY
Expand All @@ -172,6 +173,7 @@ NTSTATUS NPF_Deny(
IN PIRP Irp
)
{
UNREFERENCED_PARAMETER(DeviceObject);
TRACE_ENTER();
Irp->IoStatus.Information = 0;
Irp->IoStatus.Status = STATUS_UNSUCCESSFUL;
Expand Down Expand Up @@ -228,7 +230,7 @@ NPF_GetRegistryOption_AdapterName(
_Inout_ PNDIS_STRING pOutputString
)
{
size_t i=0, j=0;
USHORT i=0, j=0;
NPF_GetRegistryOption_String(pRegistryPath, pRegValueName, pOutputString);
if (pOutputString->Buffer == NULL)
{
Expand Down Expand Up @@ -291,7 +293,7 @@ DriverEntry(
FilterDriverObject = DriverObject;

RtlInitUnicodeString(&parametersPath, NULL);
parametersPath.MaximumLength=RegistryPath->Length+wcslen(L"\\Parameters")*sizeof(WCHAR)+sizeof(UNICODE_NULL);
parametersPath.MaximumLength=RegistryPath->Length+sizeof(L"\\Parameters");
parametersPath.Buffer=ExAllocatePoolWithTag(PagedPool, parametersPath.MaximumLength, NPF_UNICODE_BUFFER_TAG);
if (!parametersPath.Buffer) {
return STATUS_INSUFFICIENT_RESOURCES;
Expand Down Expand Up @@ -947,8 +949,6 @@ Return Value:
NDIS_STRING SymLink;
NDIS_EVENT Event;
LOCK_STATE_EX lockState;
PSINGLE_LIST_ENTRY Curr = NULL;
PSINGLE_LIST_ENTRY Prev = NULL;

TRACE_ENTER();

Expand Down Expand Up @@ -1127,7 +1127,6 @@ NPF_IoControl(
ULONG Information = 0;
PLIST_ENTRY CurrEntry;
PULONG pUL;
PUCHAR tpointer = NULL; //assign NULL to suppress error C4703: potentially uninitialized local pointer variable
ULONG dim, timeout;
struct bpf_insn* NewBpfProgram;
PPACKET_OID_DATA OidData;
Expand All @@ -1141,7 +1140,6 @@ NPF_IoControl(
BOOLEAN SyncWrite = FALSE;
ULONG insns;
ULONG cnt;
BOOLEAN IsExtendedFilter = FALSE;
PUINT pStats;
ULONG StatsLength;
PULONG pCombinedPacketFilter;
Expand All @@ -1154,6 +1152,7 @@ NPF_IoControl(
VOID* POINTER_32 hUserEvent32Bit;
#endif //_WIN64

UNREFERENCED_PARAMETER(DeviceObject);
TRACE_ENTER();

IrpSp = IoGetCurrentIrpStackLocation(Irp);
Expand Down Expand Up @@ -1283,7 +1282,7 @@ NPF_IoControl(
NdisReleaseSpinLock(&Open->WriteLock);

WriteRes = NPF_BufferedWrite(Irp,
(PUCHAR) Irp->AssociatedIrp.SystemBuffer,
(PCHAR) Irp->AssociatedIrp.SystemBuffer,
IrpSp->Parameters.DeviceIoControl.InputBufferLength,
SyncWrite);

Expand Down Expand Up @@ -1394,20 +1393,16 @@ NPF_IoControl(

mode = *((PULONG)Irp->AssociatedIrp.SystemBuffer);

///////kernel dump does not work at the moment//////////////////////////////////////////
if (mode & MODE_DUMP)
// Verify no unsupported mode is set
if (mode == 0 || mode & ~(MODE_CAPT | MODE_MON | MODE_STAT | MODE_DUMP))
{
SET_FAILURE(STATUS_INVALID_DEVICE_REQUEST);
break;
}
///////kernel dump does not work at the moment//////////////////////////////////////////

if (mode == MODE_CAPT)
else if (mode == MODE_CAPT)
{
Open->mode = MODE_CAPT;

SET_RESULT_SUCCESS(0);
break;
}
else if (mode == MODE_MON)
{
Expand All @@ -1417,8 +1412,6 @@ NPF_IoControl(
//

SET_FAILURE(STATUS_INVALID_DEVICE_REQUEST);

break;
}
else
{
Expand All @@ -1436,16 +1429,15 @@ NPF_IoControl(

if (mode & MODE_DUMP)
{
Open->mode |= MODE_DUMP;
//////kernel dump does not work at the moment//////////////////////////////////////////
SET_FAILURE(STATUS_INVALID_DEVICE_REQUEST);
break;
// Open->mode |= MODE_DUMP;
// Open->MinToCopy=(Open->BufSize<2000000)?Open->BufSize/2:1000000;
}

SET_RESULT_SUCCESS(0);
break;
}

SET_FAILURE(STATUS_INVALID_DEVICE_REQUEST);

break;

case BIOCSETDUMPFILENAME:
Expand Down Expand Up @@ -1653,7 +1645,7 @@ NPF_IoControl(
break;
}
// If there's no change, we're done!
if (dim == Open->Size) {
if ((LONG)dim == Open->Size) {
SET_RESULT_SUCCESS(0);
break;
}
Expand Down Expand Up @@ -1823,7 +1815,7 @@ NPF_IoControl(
else
{
TRACE_MESSAGE2(PACKET_DEBUG_LOUD, "Dot11: AdapterName=%ws, OID_GEN_MEDIA_IN_USE & BIOCGETOID, OidData->Data = %d", Open->pFiltMod->AdapterName.Buffer, NdisMediumRadio80211);
*((PUINT)OidData->Data) = NdisMediumRadio80211;
*((PUINT)OidData->Data) = (UINT)NdisMediumRadio80211;
OidData->Length = sizeof(UINT);
SET_RESULT_SUCCESS(sizeof(PACKET_OID_DATA) - 1 + OidData->Length);
}
Expand Down
14 changes: 7 additions & 7 deletions packetWin7/npf/npf/Read.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ NPF_Read(
LOCK_STATE_EX lockState;
NTSTATUS Status = STATUS_SUCCESS;

UNREFERENCED_PARAMETER(DeviceObject);
TRACE_ENTER();

IrpSp = IoGetCurrentIrpStackLocation(Irp);
Expand Down Expand Up @@ -452,7 +453,6 @@ NPF_DoTap(
struct timeval tstamp = {0, 0};
NBLCopiesHead.Next = NULL;
PNPF_SRC_NB pSrcNB = NULL;
PNPF_NB_COPIES pNBCopies = NULL;
PSINGLE_LIST_ENTRY pNBCopiesEntry = NULL;
PDEVICE_EXTENSION pDevExt = NULL;

Expand Down Expand Up @@ -633,8 +633,8 @@ PNPF_CAP_DATA NPF_GetCapData(

// Increment refcounts on relevant structures
pCapData->pNBCopy = pNBCopy;
InterlockedIncrement(&pNBCopy->refcount);
InterlockedIncrement(&pNBLCopy->refcount);
NpfInterlockedIncrement(&(LONG)pNBCopy->refcount);
NpfInterlockedIncrement(&(LONG)pNBLCopy->refcount);

pCapData->ulCaplen = uCapLen;

Expand Down Expand Up @@ -1253,10 +1253,10 @@ NPF_TapExForEachOpen(
}
}

NpfInterlockedExchangeAdd(&Open->ResourceDropped, resdropped);
NpfInterlockedExchangeAdd(&Open->Dropped, dropped);
NpfInterlockedExchangeAdd(&Open->Received, received);
NpfInterlockedExchangeAdd(&Open->Accepted, accepted);
NpfInterlockedExchangeAdd(&(LONG)Open->ResourceDropped, resdropped);
NpfInterlockedExchangeAdd(&(LONG)Open->Dropped, dropped);
NpfInterlockedExchangeAdd(&(LONG)Open->Received, received);
NpfInterlockedExchangeAdd(&(LONG)Open->Accepted, accepted);

NPF_StopUsingOpenInstance(Open, OpenRunning, AtDispatchLevel);
//TRACE_EXIT();
Expand Down
17 changes: 9 additions & 8 deletions packetWin7/npf/npf/Write.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ NPF_Write(
ULONG numSentPackets;
NTSTATUS Status = STATUS_SUCCESS;

UNREFERENCED_PARAMETER(DeviceObject);
TRACE_ENTER();

IrpSp = IoGetCurrentIrpStackLocation(Irp);
Expand Down Expand Up @@ -347,7 +348,7 @@ NPF_Write(

NT_ASSERT(Open->pFiltMod != NULL);

NpfInterlockedIncrement(&Open->TransmitPendingPackets);
NpfInterlockedIncrement(&(LONG)Open->TransmitPendingPackets);

NdisResetEvent(&Open->NdisWriteCompleteEvent);

Expand Down Expand Up @@ -411,7 +412,7 @@ NPF_Write(
if (!NT_SUCCESS(Status))
{
// Couldn't send this one. Don't wait for it!
NpfInterlockedDecrement(&Open->TransmitPendingPackets);
NpfInterlockedDecrement(&(LONG)Open->TransmitPendingPackets);
NPF_FreePackets(pNetBufferList);
break;
}
Expand Down Expand Up @@ -537,9 +538,9 @@ NPF_BufferedWrite(
PNET_BUFFER_LIST pNetBufferList = NULL;
ULONG SendFlags = 0;
UINT i;
LARGE_INTEGER StartTicks, CurTicks, TargetTicks;
LARGE_INTEGER StartTicks = { 0 }, CurTicks, TargetTicks;
LARGE_INTEGER TimeFreq;
struct timeval BufStartTime;
struct timeval BufStartTime = { 0 };
struct sf_pkthdr* pWinpcapHdr;
PMDL TmpMdl;
ULONG Pos = 0;
Expand Down Expand Up @@ -732,7 +733,7 @@ NPF_BufferedWrite(
NT_ASSERT(Open->pFiltMod != NULL);

// Increment the number of pending sends
NpfInterlockedIncrement(&Open->Multiple_Write_Counter);
NpfInterlockedIncrement(&(LONG)Open->Multiple_Write_Counter);

//receive the packets before sending them
NPF_DoTap(Open->pFiltMod, pNetBufferList, Open, NPF_IRQL_UNKNOWN);
Expand All @@ -750,7 +751,7 @@ NPF_BufferedWrite(
pNetBufferList);
if (!NT_SUCCESS(Status))
{
NpfInterlockedDecrement(&Open->Multiple_Write_Counter);
NpfInterlockedDecrement(&(LONG)Open->Multiple_Write_Counter);
NPF_FreePackets(pNetBufferList);
result = -Status;
break;
Expand Down Expand Up @@ -1041,7 +1042,7 @@ NPF_SendCompleteExForEachOpen(
if (FreeBufAfterWrite)
{
// Increment the number of pending sends
NpfInterlockedDecrement(&Open->Multiple_Write_Counter);
NpfInterlockedDecrement(&(LONG)Open->Multiple_Write_Counter);

NdisSetEvent(&Open->WriteEvent);

Expand All @@ -1053,7 +1054,7 @@ NPF_SendCompleteExForEachOpen(
// Packet sent by NPF_Write()
//

ULONG stillPendingPackets = NpfInterlockedDecrement(&Open->TransmitPendingPackets);
ULONG stillPendingPackets = NpfInterlockedDecrement(&(LONG)Open->TransmitPendingPackets);

//
// if the number of packets submitted to NdisSend and not acknoledged is less than half the
Expand Down
2 changes: 1 addition & 1 deletion packetWin7/npf/npf/time_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ __inline void TIME_SYNCHRONIZE(
LARGE_INTEGER TimeFreq, PTime;

// get the absolute value of the system boot time.

NT_ASSERT(g_ptrQuerySystemTime != NULL);
PTime = KeQueryPerformanceCounter(&TimeFreq);
g_ptrQuerySystemTime(&SystemTime);

Expand Down
Loading

0 comments on commit f82685f

Please sign in to comment.