Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate unit test can detect wrong implementation #46

Closed
wants to merge 2 commits into from

Conversation

tnqn
Copy link
Owner

@tnqn tnqn commented Oct 15, 2024

No description provided.

tnqn added 2 commits October 15, 2024 18:22
uintptr cannot be stored in variable before conversion back to Pointer.
This is because the variable is an integer with no pointer semantics,
even if a uintptr holds the address of some object, the garbage
collector will not update that uintptr's value if the object moves, nor
will that uintptr keep the object from being reclaimed.

The issue can be detected by checkptr when running unit test with race
detector:

checkptr: pointer arithmetic result points to invalid allocation
```
pFirstRow := uintptr(unsafe.Pointer(&table.Table[0]))
row := *(*MibIPForwardRow)(unsafe.Pointer(pFirstRow + rowSize*uintptr(i)))
```

While it can be fixed by performing both conversions in the same
expression like below, using `unsafe.Slice` to get the slice is much
simpler.
```
row := *(*MibIPForwardRow)(unsafe.Pointer(uintptr(unsafe.Pointer(&table.Table[0])) + rowSize*uintptr(i)))
```

The patch also adds an unit test to validate it.

Signed-off-by: Quan Tian <quan.tian@broadcom.com>
@tnqn tnqn changed the base branch from unit-test-copy-rows to main October 15, 2024 10:28
@tnqn tnqn marked this pull request as ready for review October 15, 2024 10:29
@tnqn tnqn closed this Oct 15, 2024
@tnqn tnqn reopened this Oct 15, 2024
@tnqn tnqn closed this Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant