Konstantin8105 / c4go

Transpiling C code to Go code
MIT License
365 stars 38 forks source link

Failure at 2D Array Initialization #517

Open Yeaseen opened 2 months ago

Yeaseen commented 2 months ago

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
}
Konstantin8105 commented 2 months ago

Dear @Yeaseen ,

If you run :

go test -v -run=TestBookSources

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