Re: [go-nuts] Re: Mapping C pointer to BYTE to golang []byte

2017-12-15 Thread 'Bryan C. Mills' via golang-nuts
You can ignore the vet warning if you are certain that the uintptr values
you are converting to unsafe.Pointer are *valid pointers* (i.e., not
non-pointer sentinel values) that point *outside the Go heap*.

It sounds like that is probably the case, but you might want to check with
someone more familiar with Windows system calls to be sure.

On Fri, Dec 15, 2017 at 3:23 PM, snmed  wrote:

> Sorry if I wasn't clear about that, I calling CertFindCertificateInStore
> 
>  which according to MSDN returns a pointer to a struct of
> type PCCERT_CONTEXT and must be freed with CertFreeCertificateContext
> 
> .
>
> The returned pointer is freed when passed as the *pPrevCertContext*
>> parameter on a subsequent call to the function. Otherwise, the pointer must
>> be explicitly freed by calling *CertFreeCertificateContext*
>> .
>> A *pPrevCertContext* that is not *NULL* is always freed by
>> *CertFindCertificateInStore* using a call to *CertFreeCertificateContext*,
>> even if there is an error in the function.
>
>
> So yes it is allocated by the crypt API of Windows, but i'm still not sure
> if can ignore the vet warning or not. As far as I understand, go only
> manages the pointer to the struct but not the struct itself.
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/golang-nuts/0JYB0-ZcFpk/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Re: Mapping C pointer to BYTE to golang []byte

2017-12-15 Thread snmed
Sorry if I wasn't clear about that, I calling CertFindCertificateInStore 

 which 
according to MSDN returns a pointer to a struct of type PCCERT_CONTEXT and 
must be freed with CertFreeCertificateContext 

.

The returned pointer is freed when passed as the *pPrevCertContext* 
> parameter on a subsequent call to the function. Otherwise, the pointer must 
> be explicitly freed by calling *CertFreeCertificateContext* 
> .
>  
> A *pPrevCertContext* that is not *NULL* is always freed by 
> *CertFindCertificateInStore* using a call to *CertFreeCertificateContext*, 
> even if there is an error in the function.


So yes it is allocated by the crypt API of Windows, but i'm still not sure 
if can ignore the vet warning or not. As far as I understand, go only 
manages the pointer to the struct but not the struct itself.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Re: Mapping C pointer to BYTE to golang []byte

2017-12-15 Thread 'Bryan C. Mills' via golang-nuts
You haven't actually said which system call you're invoking, but from the
name CERT_CONTEXT and the layout of the struct I'm guessing it's probably
CertCreateCertificateContext

 or CertEnumCertificatesInStore
?

I don't really know anything about Windows system calls, but it does seem
like those allocate memory somewhere outside of the Go heap.


On Fri, Dec 15, 2017 at 1:54 AM, snmed  wrote:

