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 := (*[1000000]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:
>
> 104    certctx := new(CERT_CONTEXT)
> 105
> 106    certctx = (*CERT_CONTEXT) (unsafe.Pointer(pccert_context))
> 107
> 108    
> 109    ca := (*[1000000]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.

Reply via email to