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
Firstly, thanks for creating and maintaining such a useful utility.
I found commit ddcfe26 changes the default type mapping for long and int type. Indeed, unless the size is explicitly specified (e.g. uint64_t), the sizes of types int / unsigned int / long / unsigned long are arch-specific and implementation-specific. The change in commit ddcfe26 seems to be correct.
However, such changes will break the signature of existing Go bindings between versions (NVIDIA/go-nvml#31 (comment)). If the user wants to generate bindings for a specific platform (e.g. x86_64_linux), type unsigned long should be translated to uint or uint64 type.
#include<limits.h>#include<stdint.h>#include<stdio.h>#include<stdlib.h>// User defined function in Cvoidprintf_ulong(unsigned long*x)
{
*x= (unsigned long)UINT32_MAX+1;
printf("Value in function: %lu\n", *x);
}
// Binding in Golangvoidgo_binding()
{
uint64_tvalue64=0;
printf("Value before function: %lu\n", value64);
printf_ulong((unsigned long*)&value64);
printf("Value after function: %lu\n", value64);
}
intmain()
{
go_binding();
return0;
}
On x86_64 Linux machine (Ubuntu 20.04 LTS), compile and run with g++ 9.3.0:
$ uname -io x86_64 GNU/Linux
$ gcc --versiongcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0Copyright (C) 2019 Free Software Foundation, Inc.This is free software; see the source for copying conditions. There is NOwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gcc -std=c99 "test.c" -o "test"&& { ./"test"; rm "test" }Value before function: 0Value in function: 4294967296Value after function: 4294967296
C snippet (change types to uint32_t but keep the function input type as unsigned long):
#include<limits.h>#include<stdint.h>#include<stdio.h>#include<stdlib.h>// User defined function in Cvoidprintf_ulong(unsigned long*x)
{
*x= (unsigned long)UINT32_MAX+1;
printf("Value in function: %lu\n", *x);
}
// Binding in Golangvoidgo_binding()
{
uint32_tvalue32=0;
printf("Value before function: %u\n", value32);
printf_ulong((unsigned long*)&value32);
printf("Value after function: %u\n", value32);
}
intmain()
{
go_binding();
return0;
}
Got errors:
$ gcc -std=c99 "test.c" -o "test"&& { ./"test"; rm "test" }Value before function: 0Value in function: 4294967296Value after function: 0*** stack smashing detected ***: terminated
Firstly, thanks for creating and maintaining such a useful utility.
I found commit ddcfe26 changes the default type mapping for
long
andint
type. Indeed, unless the size is explicitly specified (e.g.uint64_t
), the sizes of typesint
/unsigned int
/long
/unsigned long
are arch-specific and implementation-specific. The change in commit ddcfe26 seems to be correct.However, such changes will break the signature of existing Go bindings between versions (NVIDIA/go-nvml#31 (comment)). If the user wants to generate bindings for a specific platform (e.g.
x86_64_linux
), typeunsigned long
should be translated touint
oruint64
type.Should we make this mapping to be arch-specific (wiki/Parser-config-section#arch)?
c-for-go/parser/predefined.go
Lines 81 to 104 in 291d167
Or is there a way to add some option in the configuration to customize it?
Related issue NVIDIA/go-nvml#31.
The text was updated successfully, but these errors were encountered: