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>typedefstruct{
intintM;
} mystruct;
mystructtest_bool(intinp){
return (mystruct){.intM=inp!=0};
}
intmain(){
intb=5;
mystructresult=test_bool(b);
printf("The value of 'a' in the returned struct is: %d\n", result.intM);
b=0;
result=test_bool(b);
printf("The value of 'a' in the returned struct when b = 0 is: %d\n", result.intM);
return0;
}
C code's output
Translated Go code by c4go
//// 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"// _struct_at_mnt_bigdata_YEASEEN_FuzzTranspilers_main_fuzzers_TFuzz_pre_post_works_c4go_transpile_runner_c_2 - transpiled function from /mnt/bigdata/YEASEEN/FuzzTranspilers-main/fuzzers/TFuzz/pre_post_works/c4go_transpile/runner.c:2type_struct_at_mnt_bigdata_YEASEEN_FuzzTranspilers_main_fuzzers_TFuzz_pre_post_works_c4go_transpile_runner_c_2struct {
intMint32
}
// mystruct - transpiled function from /mnt/bigdata/YEASEEN/FuzzTranspilers-main/fuzzers/TFuzz/pre_post_works/c4go_transpile/runner.c:2typemystruct=_struct_at_mnt_bigdata_YEASEEN_FuzzTranspilers_main_fuzzers_TFuzz_pre_post_works_c4go_transpile_runner_c_2// test_bool - transpiled function from /mnt/bigdata/YEASEEN/FuzzTranspilers-main/fuzzers/TFuzz/pre_post_works/c4go_transpile/runner.c:5functest_bool(inpint32) mystruct {
returnmystruct{inp!=0}
}
// main - transpiled function from /mnt/bigdata/YEASEEN/FuzzTranspilers-main/fuzzers/TFuzz/pre_post_works/c4go_transpile/runner.c:8funcmain() {
varbint32=5varresultmystruct=test_bool(b)
noarch.Printf([]byte("The value of 'a' in the returned struct is: %d\n\x00"), result.intM)
b=0result=test_bool(b)
noarch.Printf([]byte("The value of 'a' in the returned struct when b = 0 is: %d\n\x00"), result.intM)
return
}
Go compiler build error
Root Cause
c4go fails to convert boolean expression to Integer in Struct initialization
Modified Go code that works
I modified the initialization so that it could use noarch.BoolToInt function. And it gives the outputs as the source C code does.
Instead of return mystruct{inp != 0}, my modification is return mystruct{noarch.BoolToInt(inp != 0)}
//// 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"// _struct_at_mnt_bigdata_YEASEEN_FuzzTranspilers_main_fuzzers_TFuzz_pre_post_works_c4go_transpile_runner_c_2 - transpiled function from /mnt/bigdata/YEASEEN/FuzzTranspilers-main/fuzzers/TFuzz/pre_post_works/c4go_transpile/runner.c:2type_struct_at_mnt_bigdata_YEASEEN_FuzzTranspilers_main_fuzzers_TFuzz_pre_post_works_c4go_transpile_runner_c_2struct {
intMint32
}
// mystruct - transpiled function from /mnt/bigdata/YEASEEN/FuzzTranspilers-main/fuzzers/TFuzz/pre_post_works/c4go_transpile/runner.c:2typemystruct=_struct_at_mnt_bigdata_YEASEEN_FuzzTranspilers_main_fuzzers_TFuzz_pre_post_works_c4go_transpile_runner_c_2// test_bool - transpiled function from /mnt/bigdata/YEASEEN/FuzzTranspilers-main/fuzzers/TFuzz/pre_post_works/c4go_transpile/runner.c:5functest_bool(inpint32) mystruct {
returnmystruct{noarch.BoolToInt(inp!=0)}
}
// main - transpiled function from /mnt/bigdata/YEASEEN/FuzzTranspilers-main/fuzzers/TFuzz/pre_post_works/c4go_transpile/runner.c:8funcmain() {
varbint32=5varresultmystruct=test_bool(b)
noarch.Printf([]byte("The value of 'a' in the returned struct is: %d\n\x00"), result.intM)
b=0result=test_bool(b)
noarch.Printf([]byte("The value of 'a' in the returned struct when b = 0 is: %d\n\x00"), result.intM)
return
}
Output after modification
The text was updated successfully, but these errors were encountered:
The legacy C codebase includes this feature. However, if a transpiler like c4go is used to translate the C code to Go, it will fail. After all, this is the task of a transpiler to convert legacy code to memory-safe modern languages
Source C code
C code's output
Translated Go code by
c4go
Go compiler build error
Root Cause
c4go
fails to convert boolean expression to Integer in Struct initializationModified Go code that works
I modified the initialization so that it could use
noarch.BoolToInt
function. And it gives the outputs as the source C code does.Instead of
return mystruct{inp != 0}
, my modification isreturn mystruct{noarch.BoolToInt(inp != 0)}
Output after modification
The text was updated successfully, but these errors were encountered: