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

negative pointer addition overflows uintptr #41

Closed
TotallyGamerJet opened this issue Nov 25, 2021 · 0 comments
Closed

negative pointer addition overflows uintptr #41

TotallyGamerJet opened this issue Nov 25, 2021 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@TotallyGamerJet
Copy link
Contributor

Given this C code:

int main() {
  int *ptr = 0;
  ptr -= 3;
  return 0;
}

Results in this Go Code:

package main

import (
	"os"
	"unsafe"
)

func main() {
	var ptr *int = nil
	ptr = (*int)(unsafe.Add(unsafe.Pointer(ptr), -(unsafe.Sizeof(int(0)) * 3)))
	os.Exit(0)
}

The negative number overflows uintptr from unsafe.Sizeof. Just casting it to any signed number fixes the error. Should it be a cast to int because that is usually the same size as the register like uintptr?

@dennwc dennwc self-assigned this Nov 26, 2021
@dennwc dennwc added the bug Something isn't working label Nov 26, 2021
@dennwc dennwc closed this as completed in 96a0704 Nov 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants