Consensys / gnark

gnark is a fast zk-SNARK library that offers a high-level API to design circuits. The library is open source and developed under the Apache 2.0 license
https://hackmd.io/@gnark
Apache License 2.0
1.45k stars 381 forks source link

fix: fix slice init length #1288

Closed cuishuang closed 1 month ago

cuishuang commented 1 month ago

Description

The intention here should be to initialize a slice with a capacity of len(prof.Location) rather than initializing the length of this slice.

The online demo: https://go.dev/play/p/q1BcVCmvidW

Type of change

How has this been tested?

How has this been benchmarked?

Checklist:

CLAassistant commented 1 month ago

CLA assistant check
All committers have signed the CLA.

ivokub commented 1 month ago

Thanks for the PR. Right now it indeed seems that we initialize the nodes differently. I'll further check if we don't somehow assume that first len(prof.Location) values are there for inserting something, but currently it looks like not.

cuishuang commented 1 month ago

Thanks for the PR. Right now it indeed seems that we initialize the nodes differently. I'll further check if we don't somehow assume that first len(prof.Location) values are there for inserting something, but currently it looks like not.

I have searched many Go repositories, and this is a common issue. I am trying to solve it at the Go toolchain level

ivokub commented 1 month ago

Thanks for the PR. Right now it indeed seems that we initialize the nodes differently. I'll further check if we don't somehow assume that first len(prof.Location) values are there for inserting something, but currently it looks like not.

I have searched many Go repositories, and this is a common issue. I am trying to solve it at the Go toolchain level

Interesting! Imo it is a very good find. Indeed we have based our profiling implementation on the Go standard one, but capturing different metrics.

cuishuang commented 1 month ago

Thanks for the PR. Right now it indeed seems that we initialize the nodes differently. I'll further check if we don't somehow assume that first len(prof.Location) values are there for inserting something, but currently it looks like not.

I have searched many Go repositories, and this is a common issue. I am trying to solve it at the Go toolchain level

Interesting! Imo it is a very good find. Indeed we have based our profiling implementation on the Go standard one, but capturing different metrics.

Thank you for your recognition!

I am preparing to add a detection item in go vet to avoid this issue. In the future, it may only be necessary to update the Go version, as there will be relevant detection items in the latest toolchain

ivokub commented 1 month ago

I checked - in gnark this code path is never called and cannot be called without modifying the internal implementation. And even if it is initalized incorrectly, then later in selectNodesForGraph the nil entries are filtered out. But the issue is valid in any case.

Thanks for reporting!