<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/cgo-s-pointer-check-is-all-over-the-place-hylps5jqs" -->

---
title: cgo's pointer check is all over the place | daily.dev
description: Go's cgo pointer checker behaves inconsistently depending on how a pointer expression is written in source code. Passing `unsafe.Pointer(&s.small)` directly to...
canonical: https://daily.dev/posts/cgo-s-pointer-check-is-all-over-the-place-hylps5jqs
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: cgo's pointer check is all over the place | daily.dev
og:description: Go's cgo pointer checker behaves inconsistently depending on how a pointer expression is written in source code. Passing `unsafe.Pointer(&s.small)` directly to...
og:url: https://daily.dev/posts/cgo-s-pointer-check-is-all-over-the-place-hylps5jqs
og:image: https://api.daily.dev/og/posts/hyLpS5jQS.png
og:image:alt: cgo's pointer check is all over the place
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# cgo's pointer check is all over the place

**[Lobsters](https://daily.dev/sources/lobsters)** · 20 min read · 1 upvotes · 0 comments

## Summary

Go's cgo pointer checker behaves inconsistently depending on how a pointer expression is written in source code. Passing `unsafe.Pointer(&s.small)` directly to a C call passes, but assigning it to a variable first and passing the variable panics with 'Go pointer to unpinned Go pointer', even though both reference the same address. Benchmarks show the checked path costs between 110x and 1600x more than the direct path, going from 48ns to as much as 75,748ns per call. The check also only scans two levels of nested pointers deep, so cgocheck2's 'complete checking' still misses unpinned pointers 999,997 nodes into a linked list. The article recommends taking the address directly inside the C call, but pinning everything C can reach remains the programmer's job.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://theconsensus.dev/p/2026/11/15/cgos-pointer-check.html>

## Questions this post answers

### why does my cgo call panic with 'Go pointer to unpinned Go pointer' only when I assign the unsafe.Pointer to a variable first

cgo's pointer checker works syntactically, not by Go type, and only recognizes patterns like &x, &x[i], or x[i:j] written directly in the call. When you assign unsafe.Pointer(&s.field) to a variable before passing it, cgo loses track of what the pointer refers to and falls back to runtime.cgoCheckUnknownPointer, which conservatively checks the entire containing object for unpinned pointers rather than just the field, causing a panic on otherwise legal code.

_Developers debugging cgo pointer panics can track these syntax quirks and Go runtime behavior on daily.dev._

### how much slower is passing an unsafe.Pointer through a variable to a cgo function versus passing it directly in the call

Passing the pointer directly in the call keeps a constant cost around 48ns regardless of how many pointers the struct contains, since cgo skips the exhaustive check when it recognizes the syntax. Passing through a variable forces runtime.cgoCheckUnknownPointer to scan every pointer in the object, costing 5,241ns for 1,024 pointers up to 75,748ns for 16,384 pointers, a 110x to roughly 1600x slowdown measured on Go 1.27.0.

_Anyone tuning Go FFI call paths can compare these cgocheck costs before shipping performance-sensitive code on daily.dev._

### does Go's cgocheck2 experiment catch all unpinned Go pointers reachable from a cgo call argument

No, cgocheck2 only scans two levels of pointer indirection deep, even though it is documented as doing 'complete checking'. In a linked list test with a million nodes, only the first two nodes needed to be pinned to avoid a panic, meaning 999,997 unpinned nodes that C code could still reach went undetected under GOEXPERIMENT=cgocheck2 on Go 1.27.0.

_Teams relying on cgo pinning guarantees can verify edge cases like this before trusting cgocheck2 in production, tracked on daily.dev._

## Similar posts on daily.dev

- [Eliminating Go bounds checks with unsafe](https://daily.dev/posts/eliminating-go-bounds-checks-with-unsafe-ddqifwlib) · Hacker News · 1 upvotes · 0 comments
- [Excessive nil pointer checks in Go](https://daily.dev/posts/excessive-nil-pointer-checks-in-go-ezeglbnjy) · Lobsters · 7 upvotes · 0 comments
- [cloud security, system engineering and compilers](https://daily.dev/posts/cloud-security-system-engineering-and-compilers-sxfl15jgp) · Lobsters · 1 upvotes · 0 comments

---

Tags: [#performance](https://daily.dev/tags/performance), [#golang](https://daily.dev/tags/golang)

[View this post on daily.dev](https://daily.dev/posts/cgo-s-pointer-check-is-all-over-the-place-hylps5jqs)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"cgo's pointer check is all over the place","url":"https://daily.dev/posts/cgo-s-pointer-check-is-all-over-the-place-hylps5jqs","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/cgo-s-pointer-check-is-all-over-the-place-hylps5jqs"},"datePublished":"2026-11-24T20:26:48.424Z","dateModified":"2026-11-24T20:27:36.287Z","description":"Go's cgo pointer checker behaves inconsistently depending on how a pointer expression is written in source code. Passing `unsafe.Pointer(\u0026s.small)` directly to...","image":"https://theconsensus.dev/static/cgos-pointer-check.avif","thumbnailUrl":"https://theconsensus.dev/static/cgos-pointer-check.avif","isAccessibleForFree":true,"articleSection":"Lobsters","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Lobsters","logo":"https://media.daily.dev/image/upload/s--tl8v_Fku--/f_auto,t_logo/v1698841318/logos/lobste.jpg","url":"https://daily.dev/sources/lobsters"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/cgo-s-pointer-check-is-all-over-the-place-hylps5jqs","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"performance,golang","timeRequired":"PT20M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Lobsters","item":"https://daily.dev/sources/lobsters"},{"@type":"ListItem","position":3,"name":"cgo's pointer check is all over the place"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/cgo-s-pointer-check-is-all-over-the-place-hylps5jqs#faq","mainEntity":[{"@type":"Question","name":"why does my cgo call panic with 'Go pointer to unpinned Go pointer' only when I assign the unsafe.Pointer to a variable first","acceptedAnswer":{"@type":"Answer","text":"cgo's pointer checker works syntactically, not by Go type, and only recognizes patterns like \u0026x, \u0026x[i], or x[i:j] written directly in the call. When you assign unsafe.Pointer(\u0026s.field) to a variable before passing it, cgo loses track of what the pointer refers to and falls back to runtime.cgoCheckUnknownPointer, which conservatively checks the entire containing object for unpinned pointers rather than just the field, causing a panic on otherwise legal code. Developers debugging cgo pointer panics can track these syntax quirks and Go runtime behavior on daily.dev."}},{"@type":"Question","name":"how much slower is passing an unsafe.Pointer through a variable to a cgo function versus passing it directly in the call","acceptedAnswer":{"@type":"Answer","text":"Passing the pointer directly in the call keeps a constant cost around 48ns regardless of how many pointers the struct contains, since cgo skips the exhaustive check when it recognizes the syntax. Passing through a variable forces runtime.cgoCheckUnknownPointer to scan every pointer in the object, costing 5,241ns for 1,024 pointers up to 75,748ns for 16,384 pointers, a 110x to roughly 1600x slowdown measured on Go 1.27.0. Anyone tuning Go FFI call paths can compare these cgocheck costs before shipping performance-sensitive code on daily.dev."}},{"@type":"Question","name":"does Go's cgocheck2 experiment catch all unpinned Go pointers reachable from a cgo call argument","acceptedAnswer":{"@type":"Answer","text":"No, cgocheck2 only scans two levels of pointer indirection deep, even though it is documented as doing 'complete checking'. In a linked list test with a million nodes, only the first two nodes needed to be pinned to avoid a panic, meaning 999,997 unpinned nodes that C code could still reach went undetected under GOEXPERIMENT=cgocheck2 on Go 1.27.0. Teams relying on cgo pinning guarantees can verify edge cases like this before trusting cgocheck2 in production, tracked on daily.dev."}}]}
```

