Skip to content

Commit

Permalink
Handles int conversions in the networking mock
Browse files Browse the repository at this point in the history
It's too much close to the real system APIs :)
  • Loading branch information
CarlosNihelton committed Aug 26, 2024
1 parent c8a07ac commit 6ff64e1
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions windows-agent/internal/daemon/daemontestutils/networking_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package daemontestutils

import (
"errors"
"math"
"net"
"unsafe"

Expand Down Expand Up @@ -105,24 +106,29 @@ func (m *MockIPConfig) GetAdaptersAddresses(_, _ uint32, _ uintptr, adapterAddre

// fillBufferFromTemplate fills a pre-allocated buffer of ipAdapterAddresses with the data from the mockIPAddrsTemplate.
func fillBufferFromTemplate(adaptersAddresses *IPAdapterAddresses, sizePointer *uint32, mockIPAddrsTemplate []MockIPAddrsTemplate) error {
count := uint32(len(mockIPAddrsTemplate))
objSize := uint32(unsafe.Sizeof(IPAdapterAddresses{}))
count := len(mockIPAddrsTemplate)
objSize := int(unsafe.Sizeof(IPAdapterAddresses{}))
bufSizeNeeded := count * objSize
if *sizePointer < bufSizeNeeded {
if bufSizeNeeded >= math.MaxUint32 || bufSizeNeeded < 0 {
return errors.New("buffer size limit reached")
}
//nolint:gosec // Value guaranteed to fit inside uint32.
bufSz := uint32(bufSizeNeeded)
if *sizePointer < bufSz {
return ERROR_BUFFER_OVERFLOW
}

//nolint:gosec // Using unsafe to manipulate pointers mimicking the Win32 API, only used in tests.
begin := unsafe.Pointer(adaptersAddresses)
for _, addr := range mockIPAddrsTemplate {
next := unsafe.Add(begin, int(objSize)) // next = ++begin
next := unsafe.Add(begin, objSize) // next = ++begin

ptr := (*IPAdapterAddresses)(begin)
fillFromTemplate(&addr, ptr, (*IPAdapterAddresses)(next))

begin = next
}
*sizePointer = bufSizeNeeded
*sizePointer = bufSz
return nil
}

Expand Down

0 comments on commit 6ff64e1

Please sign in to comment.