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

Bug: Invalid Pointer Dereference for 3D Arrays; for 2D arrays Pointer Dereference is ok #78

Open
Yeaseen opened this issue Aug 31, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@Yeaseen
Copy link
Contributor

Yeaseen commented Aug 31, 2024

Giving CxGo a hard time to explore this tool.

Source C code:

#include <stdio.h>
int main() {
    int array3D[3][3][3];
    int (*pArray3D)[3][3] = array3D;
    int i, j, k;
    for ( i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            for (k = 0; k < 3; k++) {
                *(*(*(pArray3D + i) + j) + k) = 5;
            }
        }
    }
    printf("Test: %.d\n", pArray3D[0][0][0]);
    return 0;
}

Translated Go Code

package main

import (
	"github.com/gotranspile/cxgo/runtime/stdio"
	"os"
	"unsafe"
)

func main() {
	var (
		array3D  [3][3][3]int
		pArray3D *[3][3]int = (*[3][3]int)(unsafe.Pointer(&array3D[0][0][0]))
		i        int
		j        int
		k        int
	)
	for i = 0; i < 3; i++ {
		for j = 0; j < 3; j++ {
			for k = 0; k < 3; k++ {
				*unsafe.Pointer(uintptr((*((*[3][3]int)(unsafe.Add(unsafe.Pointer(pArray3D), unsafe.Sizeof([3][3]int{})*uintptr(i)))))[j][0] + k)) = 5
			}
		}
	}
	stdio.Printf("Test: %.d\n", (*(*[3][3]int)(unsafe.Add(unsafe.Pointer(pArray3D), unsafe.Sizeof([3][3]int{})*0)))[0][0])
	os.Exit(0)
}

Go build Failure

./runner.go:20:6: invalid operation: cannot indirect unsafe.Pointer(uintptr((*((*[3][3]int)(unsafe.Add(unsafe.Pointer(pArray3D), unsafe.Sizeof([3][3]int{}) * uintptr(i)))))[j][0] + k)) (value of type unsafe.Pointer)

Root Cause:

Invalid Pointer dereferencing for 3D arrays. But, for 2D array version of the source C code, CxGo pdouces correct Go code.

Modification that worked

package main

import (
	"github.com/gotranspile/cxgo/runtime/stdio"
	"os"
	"unsafe"
)

func main() {
	var (
		array3D  [3][3][3]int
		pArray3D *[3][3]int = (*[3][3]int)(unsafe.Pointer(&array3D[0][0][0]))
		i        int
		j        int
		k        int
	)
	for i = 0; i < 3; i++ {
		for j = 0; j < 3; j++ {
			for k = 0; k < 3; k++ {
                               //derefencing has been modified here
				(*(*[3][3]int)(unsafe.Add(unsafe.Pointer(pArray3D), unsafe.Sizeof([3][3]int{})*uintptr(i))))[j][k] = 5
			}
		}
	}
	stdio.Printf("Test: %.d\n", (*(*[3][3]int)(unsafe.Add(unsafe.Pointer(pArray3D), unsafe.Sizeof([3][3]int{})*0)))[0][0])
	os.Exit(0)
}
@dennwc dennwc self-assigned this Aug 31, 2024
@dennwc dennwc added the bug Something isn't working label Aug 31, 2024
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