> Hi Bryan
>
> But the returned uintptr from syscall is as far as i know a pointer to a
> struct which has been created by the C API, so go runtime won't touch it or
> do I miss something?
>
>
> Am Freitag, 15. Dezember 2017 00:48:51 UTC+1 schrieb Bryan Mills:
>>
>> In this case, the vet tool is correct if you're making the syscall with
>> Go-allocated memory.
>> The Go runtime is allowed to move values around: the address of a Go
>> variable is only pinned for the duration of the syscall itself.
>>
>> If you've got C-allocated memory (or statically-allocated memory),
>> https://golang.org/issue/13656#issuecomment-303216308 has a solution
>> that avoids copying and is more robust to large sizes.
>>
>>
>> On Thursday, December 14, 2017 at 5:27:57 AM UTC-5, snmed wrote:
>>>
>>> Okay I found a way, this seems to work:
>>>
>>> ca := (*[100]byte) (unsafe.Pointer(certctx.pbCertEncoded))[:certctx.
>>> cbCertEncoded]
>>>
>>> or
>>>
>>> mm := make([]byte, certctx.cbCertEncoded)
>>> for i := uint32(0) ; i < certctx.cbCertEncoded; i++ {
>>> mm[i] = *((*byte)(unsafe.Pointer(certctx.pbCertEncoded + uintptr
>>> (i) * unsafe.Sizeof(new(byte)
>>> }
>>>
>>>
>>>
>>> Anyway, the vet tool is complaining:
>>>
>>> main.go:106: possible misuse of unsafe.Pointer
>>> main.go:109: possible misuse of unsafe.Pointer
>>>
>>> This is the code fragment:
>>>
>>> 104certctx := new(CERT_CONTEXT)
>>> 105
>>> 106certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
>>> 107
>>> 108
>>> 109ca := (*[100]byte) (unsafe.Pointer(certctx.pbCertEncoded))[:
>>> certctx.cbCertEncoded]
>>>
>>> Is there another way to use syscall return values uintptr without vet
>>> warnings? And which solution should I prefer? I think the later one should
>>> be more safe, isn't it?
>>>
>>> Cheers
>>>
>>>
>>> Am Donnerstag, 14. Dezember 2017 09:29:38 UTC+1 schrieb snmed:

 Hi Miki

 I'm using syscall package and no C import, but maybe it is possible to
 use this function as well?

 Am Donnerstag, 14. Dezember 2017 09:18:26 UTC+1 schrieb Miki Tebeka:
>
> Do you mean
>
> func C.GoBytes(unsafe.Pointer, C.int) []byte
>
>  ?
>
> On Thursday, December 14, 2017 at 9:05:32 AM UTC+2, snmed wrote:
>>
>> Hi all
>>
>> I'm trying to map a C structure to an equivalent go struct, but I
>> bumped into a problem with a pointer to byte that is actually an array of
>> bytes.
>>
>> Here is the C struct:
>>
>> typedef struct _CERT_CONTEXT {
>>   DWORD  dwCertEncodingType;
>>   BYTE   *pbCertEncoded;
>>   DWORD  cbCertEncoded;
>>   PCERT_INFO pCertInfo;
>>   HCERTSTORE hCertStore;
>> } CERT_CONTEXT, *PCERT_CONTEXT;
>>
>>
>> and this is my go struct:
>>
>> type CERT_CONTEXT struct {
>> dwCertEncodingType uint32
>> pbCertEncoded  uintptr
>> cbCertEncoded  uint32
>> pCertInfo  uintptr
>> hCertStore uintptr
>> }
>>
>> for my case I need only the first 3 fields and I do not have any
>> problem to get 1 and 3, but I can't remember how to translate the second
>> field to a slice of bytes.
>> This is how I map the struct from an uintptr and print it to the
>> console:
>>
>> certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
>> fmt.Printf("%v\n", certctx)
>>
>> >&{1 807520 674 833008 789360}
>>
>> Any advise is warmly welcome.
>>
>> Cheers,
>> Sandro
>>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/golang-nuts/0JYB0-ZcFpk/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Mapping C pointer to BYTE to golang []byte

2017-12-14 Thread snmed
Hi Bryan

But the returned uintptr from syscall is as far as i know a pointer to a 
struct which has been created by the C API, so go runtime won't touch it or 
do I miss something?

Am Freitag, 15. Dezember 2017 00:48:51 UTC+1 schrieb Bryan Mills:
>
> In this case, the vet tool is correct if you're making the syscall with 
> Go-allocated memory.
> The Go runtime is allowed to move values around: the address of a Go 
> variable is only pinned for the duration of the syscall itself.
>
> If you've got C-allocated memory (or statically-allocated memory), 
> https://golang.org/issue/13656#issuecomment-303216308 has a solution that 
> avoids copying and is more robust to large sizes.
>
>
> On Thursday, December 14, 2017 at 5:27:57 AM UTC-5, snmed wrote:
>>
>> Okay I found a way, this seems to work:
>>
>> ca := (*[100]byte) (unsafe.Pointer(certctx.pbCertEncoded))[:certctx.
>> cbCertEncoded]
>>
>> or
>>
>> mm := make([]byte, certctx.cbCertEncoded)
>> for i := uint32(0) ; i < certctx.cbCertEncoded; i++ {
>> mm[i] = *((*byte)(unsafe.Pointer(certctx.pbCertEncoded + uintptr(
>> i) * unsafe.Sizeof(new(byte)
>> }
>>
>>
>>
>> Anyway, the vet tool is complaining:
>>
>> main.go:106: possible misuse of unsafe.Pointer
>> main.go:109: possible misuse of unsafe.Pointer
>>
>> This is the code fragment:
>>
>> 104certctx := new(CERT_CONTEXT)
>> 105
>> 106certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
>> 107
>> 108
>> 109ca := (*[100]byte) (unsafe.Pointer(certctx.pbCertEncoded))[:
>> certctx.cbCertEncoded]
>>
>> Is there another way to use syscall return values uintptr without vet 
>> warnings? And which solution should I prefer? I think the later one should 
>> be more safe, isn't it?
>>
>> Cheers
>>
>>
>> Am Donnerstag, 14. Dezember 2017 09:29:38 UTC+1 schrieb snmed:
>>>
>>> Hi Miki 
>>>
>>> I'm using syscall package and no C import, but maybe it is possible to 
>>> use this function as well?
>>>
>>> Am Donnerstag, 14. Dezember 2017 09:18:26 UTC+1 schrieb Miki Tebeka:

 Do you mean

 func C.GoBytes(unsafe.Pointer, C.int) []byte

  ?

 On Thursday, December 14, 2017 at 9:05:32 AM UTC+2, snmed wrote:
>
> Hi all
>
> I'm trying to map a C structure to an equivalent go struct, but I 
> bumped into a problem with a pointer to byte that is actually an array of 
> bytes.
>
> Here is the C struct:
>
> typedef struct _CERT_CONTEXT {
>   DWORD  dwCertEncodingType;
>   BYTE   *pbCertEncoded;
>   DWORD  cbCertEncoded;
>   PCERT_INFO pCertInfo;
>   HCERTSTORE hCertStore;
> } CERT_CONTEXT, *PCERT_CONTEXT;
>
>
> and this is my go struct:
>
> type CERT_CONTEXT struct {
> dwCertEncodingType uint32
> pbCertEncoded  uintptr
> cbCertEncoded  uint32
> pCertInfo  uintptr
> hCertStore uintptr
> }
>
> for my case I need only the first 3 fields and I do not have any 
> problem to get 1 and 3, but I can't remember how to translate the second 
> field to a slice of bytes.
> This is how I map the struct from an uintptr and print it to the 
> console:
>
> certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
> fmt.Printf("%v\n", certctx)
> 
> >&{1 807520 674 833008 789360}
>
> Any advise is warmly welcome.
>
> Cheers,
> Sandro
>


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Mapping C pointer to BYTE to golang []byte

2017-12-14 Thread 'Bryan Mills' via golang-nuts
In this case, the vet tool is correct if you're making the syscall with 
Go-allocated memory.
The Go runtime is allowed to move values around: the address of a Go 
variable is only pinned for the duration of the syscall itself.

If you've got C-allocated memory (or statically-allocated 
memory), https://golang.org/issue/13656#issuecomment-303216308 has a 
solution that avoids copying and is more robust to large sizes.


On Thursday, December 14, 2017 at 5:27:57 AM UTC-5, snmed wrote:
>
> Okay I found a way, this seems to work:
>
> ca := (*[100]byte) (unsafe.Pointer(certctx.pbCertEncoded))[:certctx.
> cbCertEncoded]
>
> or
>
> mm := make([]byte, certctx.cbCertEncoded)
> for i := uint32(0) ; i < certctx.cbCertEncoded; i++ {
> mm[i] = *((*byte)(unsafe.Pointer(certctx.pbCertEncoded + uintptr(i
> ) * unsafe.Sizeof(new(byte)
> }
>
>
>
> Anyway, the vet tool is complaining:
>
> main.go:106: possible misuse of unsafe.Pointer
> main.go:109: possible misuse of unsafe.Pointer
>
> This is the code fragment:
>
> 104certctx := new(CERT_CONTEXT)
> 105
> 106certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
> 107
> 108
> 109ca := (*[100]byte) (unsafe.Pointer(certctx.pbCertEncoded))[:
> certctx.cbCertEncoded]
>
> Is there another way to use syscall return values uintptr without vet 
> warnings? And which solution should I prefer? I think the later one should 
> be more safe, isn't it?
>
> Cheers
>
>
> Am Donnerstag, 14. Dezember 2017 09:29:38 UTC+1 schrieb snmed:
>>
>> Hi Miki 
>>
>> I'm using syscall package and no C import, but maybe it is possible to 
>> use this function as well?
>>
>> Am Donnerstag, 14. Dezember 2017 09:18:26 UTC+1 schrieb Miki Tebeka:
>>>
>>> Do you mean
>>>
>>> func C.GoBytes(unsafe.Pointer, C.int) []byte
>>>
>>>  ?
>>>
>>> On Thursday, December 14, 2017 at 9:05:32 AM UTC+2, snmed wrote:

 Hi all

 I'm trying to map a C structure to an equivalent go struct, but I 
 bumped into a problem with a pointer to byte that is actually an array of 
 bytes.

 Here is the C struct:

 typedef struct _CERT_CONTEXT {
   DWORD  dwCertEncodingType;
   BYTE   *pbCertEncoded;
   DWORD  cbCertEncoded;
   PCERT_INFO pCertInfo;
   HCERTSTORE hCertStore;
 } CERT_CONTEXT, *PCERT_CONTEXT;


 and this is my go struct:

 type CERT_CONTEXT struct {
 dwCertEncodingType uint32
 pbCertEncoded  uintptr
 cbCertEncoded  uint32
 pCertInfo  uintptr
 hCertStore uintptr
 }

 for my case I need only the first 3 fields and I do not have any 
 problem to get 1 and 3, but I can't remember how to translate the second 
 field to a slice of bytes.
 This is how I map the struct from an uintptr and print it to the 
 console:

 certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
 fmt.Printf("%v\n", certctx)
 
 >&{1 807520 674 833008 789360}

 Any advise is warmly welcome.

 Cheers,
 Sandro

>>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Mapping C pointer to BYTE to golang []byte

2017-12-14 Thread snmed
Okay I found a way, this seems to work:

ca := (*[100]byte) (unsafe.Pointer(certctx.pbCertEncoded))[:certctx.
cbCertEncoded]

or

mm := make([]byte, certctx.cbCertEncoded)
for i := uint32(0) ; i < certctx.cbCertEncoded; i++ {
mm[i] = *((*byte)(unsafe.Pointer(certctx.pbCertEncoded + uintptr(i) 
* unsafe.Sizeof(new(byte)
}



Anyway, the vet tool is complaining:

main.go:106: possible misuse of unsafe.Pointer
main.go:109: possible misuse of unsafe.Pointer

This is the code fragment:

104certctx := new(CERT_CONTEXT)
105
106certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
107
108
109ca := (*[100]byte) (unsafe.Pointer(certctx.pbCertEncoded))[:
certctx.cbCertEncoded]

Is there another way to use syscall return values uintptr without vet 
warnings? And which solution should I prefer? I think the later one should 
be more safe, isn't it?

Cheers


Am Donnerstag, 14. Dezember 2017 09:29:38 UTC+1 schrieb snmed:
>
> Hi Miki 
>
> I'm using syscall package and no C import, but maybe it is possible to use 
> this function as well?
>
> Am Donnerstag, 14. Dezember 2017 09:18:26 UTC+1 schrieb Miki Tebeka:
>>
>> Do you mean
>>
>> func C.GoBytes(unsafe.Pointer, C.int) []byte
>>
>>  ?
>>
>> On Thursday, December 14, 2017 at 9:05:32 AM UTC+2, snmed wrote:
>>>
>>> Hi all
>>>
>>> I'm trying to map a C structure to an equivalent go struct, but I bumped 
>>> into a problem with a pointer to byte that is actually an array of bytes.
>>>
>>> Here is the C struct:
>>>
>>> typedef struct _CERT_CONTEXT {
>>>   DWORD  dwCertEncodingType;
>>>   BYTE   *pbCertEncoded;
>>>   DWORD  cbCertEncoded;
>>>   PCERT_INFO pCertInfo;
>>>   HCERTSTORE hCertStore;
>>> } CERT_CONTEXT, *PCERT_CONTEXT;
>>>
>>>
>>> and this is my go struct:
>>>
>>> type CERT_CONTEXT struct {
>>> dwCertEncodingType uint32
>>> pbCertEncoded  uintptr
>>> cbCertEncoded  uint32
>>> pCertInfo  uintptr
>>> hCertStore uintptr
>>> }
>>>
>>> for my case I need only the first 3 fields and I do not have any problem 
>>> to get 1 and 3, but I can't remember how to translate the second field to a 
>>> slice of bytes.
>>> This is how I map the struct from an uintptr and print it to the console:
>>>
>>> certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
>>> fmt.Printf("%v\n", certctx)
>>> 
>>> >&{1 807520 674 833008 789360}
>>>
>>> Any advise is warmly welcome.
>>>
>>> Cheers,
>>> Sandro
>>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Mapping C pointer to BYTE to golang []byte

2017-12-14 Thread snmed
Hi Miki 

I'm using syscall package and no C import, but maybe it is possible to use 
this function as well?

Am Donnerstag, 14. Dezember 2017 09:18:26 UTC+1 schrieb Miki Tebeka:
>
> Do you mean
>
> func C.GoBytes(unsafe.Pointer, C.int) []byte
>
>  ?
>
> On Thursday, December 14, 2017 at 9:05:32 AM UTC+2, snmed wrote:
>>
>> Hi all
>>
>> I'm trying to map a C structure to an equivalent go struct, but I bumped 
>> into a problem with a pointer to byte that is actually an array of bytes.
>>
>> Here is the C struct:
>>
>> typedef struct _CERT_CONTEXT {
>>   DWORD  dwCertEncodingType;
>>   BYTE   *pbCertEncoded;
>>   DWORD  cbCertEncoded;
>>   PCERT_INFO pCertInfo;
>>   HCERTSTORE hCertStore;
>> } CERT_CONTEXT, *PCERT_CONTEXT;
>>
>>
>> and this is my go struct:
>>
>> type CERT_CONTEXT struct {
>> dwCertEncodingType uint32
>> pbCertEncoded  uintptr
>> cbCertEncoded  uint32
>> pCertInfo  uintptr
>> hCertStore uintptr
>> }
>>
>> for my case I need only the first 3 fields and I do not have any problem 
>> to get 1 and 3, but I can't remember how to translate the second field to a 
>> slice of bytes.
>> This is how I map the struct from an uintptr and print it to the console:
>>
>> certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
>> fmt.Printf("%v\n", certctx)
>> 
>> >&{1 807520 674 833008 789360}
>>
>> Any advise is warmly welcome.
>>
>> Cheers,
>> Sandro
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Mapping C pointer to BYTE to golang []byte

2017-12-14 Thread Miki Tebeka
Do you mean

func C.GoBytes(unsafe.Pointer, C.int) []byte

 ?

On Thursday, December 14, 2017 at 9:05:32 AM UTC+2, snmed wrote:
>
> Hi all
>
> I'm trying to map a C structure to an equivalent go struct, but I bumped 
> into a problem with a pointer to byte that is actually an array of bytes.
>
> Here is the C struct:
>
> typedef struct _CERT_CONTEXT {
>   DWORD  dwCertEncodingType;
>   BYTE   *pbCertEncoded;
>   DWORD  cbCertEncoded;
>   PCERT_INFO pCertInfo;
>   HCERTSTORE hCertStore;
> } CERT_CONTEXT, *PCERT_CONTEXT;
>
>
> and this is my go struct:
>
> type CERT_CONTEXT struct {
> dwCertEncodingType uint32
> pbCertEncoded  uintptr
> cbCertEncoded  uint32
> pCertInfo  uintptr
> hCertStore uintptr
> }
>
> for my case I need only the first 3 fields and I do not have any problem 
> to get 1 and 3, but I can't remember how to translate the second field to a 
> slice of bytes.
> This is how I map the struct from an uintptr and print it to the console:
>
> certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
> fmt.Printf("%v\n", certctx)
> 
> >&{1 807520 674 833008 789360}
>
> Any advise is warmly welcome.
>
> Cheers,
> Sandro
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.