You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<stdio.h>unionData {
intp_p;
};
unionDatadata1= { 100 };
intmain() {
printf("Value at the start of main: %d\n", data1.p_p);
data1.p_p=200;
printf("Value after modification: %d\n", data1.p_p);
return0;
}
C code's output
Value at the start of main: 100 Value after modification: 200
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"import"unsafe"typeDatastruct{ memory unsafe.Pointer }
func (unionVar*Data) copy() Data {
varbuffer [8]bytefori:=range// Data - transpiled function from /mnt/bigdata/YEASEEN/FuzzTranspilers-main/fuzzers/TFuzz/pre_post_works/c4go_transpile/runner.c:2buffer {
buffer[i] = (*((*[8]byte)(unionVar.memory)))[i]
}
varnewUnionDatanewUnion.memory=unsafe.Pointer(&buffer)
returnnewUnion
}
func (unionVar*Data) p_p() *int32 {
ifunionVar.memory==nil {
varbuffer [8]byteunionVar.memory=unsafe.Pointer(&buffer)
}
return (*int32)(unionVar.memory)
}
// data1 - transpiled function from /mnt/bigdata/YEASEEN/FuzzTranspilers-main/fuzzers/TFuzz/pre_post_works/c4go_transpile/runner.c:5vardata1Data=Data{100}
// main - transpiled function from /mnt/bigdata/YEASEEN/FuzzTranspilers-main/fuzzers/TFuzz/pre_post_works/c4go_transpile/runner.c:6funcmain() {
noarch.Printf([]byte("Value at the start of main: %d\n\x00"), (*data1.p_p()))
(*data1.p_p()) =200noarch.Printf([]byte("Value after modification: %d\n\x00"), (*data1.p_p()))
return
}
Go compiler build error after the translated code is built
# command-line-arguments
./runner.go:34:23: cannot use 100 (untyped int constant) as unsafe.Pointer value in struct literal
Root Cause
The initialization of variable data1 is of type struct Data.
First of all, this global initialization should be inside
Modified Go code that works
I fixed the go code manually, and the fix works.
//// 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"import"unsafe"typeDatastruct{ memory unsafe.Pointer }
func (unionVar*Data) copy() Data {
varbuffer [8]bytefori:=range// Data - transpiled function from /mnt/bigdata/YEASEEN/FuzzTranspilers-main/fuzzers/TFuzz/pre_post_works/c4go_transpile/runner.c:2buffer {
buffer[i] = (*((*[8]byte)(unionVar.memory)))[i]
}
varnewUnionDatanewUnion.memory=unsafe.Pointer(&buffer)
returnnewUnion
}
func (unionVar*Data) p_p() *int32 {
ifunionVar.memory==nil {
varbuffer [8]byteunionVar.memory=unsafe.Pointer(&buffer)
}
return (*int32)(unionVar.memory)
}
// data1 - transpiled function from /mnt/bigdata/YEASEEN/FuzzTranspilers-main/fuzzers/TFuzz/pre_post_works/c4go_transpile/runner.c:5// Declare a global variablevardata1Data// Initialize the global variable in the init functionfuncinit() {
*data1.p_p() =100
}
// main - transpiled function from /mnt/bigdata/YEASEEN/FuzzTranspilers-main/fuzzers/TFuzz/pre_post_works/c4go_transpile/runner.c:6funcmain() {
noarch.Printf([]byte("Value at the start of main: %d\n\x00"), (*data1.p_p()))
(*data1.p_p()) =200noarch.Printf([]byte("Value after modification: %d\n\x00"), (*data1.p_p()))
return
}
So, instead of var data1 Data = Data{100}, my modification
// Declare a global variablevardata1Data// Initialize the global variable in the init functionfuncinit() {
*data1.p_p() =100
}
Output after modification: Value at the start of main: 100 Value after modification: 200
The text was updated successfully, but these errors were encountered:
Thank you for your clean test case.
If you want to implement, then feel free for create PR.
If you do not want prepare PR, then I will implement by myself.
Source C code
C code's output
Value at the start of main: 100
Value after modification: 200
Output Go code
Go compiler build error after the translated code is built
Root Cause
The initialization of variable
data1
is of type structData
.First of all, this global initialization should be inside
Modified Go code that works
I fixed the go code manually, and the fix works.
So, instead of
var data1 Data = Data{100}
, my modificationOutput after modification:
Value at the start of main: 100
Value after modification: 200
The text was updated successfully, but these errors were encountered: