@@ -33,6 +33,7 @@ func (e *Encoder) Encode(idx Index) error {
3333 hashToIndex , fanout , extraEdgesCount , generationV2OverflowCount := e .prepare (idx , hashes )
3434
3535 chunkSignatures := [][]byte {OIDFanoutChunk .Signature (), OIDLookupChunk .Signature (), CommitDataChunk .Signature ()}
36+ //nolint:gosec // G115: len() and hash.Size() are always small positive values
3637 chunkSizes := []uint64 {szUint32 * lenFanout , uint64 (len (hashes ) * e .hash .Size ()), uint64 (len (hashes ) * (e .hash .Size () + szCommitData ))}
3738 if extraEdgesCount > 0 {
3839 chunkSignatures = append (chunkSignatures , ExtraEdgeListChunk .Signature ())
@@ -86,7 +87,7 @@ func (e *Encoder) prepare(idx Index, hashes []plumbing.Hash) (hashToIndex map[pl
8687 hashToIndex = make (map [plumbing.Hash ]uint32 )
8788 fanout = make ([]uint32 , lenFanout )
8889 for i , hash := range hashes {
89- hashToIndex [hash ] = uint32 (i )
90+ hashToIndex [hash ] = uint32 (i ) //nolint:gosec // G115: i is loop index bounded by hashes count
9091 fanout [hash .Bytes ()[0 ]]++
9192 }
9293
@@ -99,9 +100,9 @@ func (e *Encoder) prepare(idx Index, hashes []plumbing.Hash) (hashToIndex map[pl
99100
100101 // Find out if we will need extra edge table
101102 for i := range len (hashes ) {
102- v , _ := idx .GetCommitDataByIndex (uint32 (i ))
103+ v , _ := idx .GetCommitDataByIndex (uint32 (i )) //nolint:gosec // G115: i is loop index
103104 if len (v .ParentHashes ) > 2 {
104- extraEdgesCount += uint32 (len (v .ParentHashes ) - 1 )
105+ extraEdgesCount += uint32 (len (v .ParentHashes ) - 1 ) //nolint:gosec // G115: parent count is small
105106 }
106107 if hasGenerationV2 && v .GenerationV2Data () > math .MaxUint32 {
107108 generationV2OverflowCount ++
@@ -114,7 +115,7 @@ func (e *Encoder) prepare(idx Index, hashes []plumbing.Hash) (hashToIndex map[pl
114115func (e * Encoder ) encodeFileHeader (chunkCount int ) (err error ) {
115116 if _ , err = e .Write (commitFileSignature ); err == nil {
116117 version := byte (1 )
117- if crypto .Hash (e .hash .Size ()) == crypto .Hash (crypto .SHA256 .Size ()) {
118+ if crypto .Hash (e .hash .Size ()) == crypto .Hash (crypto .SHA256 .Size ()) { //nolint:gosec // G115: hash.Size() is always small positive
118119 version = byte (2 )
119120 }
120121 _ , err = e .Write ([]byte {1 , version , byte (chunkCount ), 0 })
@@ -124,7 +125,7 @@ func (e *Encoder) encodeFileHeader(chunkCount int) (err error) {
124125
125126func (e * Encoder ) encodeChunkHeaders (chunkSignatures [][]byte , chunkSizes []uint64 ) (err error ) {
126127 // 8 bytes of file header, 12 bytes for each chunk header and 12 byte for terminator
127- offset := uint64 (szSignature + szHeader + (len (chunkSignatures )+ 1 )* (szChunkSig + szUint64 ))
128+ offset := uint64 (szSignature + szHeader + (len (chunkSignatures )+ 1 )* (szChunkSig + szUint64 )) //nolint:gosec // G115: small constants
128129 for i , signature := range chunkSignatures {
129130 if _ , err = e .Write (signature ); err == nil {
130131 err = binary .WriteUint64 (e , offset )
@@ -182,7 +183,7 @@ func (e *Encoder) encodeCommitData(hashes []plumbing.Hash, hashToIndex map[plumb
182183 parent2 = hashToIndex [commitData .ParentHashes [1 ]]
183184 default :
184185 parent1 = hashToIndex [commitData .ParentHashes [0 ]]
185- parent2 = uint32 (len (extraEdges )) | parentOctopusUsed
186+ parent2 = uint32 (len (extraEdges )) | parentOctopusUsed //nolint:gosec // G115: extraEdges count is bounded
186187 for _ , parentHash := range commitData .ParentHashes [1 :] {
187188 extraEdges = append (extraEdges , hashToIndex [parentHash ])
188189 }
@@ -196,7 +197,7 @@ func (e *Encoder) encodeCommitData(hashes []plumbing.Hash, hashToIndex map[plumb
196197 return extraEdges , generationV2Data , err
197198 }
198199
199- unixTime := uint64 (commitData .When .Unix ())
200+ unixTime := uint64 (commitData .When .Unix ()) //nolint:gosec // G115: Unix timestamp is always positive for valid commits
200201 unixTime |= uint64 (commitData .Generation ) << 34
201202 if err = binary .WriteUint64 (e , unixTime ); err != nil {
202203 return extraEdges , generationV2Data , err
@@ -222,7 +223,7 @@ func (e *Encoder) encodeGenerationV2Data(generationV2Data []uint64) (overflows [
222223 for _ , data := range generationV2Data {
223224 if data >= 0x80000000 {
224225 // overflow
225- if err = binary .WriteUint32 (e , uint32 (head )| 0x80000000 ); err != nil {
226+ if err = binary .WriteUint32 (e , uint32 (head )| 0x80000000 ); err != nil { //nolint:gosec // G115: head is bounded
226227 return nil , err
227228 }
228229 generationV2Data [head ] = data
0 commit comments