bytealg.MakeNoZero has indirectly already leaked to the end user

178 views
Skip to first unread message

Jérôme LAFORGE

unread,
Jan 5, 2026, 12:39:10 AMJan 5
to golang-dev
Happy New Year everyone!
The documentation for bytealg.MakeNoZero states: "do not leak to the end user." However, in practice, there is at least one hack to access it using the unsafe package. Wouldn't it be simpler to avoid this kind of hack by directly exposing this function within the unsafe package, since this function has already indirectly leaked to the end user?

The hack:
func MakeSlice(c int) []byte {
var sb strings.Builder
sb.Grow(c)
s := unsafe.StringData(sb.String())
return unsafe.Slice(s, c)[:0]
}

Benny Siegert

unread,
Jan 5, 2026, 8:41:21 AMJan 5
to Jérôme LAFORGE, golang-dev
On Sun, 4 Jan 2026, Jérôme LAFORGE wrote:

> Happy New Year everyone!
> The documentation for bytealg.MakeNoZero states: "do not leak to the end user." However, in practice, there is at
> least one hack to access it using the unsafe package. Wouldn't it be simpler to avoid this kind of hack by directly
> exposing this function within the unsafe package, since this function has already indirectly leaked to the end
> user?

The part of the comment about "leaking to the end user" was not about the
MakeNoZero API but rather about the uninitialized memory it returns. You
need to make sure you overwrite all the memory with something useful
before handing its content to someone else.

--
Benny

Filippo Valsorda

unread,
Jan 5, 2026, 8:55:24 AMJan 5
to golang-dev
The question here is "do we want a documented unsafe.MakeUninitialized API with a forever backwards-compatibility promise?" You can open a proposal to discuss that. The fact that you can already get that through this specific unsupported unsafe strings.Builder hack is kind of inconsequential: everything leaks through sufficient unsafe use.
--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.

t hepudds

unread,
Jan 5, 2026, 9:53:40 AMJan 5
to Filippo Valsorda, golang-dev
Hi Jérôme, 

FWIW, just exposing some flavor of bytealg.MakeNoZero is likely more dangerous than you might guess, including with regards to respecting the Go memory model as far as I understand.

I just filed a related issue based on some observations Rick Hudson made while reviewing the runtime.freegc work.

Rick pointed out what seems to be a long-standing problem with current uses of bytealg.MakeNoZero in the standard library and other callers of mallocgc with needzero=false. Based on that, I filed:

   #77074 -- "runtime, strings, bytes: missing publication barriers after mallocgc with needzero=false and bytealg.MakeNoZero"
   https://github.com/golang/go/issues/77074

Separately, my guess would be that more directly exposing bytealg.MakeNoZero to end-users is relatively unlikely based on the goals of the Go project.

Best regards,
--thepudds

Jérôme LAFORGE

unread,
Jan 5, 2026, 10:19:49 AMJan 5
to golang-dev
It is more clear for "leaking to end user".
Many thank Benny

Jérôme LAFORGE

unread,
Jan 5, 2026, 10:29:54 AMJan 5
to golang-dev
Please find this modest proposal (be kind, it is my first one): https://github.com/golang/go/issues/77077

Jérôme LAFORGE

unread,
Jan 5, 2026, 10:36:14 AMJan 5
to golang-dev
Hi thepudds.

Thank for pointing me this issue. 
Reply all
Reply to author
Forward
0 new messages