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

Fix invalid conversion between unsafe.Pointer and uintptr #6673

Merged
merged 1 commit into from
Oct 17, 2024

Commits on Oct 15, 2024

  1. Fix invalid conversion between unsafe.Pointer and uintptr

    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 <[email protected]>
    tnqn committed Oct 15, 2024
    Configuration menu
    Copy the full SHA
    9396532 View commit details
    Browse the repository at this point in the history