Browse code

libnetwork/bitmap: fix append to non-zero initialized length (makezero)

Changing to use binary.LittleEndian.AppendUint64, which does not require
the slice to have an initial size, and makes the code slightly more
straightforward.

libnetwork/bitmap/sequence.go:296:7: append to slice `ba` with non-zero initialized length (makezero)
ba = append(ba, bm...)
^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/02/08 03:01:20
Showing 1 changed files
... ...
@@ -286,15 +286,16 @@ func (h *Bitmap) validateOrdinal(ordinal uint64) error {
286 286
 
287 287
 // MarshalBinary encodes h into a binary representation.
288 288
 func (h *Bitmap) MarshalBinary() ([]byte, error) {
289
-	ba := make([]byte, 16)
290
-	binary.BigEndian.PutUint64(ba[0:], h.bits)
291
-	binary.BigEndian.PutUint64(ba[8:], h.unselected)
292 289
 	bm, err := h.head.toByteArray()
293 290
 	if err != nil {
294 291
 		return nil, fmt.Errorf("failed to serialize head: %v", err)
295 292
 	}
296
-	ba = append(ba, bm...)
297 293
 
294
+	// Pre-allocate capacity for "bits" and "unselected" (16 bytes) and head.
295
+	ba := make([]byte, 0, 16+len(bm))
296
+	ba = binary.BigEndian.AppendUint64(ba, h.bits)
297
+	ba = binary.BigEndian.AppendUint64(ba, h.unselected)
298
+	ba = append(ba, bm...)
298 299
 	return ba, nil
299 300
 }
300 301