Unintended public exposure of C symbols via @cImport in Zig
0 reputation · 07 Aug 2021, 22:11 UTC
Unintended public exposure of C symbols via @cImport in Zig
Goal: Verify whether placing a @cImport block inside a pub block causes the imported C declarations to become part of the module’s public API, potentially allowing other Zig files to access them without an explicit pub qualifier.
Constraints: Zig defaults to file‑private visibility; a @cImport inherits the visibility of its surrounding block. The compiler warns in version 0.13.0 when the import is inside a pub block, but it is unclear if this warning fully mitigates the risk or if additional patterns are needed to keep C symbols private while still usable internally.
Questions:
- Does moving the @cImport out of any pub block guarantee that the imported C symbols remain file‑private?
- What is the recommended way to isolate C imports for internal use without leaking them to the module’s public interface?
- Is the existing compiler warning sufficient to prevent accidental exposure, or should developers adopt extra safeguards such as wrapping the import in an anonymous namespace?