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

Failure at 2D Array Initialization #517

Open
Yeaseen opened this issue Aug 31, 2024 · 1 comment
Open

Failure at 2D Array Initialization #517

Yeaseen opened this issue Aug 31, 2024 · 1 comment

Comments

@Yeaseen
Copy link

Yeaseen commented Aug 31, 2024

Sorry to give c4go a hard time. I really care about the issues that are unexplored in c4go. Maybe I will explore this tool.

Source C Code:

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

Output Go code:

//
//	Package - transpiled by c4go
//
//	If you have found any issues, please raise an issue at:
//	https://github.com/Konstantin8105/c4go/
//

package main

import "github.com/Konstantin8105/c4go/noarch"

// main - transpiled function from  /mnt/bigdata/YEASEEN/FuzzTranspilers-main/fuzzers/TFuzz/pre_post_works/c4go_transpile/runner.c:2
func main() {
	var array2D [][]int32 = make([][]int32, 3)
	var pArray2D [][]int32 = array2D
	var i int32
	var j int32
	for i = 0; i < 3; i++ {
		for j = 0; j < 3; j++ {
			(pArray2D[0+i])[0+j] = 5
		}
	}
	noarch.Printf([]byte("Test: %.d\n\x00"), pArray2D[0][0])
	return
}

Go build Successful

Runtime Error

panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
main.main()
        /mnt/bigdata/YEASEEN/FuzzTranspilers-main/fuzzers/TFuzz/pre_post_works/c4go_transpile/runner.go:20 +0xfe

Root cause:

2D array initialization. The inner slices are still 'nil'

What modification worked:

//
//	Package - transpiled by c4go
//
//	If you have found any issues, please raise an issue at:
//	https://github.com/Konstantin8105/c4go/
//

package main

import "github.com/Konstantin8105/c4go/noarch"

// main - transpiled function from  /mnt/bigdata/YEASEEN/FuzzTranspilers-main/fuzzers/TFuzz/pre_post_works/c4go_transpile/runner.c:2
func main() {


	// Create a 2x2 array with initialized slices
	var array2D [][]int32 = make([][]int32, 3)
	for i := range array2D {
		array2D[i] = make([]int32, 3)
	}

	// Assign the pointer to the array
	var pArray2D [][]int32 = array2D

	var i int32
	var j int32
	for i = 0; i < 3; i++ {
		for j = 0; j < 3; j++ {
			(pArray2D[0+i])[0+j] = 5
		}
	}
	noarch.Printf([]byte("Test: %.d\n\x00"), pArray2D[0][0])
	return
}
@Yeaseen Yeaseen changed the title Failure at Pointer Arithmetic with 2D Arrays in C Failure at 2D Array Initialization Aug 31, 2024
@Konstantin8105
Copy link
Owner

Dear @Yeaseen ,

If you run :

go test -v -run=TestBookSources

then you will see a lot not-implemented function. C4GO is not perfect.

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

No branches or pull requests

2 participants