Skip to content

Commit

Permalink
small readability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
vlabo committed Nov 10, 2022
1 parent ce4bb24 commit 4d91978
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions pm_kext/col/packet_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef struct PacketCacheItem {
typedef struct {
PacketCacheItem *packets;
UINT32 maxSize;
UINT32 nextPacketID;
INT64 nextPacketID; // INT64 so we can easy check for overwrites
KSPIN_LOCK lock;
} PacketCache;

Expand Down Expand Up @@ -132,7 +132,7 @@ uint32_t packetCacheRegister(PacketCache* packetCache, PortmasterPacketInfo *pac
KLOCK_QUEUE_HANDLE lockHandle = {0};
KeAcquireInStackQueuedSpinLock(&packetCache->lock, &lockHandle);

UINT32 packetIndex = getIndexFromPacketID(packetCache, packetCache->nextPacketID);
UINT32 packetIndex = getIndexFromPacketID(packetCache, (UINT32)packetCache->nextPacketID);
PacketCacheItem *newItem = &packetCache->packets[packetIndex];

if(newItem->packetInfo != NULL && newItem->packet != NULL) {
Expand All @@ -141,7 +141,7 @@ uint32_t packetCacheRegister(PacketCache* packetCache, PortmasterPacketInfo *pac
memset(newItem, 0, sizeof(PacketCacheItem));
}

newItem->packetID = packetCache->nextPacketID;
newItem->packetID = (UINT32)packetCache->nextPacketID;
newItem->packetInfo = packetInfo;
newItem->packet = packet;
newItem->packetLength = packetLength;
Expand Down Expand Up @@ -195,15 +195,15 @@ int packetCacheRetrieve(PacketCache *packetCache, UINT32 packetID, PortmasterPac
int rc = 0;
KLOCK_QUEUE_HANDLE lockHandle = {0};
KeAcquireInStackQueuedSpinLock(&packetCache->lock, &lockHandle);
PacketCacheItem *item = getPacketFromID(packetCache, packetID);

// Check if entry was already overwritten
bool cacheFilledMoreThenOnce = (packetCache->nextPacketID - 1) > packetCache->maxSize;
if(cacheFilledMoreThenOnce && packetID <= (packetCache->nextPacketID - packetCache->maxSize - 1)) {
// Check if entry was overwritten
if((INT64)packetID <= (packetCache->nextPacketID - (INT64)packetCache->maxSize - 1)) {
DEBUG("Requested packet was overwritten: %d", packetID);
rc = 1;
}

PacketCacheItem *item = getPacketFromID(packetCache, packetID);

if(rc == 0) {
if(item != NULL) {
*packetInfo = item->packetInfo;
Expand Down Expand Up @@ -234,9 +234,8 @@ int packetCacheGet(PacketCache *packetCache, uint32_t packetID, void **packet, s
KLOCK_QUEUE_HANDLE lockHandle = {0};
KeAcquireInStackQueuedSpinLock(&packetCache->lock, &lockHandle);

// Check if entry was already overwritten
bool cacheFilledMoreThenOnce = (packetCache->nextPacketID - 1) > packetCache->maxSize;
if(cacheFilledMoreThenOnce && packetID <= (packetCache->nextPacketID - packetCache->maxSize - 1)) {
// Check if entry was overwritten
if((INT64)packetID <= (packetCache->nextPacketID - (INT64)packetCache->maxSize - 1)) {
DEBUG("Requested packet was overwritten: %d", packetID);
rc = 1;
}
Expand Down

0 comments on commit 4d91978

Please sign in to comment.